“a labor of love” (multicast pattern) chapter 4 (pages 112-131 or 123-144) chris gordon
TRANSCRIPT
““A labor of love”A labor of love”(Multicast Pattern)(Multicast Pattern)
Chapter 4 Chapter 4
(pages 112-131 or 123-144)(pages 112-131 or 123-144)
Chris GordonChris Gordon
Multicast PatternMulticast Pattern
Described in the book as incomplete, work Described in the book as incomplete, work in progress, half baked, etcin progress, half baked, etc
What is multicast?What is multicast?
Its for event driven programmingIts for event driven programming ““The flow of control is driven by external The flow of control is driven by external
stimuli called events”stimuli called events”
Consider a vending machineConsider a vending machine
A vending machine is just a very specific A vending machine is just a very specific computer driven by various eventscomputer driven by various events
How do we code this?How do we code this?
It can get very complexIt can get very complex Which objects use the events?Which objects use the events? The answers can change even The answers can change even
dynamicallydynamically Soon becomes a nightmare to Soon becomes a nightmare to
code/maintaincode/maintain
The vending machine can be The vending machine can be represented as followsrepresented as follows
An event registry is a common solution:An event registry is a common solution:
Event RegistryEvent Registry
Two interfacesTwo interfaces One for eventsOne for events One for event handlersOne for event handlers
Problems with event registryProblems with event registry
Not type safeNot type safe Can leave hidden run-time errorsCan leave hidden run-time errors
Bad!Bad!
What should we do?What should we do?
Blame someone else?Blame someone else? Give up and cry?Give up and cry? Multicast pattern!Multicast pattern!
DetailsDetails
Multiple inheritanceMultiple inheritance Each event has an abstract classEach event has an abstract class Each handler inherits from any events it Each handler inherits from any events it
can handlecan handle Type safe!Type safe!
Details (continued)Details (continued)
How do the events get delivered?How do the events get delivered? Still need a registry, but need it type Still need a registry, but need it type
specific...specific... Register becomes exactly that, multiple Register becomes exactly that, multiple
type specific registriestype specific registries Some debate over where this goes; seems Some debate over where this goes; seems
to make the most sense in the event to make the most sense in the event classesclasses
StructureStructure
So here is our final structure:So here is our final structure:
ParticipantsParticipants MessageMessage
Encapsulates information to be transferred from Sender to ReceiverEncapsulates information to be transferred from Sender to Receiver SenderSender
Maintains a registry of Receiver objects.Maintains a registry of Receiver objects. Defines an interface for registering Receiver objects.Defines an interface for registering Receiver objects. Defines and implements an interface for delivering a Message to Defines and implements an interface for delivering a Message to
registered Receiver objects.registered Receiver objects. AbstractReceiverAbstractReceiver
Defines an interface for receiving a Message object.Defines an interface for receiving a Message object. ReceiverReceiver
Implements one or more AbstractReceiver interfaces.Implements one or more AbstractReceiver interfaces. CollaborationsCollaborations
Clients register receivers with senders through Sender's registration Clients register receivers with senders through Sender's registration interface.interface.
Senders instantiate messages and deliver them to registered receivers.Senders instantiate messages and deliver them to registered receivers.
ApplicabilityApplicability
Objects want to receive info from other Objects want to receive info from other objectsobjects
Information is complex (it varies)Information is complex (it varies) You want type safety (who doesn’t?)You want type safety (who doesn’t?)
The big questionThe big question
Is it really a type?Is it really a type? Seems to be just a special case of ObserverSeems to be just a special case of Observer Should these details just be included in that Should these details just be included in that
patternpattern Lots of questions involved in this issueLots of questions involved in this issue Is every observer really a multicast?Is every observer really a multicast? There are reasons patterns shouldn’t get too bigThere are reasons patterns shouldn’t get too big Strong/weak types play into thisStrong/weak types play into this
IntentIntent
Very similar to observerVery similar to observer In observer the concrete observers varyIn observer the concrete observers vary In multicast the events (messages) are the In multicast the events (messages) are the
important variationimportant variation But it still sounds to similarBut it still sounds to similar
Surprise! There’s a new patternSurprise! There’s a new pattern
After much argument, it was decided that After much argument, it was decided that Multicast is a refinement of observer called Multicast is a refinement of observer called “Typed Message”“Typed Message”
IntentIntent
Still very similar to observerStill very similar to observer Encapsulate information in an object to Encapsulate information in an object to
add information without compromising add information without compromising type safetytype safety
MotivationMotivation
Same as multicastSame as multicast Event driven scenariosEvent driven scenarios
Participants (same)Participants (same) Message (ProductDispensedEvent)Message (ProductDispensedEvent)
Encapsulates information to be transferred from Sender to Receiver.Encapsulates information to be transferred from Sender to Receiver. Sender (Dispenser)Sender (Dispenser)
Maintains a reference to a Receiver object.Maintains a reference to a Receiver object. Implements one or more operations that involve sending a message to Implements one or more operations that involve sending a message to
the receiver. the receiver. AbstractReceiver (ProductDispensedHandler)AbstractReceiver (ProductDispensedHandler)
Defines an interface for receiving a Message object.Defines an interface for receiving a Message object. Receiver (CoinChanger)Receiver (CoinChanger)
Implements one or more AbstractReceiver interfaces.Implements one or more AbstractReceiver interfaces. CollaborationsCollaborations
A sender instantiates a message and delivers it to its receiver.A sender instantiates a message and delivers it to its receiver. A message is passive; it does not initiate communication with senders A message is passive; it does not initiate communication with senders
or receivers.or receivers.
ApplicabilityApplicability
Objects want to receive info from other Objects want to receive info from other objectsobjects
Information is complex (it varies)Information is complex (it varies) You want type safety (who doesn’t?)You want type safety (who doesn’t?) Same as multicastSame as multicast
ConsequencesConsequences
Type safety Type safety Supports implicit invocation when Supports implicit invocation when
combined with observer ??combined with observer ?? Difficult without multiple inheritance Difficult without multiple inheritance Inheritance hierarchies get cluttered Inheritance hierarchies get cluttered
More flexible than observer More flexible than observer