multi casting

22
A labor of love” A labor of love” (Multicast Pattern) (Multicast Pattern) Chapter 4 Chapter 4 (pages 112-131 or 123- (pages 112-131 or 123- 144) 144) Chris Gordon Chris Gordon

Upload: analeo

Post on 21-Jul-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Multi Casting

““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

Page 2: Multi Casting

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

Page 3: Multi Casting

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”

Page 4: Multi Casting

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

Page 5: Multi Casting

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

Page 6: Multi Casting

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:

Page 7: Multi Casting

Event RegistryEvent Registry

Two interfacesTwo interfaces One for eventsOne for events One for event handlersOne for event handlers

Page 8: Multi Casting

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!

Page 9: Multi Casting

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!

Page 10: Multi Casting

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!

Page 11: Multi Casting

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

Page 12: Multi Casting

StructureStructure

So here is our final structure:So here is our final structure:

Page 13: Multi Casting

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.

Page 14: Multi Casting

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?)

Page 15: Multi Casting

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

Page 16: Multi Casting

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

Page 17: Multi Casting

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”

Page 18: Multi Casting

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

Page 19: Multi Casting

MotivationMotivation

Same as multicastSame as multicast Event driven scenariosEvent driven scenarios

Page 20: Multi Casting

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.

Page 21: Multi Casting

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

Page 22: Multi Casting

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