1 / 27

Ivan Marsic Rutgers University

LECTURE 17: Design Patterns Publisher-Subscriber. Ivan Marsic Rutgers University. Topics. Software Design Patterns – What & Why Example Pattern: Publisher-Subscriber ( a.k.a. Observer). What Developers Do With Software. (besides development) Understand Maintain (fix bugs)

Download Presentation

Ivan Marsic Rutgers University

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LECTURE 17: Design PatternsPublisher-Subscriber Ivan Marsic Rutgers University

  2. Topics • Software Design Patterns– What & Why • Example Pattern:Publisher-Subscriber (a.k.a. Observer)

  3. What Developers Do With Software (besides development) • Understand • Maintain (fix bugs) • Upgrade (add new features)

  4. The Power of Patterns NIB CN MAP PLE

  5. The Power of Patterns

  6. The Power of Patterns APPLE IBM CNN

  7. The Power of Patterns

  8. The Power of Patterns NIB CN MAP PLE APPLE IBM CNN

  9. Software Design Patterns • Design Patterns help anticipate change • Change is bad when there are unrelated reasons to change a software module/class • Unrelated reasons are usually because of unrelated responsibilities • Another target is complex conditional logic (If-Then-Else statements, etc.)

  10. What May Change & How • What changes in real world are business rules  customer requests changes in software • Changes in the small—changes in object responsibilities towards other objects • Number of responsibilities • Data type, or method signature • Business rules • Conditions for provision/fulfillment of responsibilities • Sometimes change is regular (follows simple rules), such as new object state defined, or another object needs to be notified about something

  11. Object Responsibilities(toward other objects) • Knowing something (memorization of data or object attributes) • Doing something on its own (computation programmed in a “method”) • Business rules for implementing business policies and procedures • Calling methods of other objects (communication by sending messages) • Calling constructor methods; this is special because the caller must know the appropriate parameters for initialization of the new object.

  12. Patterns for Tackling Responsibilities • Delegating “knowing” and associated “doing” responsibilities (State) • Delegating “calling” responsibilities (Command, Publisher-Subscriber) • Delegating non-essential “doing” responsibilities (when “doing” responsibility incrementally modified) (Decorator)

  13. Key Issues • When a pattern is needed/applicable? • How to measure if a pattern-based solution is better? • When to avoid patterns because may make things worse? •  All of the above should be answered in terms of object responsibilities before/after a pattern is applied

  14. T F ? Publisher-Subscriber Pattern • A.k.a. “Observer” • Disassociates unrelated responsibilities • Helps simplify/remove complex conditional logic and allow seamless future adding of new cases • Based on “Indirect Communication”

  15. Request- vs. Event-Based Comm. Request: doSomething( info ) Request: getInfo() (1) Request: subscribe() Client Server Info Src Doer Info Src Doer info (2) event( info ) (a) (b) (c) Direct Communication Indirect Comm.

  16. “Before” == A Scenario Suitable for Applying the Pub-Sub Pattern Event Detector DoerType1 DoerType2 … … … detectEvent() tellMeWhatToDo() … tellMeWhatToDo() … … Doer1.tellMeWhatToDo() Doer2.tellMeWhatToDo() … … … … • Responsibilities • Doing: • Detect events • Calling: • Tell Doer-1 what to do • Tell Doer-2 what to do unrelated!  unrelated reasons to change the Event Detector: • When event detection needs to change or extend • When new doer types need to be told what to do

  17. “After” == Responsibilities After Applying the Pub-Sub Pattern Publisher Subscriber subscribers : List … … detectEvent() receiveEvent() … … subscriber.receive(Event) … … … Unrelated responsibilities of the Event Detector (now Publisher) are dissociated: • When event detection needs to change or extend  change Publisher • When new doer types need to be added  add an new Subscriber type (Subscribers need not be told what to do – they know what to do when a given event occurs!

  18. Publisher-Subscriber Pattern • Focused on classifying events • The focus in on the “Publisher” object and the “environment” that it is observing • Example key events in safe home access: • Entered key is valid • Entered key is invalid • Instead of making decisions & issuing orders to Doers/Subscribers • Focusing on other objects’ work: deciding when it is appropriate to call each one

  19. Publisher-Subscriber Pattern PUBLISHER • Controller (receives key-code)Key Checker (checks validity, i.e., classifies: valid/invalid) • Publishes the classification result to “subscribers” • Lock ControlLight ControlAlarm Control • Do the work based on the event classification outcome SUBSCRIBERS ( Assumed: subscription step first )

  20. Pub-Sub Pattern (a) (b)

  21. Unlock Use Case From Chapter 2

  22. Refactoring to Publisher-Subscriber Conditional logic is decided here, at design time, instead of run time 1. Subscribe for appropriate events Subscriber Event type Pub Sub-1 keyIsValid LockCtrl, LightCtrl Sub-n keyIsInvalid AlarmCtrl itIsDarkInside LightCtrl . . . Sub-n Pub Sub-1 No need to consider the “appropriateness” of calling the “servers” Design-time decisions are better than runtime decisions, because they can be easier checked if they “work” (before the product is developed) 2. When event occurs: (a) Classify: keyIsValid / keyIsInvalid (b) Notify only the subscribers for the detected event class If a new device is added -- just write a new class; NO modification of the Publisher!

  23. Pub-Sub: Unlock Use Case

  24. From Hub-and-Spokes (Star) to Token Passing (Cascading) Architecture Before: After: Pub-Sub reduced the class coupling:

  25. Design Patterns: Delegation Custodian initializes the pattern Instantiation of the Design Pattern Client collection of objects working to provide service asks for service Alternative names for Custodian are Assembler or Initializer

  26. Pub-Sub: Initialization

  27. Practical Issues • Do not design for patterns first • Reaching any kind of solution is the priority;solution optimization should be secondary • Refactor the solution to patterns • E.g., to reduce the complexity of the program’s conditional logic • Uncritical use of patterns may yield worse solutions!

More Related