Reactive programming has been touted as one of the most important new programming paradigms in recent history. The idea of designing software as asynchronous streams of data is compelling, as evidenced by the growing popularity of reactive programming and the frameworks based on it. In this session we’ll explore what it is, where it came from, and look at its implementation in the Reactive Extensions API.
3. What is a
Reactive
System?
“accepts inputs, performs
transformations on them and
produces outputs”
“repeatedly prompted
by the outside world and
their role is to
continuously respond to
external inputs”
“On the development of reactive systems”, Harel and
Pnueli, 1985
vs.
Transformational
System
5. Reactive
(Software)
Systems
Today refers to large software
systems as a whole
Parts of the system are decoupled
in space and time via message
passing
A reactive software system is a
group of reactive systems, each
responding to streams of
messages
Responsive
Message Driven
ResilientElastic
Value
Form
Means
www.reactivemanifesto.org
6. Reactive Program
The analog of message
passing in a system is
eventing in a program
Reactive System
Responsive
Event Driven
ResilientElastic
Value
Form
Means
8. Observation -
Decoupling
Code in Time
To model a piece of code
like a reactive system is to
give it the ability to respond
to other code
Some sort of observation –
like a callback, delegate,
event, etc.
Object A Object B
start observing
notify
notify
notify
stop observing
9. Observation
class A {
void processAll() {
while(b.hasItems()) {
process(b.next());
}
}
}
Class A
Class B
next() observe() notify()
class A {
public A() {
b.observe(this);
}
void notify(item) {
process(item);
}
}
10. Declarative vs.
Imperative
Reactive-style components
lend themselves to being
declarative
Say how things are, not how
to do them
5:00 Clock
Clock
5:00
5:00
“The time should be here”
Each second:
- Get the time
- Update the view
5:00
time?
13. Where can we apply this?
User Interface Logic
Datastore
REST Client
Cloud
14. class ViewModel {
void update() {
var data = await restClient.Get();
dataStore.Save(data);
data = Logic(data);
UpdateUI(data);
}
}
User Interface
Datastore
REST Client
Cloud
ViewModel
Business
Logic
15. class ViewModel {
void notify(data) {
UpdateUI(data);
}
}
User Interface
Datastore
REST Client
Cloud
ViewModel
Business
Logic
Provider
notify()
ViewModel Provider
observe entity X
notify
notify
stop observing
update X
REST call and save
update X
REST call and save
18. Reactive Extensions
■ Create observables and observe them
■ Manipulate the flow of events
Observer Observable
subscribe()
onNext()
Op
19. Example
var x = 5;
var y = 4;
var s <=> x + y; // s == 9
x = 9; // s == 13
var x = new Number { Value = 5 };
var y = new Number { Value = 4 };
var s = new Sum(x, y);
Console.WriteLine($"s={s.Value}");
x.Value = 9;
Console.WriteLine($"s={s.Value}");
https://goo.gl/gn3iTe
20. class Number
{
private int _value;
public int Value {
get { return _value; }
set { _value = value;
_subject.OnNext(_value); }
}
private readonly Subject<int> _subject = new Subject<int>();
public IObservable<int> Changed {
get { return _subject.AsObservable(); }
}
}
21. class Sum
{
private int _x;
private int _y;
private int _sum;
public int Value { get { return _sum; } }
public Sum(Number x, Number y)
{
_x = x.Value;
_y = y.Value;
_sum = _x + _y;
x.Changed.Subscribe((n) => { _x = n; _sum = _x + _y; });
y.Changed.Subscribe((n) => { _y = n; _sum = _x + _y; });
}
}
22. RX Operators – Creation
Create an observable that emits a sequence of integers spaced by a
given time interval
Interval
Interval( x sec. )
x sec. x sec. x sec. x sec.
0 1 2 3
24. Transform the items emitted by an Observable by applying a function to
each of them
Map
RX Operators – Transformation
1 2 3
10 20 30
Map( x => 10 * x )
25. Combine multiple Observables into one by merging their emissions
Merge
RX Operators – Joining
20 40 60
Merge
1 1
80 100
20 40 60 80 1001 1
26. Summary
■ Reactive programming can be thought of as modeling code
as individual reactive systems
■ Decoupling code via observation reduces dependencies and
facilitates declarative programming
■ Reactive Extensions provide an implementation of the
Observer pattern along with operators