Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 18 The Observer Pattern Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.

Similar presentations


Presentation on theme: "Chapter 18 The Observer Pattern Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh."— Presentation transcript:

1 Chapter 18 The Observer Pattern Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2 2Ku-Yaw ChangPattern-Oriented Design Outline Overview Categories of Patterns More Requirements for the International E-Commerce Case Study The Observer Pattern Applying the Observer to the Case Study Field Notes: Using the Observer Pattern Summary

3 3Ku-Yaw ChangPattern-Oriented Design Overview Introduce the categorization scheme of patterns Introduce the Observer pattern by discussing additional requirements for the case study Apply the Observer pattern to the case study Describe the Observer pattern Describe the key features of the Observer pattern Describe some experiences using the Observer pattern in practice

4 4Ku-Yaw ChangPattern-Oriented Design Outline Overview Categories of Patterns More Requirements for the International E-Commerce Case Study The Observer Pattern Applying the Observer to the Case Study Field Notes: Using the Observer Pattern Summary

5 5Ku-Yaw ChangPattern-Oriented Design Categories of Patterns

6 6Ku-Yaw ChangPattern-Oriented Design Categories of Patterns Fourth category (by the author) Decoupling patterns Decoupling patterns Almost a subset of the behavioral category

7 7Ku-Yaw ChangPattern-Oriented Design Outline Overview Categories of Patterns More Requirements for the International E-Commerce Case Study The Observer Pattern Applying the Observer to the Case Study Field Notes: Using the Observer Pattern Summary

8 8Ku-Yaw ChangPattern-Oriented Design International E-Commerce Case Study New requirement: take actions for new customers Send a welcome e-mail to the customer Send a welcome e-mail to the customer Verify the customer’s address with the post office Verify the customer’s address with the post office

9 9Ku-Yaw ChangPattern-Oriented Design International E-Commerce Case Study Hard code the notification behavior into the Customer class

10 10Ku-Yaw ChangPattern-Oriented Design International E-Commerce Case Study The problem Requirements always change Requirements always change Support different companies’ welcome letter

11 11Ku-Yaw ChangPattern-Oriented Design Outline Overview Categories of Patterns More Requirements for the International E-Commerce Case Study The Observer Pattern Applying the Observer to the Case Study Field Notes: Using the Observer Pattern Summary

12 12Ku-Yaw ChangPattern-Oriented Design The Observer Pattern Intent (by GoF) Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Handle notification automatically Decouple the notifyers and the notifyees Decouple the notifyers and the notifyees

13 13Ku-Yaw ChangPattern-Oriented Design The Observer Pattern Also called Dependents Dependents Publisher-Subscriber Publisher-SubscriberJava The Observer interface The Observer interface The Observable class The Observable class

14 14Ku-Yaw ChangPattern-Oriented Design Generic Structure

15 15Ku-Yaw ChangPattern-Oriented Design Outline Overview Categories of Patterns More Requirements for the International E-Commerce Case Study The Observer Pattern Applying the Observer to the Case Study Field Notes: Using the Observer Pattern Summary

16 16Ku-Yaw ChangPattern-Oriented Design Applying the Observer to the Case Study Two things are varying Different kinds of objects Different kinds of objects A list of objects that need to be notified Different interfaces Different interfaces Different classes tend to have different interfaces

17 17Ku-Yaw ChangPattern-Oriented Design Applying the Observer to the Case Study Step 1 Make the Observers behave in the same way Make the Observers behave in the same way Have all the observers be of the same type C++ Single or multiple inheritance Single or multiple inheritance Step 2 Have the observers register themselves Have the observers register themselves Add two methods to the subject attach(Observer) : add the given observer to its list attach(Observer) : add the given observer to its list detach(Observer) : remove the given observer from its list detach(Observer) : remove the given observer from its list

18 18Ku-Yaw ChangPattern-Oriented Design Applying the Observer to the Case Study Step 3 Notify the observers when the event occurs Notify the observers when the event occurs Each Observer implement a method called update The Subject implements a notify method Go through its list of Observers and calls this update method for each observer Go through its list of Observers and calls this update method for each observer Step 4 Get the information from the subject Get the information from the subject Pull vs. push models

19 19Ku-Yaw ChangPattern-Oriented Design Implementing Customer with Observer

20 20Ku-Yaw ChangPattern-Oriented Design Outline Overview Categories of Patterns More Requirements for the International E-Commerce Case Study The Observer Pattern Applying the Observer to the Case Study Field Notes: Using the Observer Pattern Summary

21 21Ku-Yaw ChangPattern-Oriented Design Field Notes: Using the Observer Pattern Not for all dependencies Add complexity when the dependencies are fixed Add complexity when the dependencies are fixed For changing or dynamic dependencies

22 22Ku-Yaw ChangPattern-Oriented Design Outline Overview Categories of Patterns More Requirements for the International E-Commerce Case Study The Observer Pattern Applying the Observer to the Case Study Field Notes: Using the Observer Pattern Summary

23 23Ku-Yaw ChangPattern-Oriented Design Summary All observers be responsible for registering themselves with the subject

24 The End


Download ppt "Chapter 18 The Observer Pattern Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh."

Similar presentations


Ads by Google