4. Primary-Backup
• Bonjour can guarantee no more than one
Primary replica can publish its service
• Multiple Backup replicas communicate with
the Primary to keep all replicas synchronized
• Writes are made to the Primary and
propagated back to the Backup replicas
• If the Primary fails, a Backup can take over.
The remaining backups sync to the new
Primary
5. failed to
initial become
primary
start primary failed to start
trying to trying to
become connect to
primary primary
backup did start
primary did start backup failed to start
failed to
primary connect to backup
primary
stop stop
stopping stopping
error
primary backup
primary did stop backup did stop
stopped
6. Context State
Request() Handle()
state->Handle()
ConcreteStateA ConcreteStateB
Handle() Handle()
7. State
Context
TransitionTo(Context c, State s)
Enter(Context c)
SetState(State s)
Leave(Context c)
Foo()
Bar()
Foo(StateContext c)
Baz()
Bar(StateContext c)
....
Baz(StateContext c)
...
state->Foo()
BaseState
this->Leave(c)
TransitionTo(Context c, State s) c->SetState(s)
Enter(Context c) s->Enter(c)
Leave(Context c)
Foo(StateContext c)
Bar(StateContext c)
Baz(StateContext c)
...
ConcreteStateA ConcreteStateB
TransitionTo
Enter(Context c) Foo(StateContext c)
(c, ConcreteStateA)
Baz(StateContext c) Bar(StateContext c)
23. Same result on stop
} else if (_state == GSGameControllerStateStopped) {
if (oldState == GSGameControllerStatePrimary) {
[self tearDownPrimary];
} else if (oldState == GSGameControllerStateStoppingPrimary) {
[self tearDownPrimary];
} else if (oldState == GSGameControllerStateBackup) {
[self tearDownBackup];
} else if (oldState == GSGameControllerStateStoppingBackup) {
[self tearDownBackup];
}
[self tellDelegate:_delegate
performSelectorWithSelf:
@selector(gameControllerDidStop:)];
}
24. Result
• GSGameController only knows its own
operations
• Not states or transitions
• Separation of concerns
• Each state is a black box
• Doesn’t know details of other states
25. Motivation
• Improve the design of the GameStats
application
• Make it easier to understand at a glance
• Make it easier to make improvements