Design pattern Lecture 9.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Case Studies on Design Patterns Design Refinements Examples.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
CSC 313 – Advanced Programming Topics. Design Pattern Intent  Each design pattern is a tool  Like all tools, have reason for being.
CS 210 Introduction to Design Patterns September 7 th, 2006.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Strategy Design Patterns CS 590L - Sushil Puradkar.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
Software Design Refinement Using Design Patterns
Strategy: A Behavioral Design Pattern
Where are we ? Setup Sprites Input Collision Drawing Sprites
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Chapter 5 – Design and Implementation
Chapter 7 – Object-Oriented Design
Chapter 10 Design Patterns.
CS 350 – Software Design The Strategy Pattern – Chapter 9
Behavioral Design Patterns
Observer Design Pattern
Observer Design Pattern
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Design Patterns with C# (and Food!)
Presented by Igor Ivković
DESIGN PATTERNS : Strategy Pattern
DESIGN PATTERNS : State Pattern
Strategy Design Pattern
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Introduction to Design Patterns
Advanced ProgramMING Practices
Design Patterns Lecture part 1.
OBJECT ARCHITECTURE DESIGN
Software Design Lecture : 35.
CS 325: Software Engineering
Design Patterns (Gamma, Helm, Johnson, Vlissides)
Presented by Igor Ivković
Software Design Lecture : 28.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

Design pattern Lecture 9

observer

Observer There is a lot of wild animals in some grassland in Africa, including herbivores and carnivores. Every year when entering the period from May to October, grassland is green and fragrant, providing herbivores with abundant food. Therefore, a large number of herbivores have entered the grasslands, and various carnivores also enter the grasslands along with herbivores; but when From November to April, the grassland turns yellow. Most of the plants are withered and the water source is dry. All herbivores must leave the grassland or they cannot survive. Various carnivores must also leave the grasslands. In this way, all animals depend on the state of this grassland, and green and yellow can be used to indicate different states. Here, the grasslands are observed and other animals are observers.

Observer If you develop a grassland animal observer software, you can design the grassland and each animal as classes. The class diagram is shown in Figure 1. This class diagram contains a grassland Plain and 6 animal species Hare, Antelope, Buffalo, Tiger, Lion and Fox, which contains 3 herbivores and 3 carnivores. These animals depend on the state of the grassland. If the state state is yellow, all the animals leave the grassland. If the state is green, all the animals enter the grassland. Figure 1

Observer Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and automatically. Structure

Observer Participant Observable: Observed interface, which declares three methods that should be implemented. In the simple case, the register(obs: Observer) method is responsible for registering the observers in the parameters to the Subject object, keeping a list of concrete observers in the Subject object for recording all the observers. Subject: The object that the concrete observer depends on. It implements all methods of Observable. The getState() method in the Subject can be called by ConcreteObserver to get the latest status. Observer: Observer interface, represents the dependent object. There can be more than one observer. ConcreteObserver: represents a concrete observer object.

Observer The observer pattern works as follows: The Subject maintains a data structure, such as a Java ArrayList, for recording dynamically added observers. The object interested in the state of the Subject(observer) should call the Subject 's Register method to register itself as an observer of it. Whenever the state of the Subject changes, it will notify the registered observer using the method notifyObservers(). When notified, each observer will check the state of the Subject to keep the state synchronized. According to the new state, the observer will decide to do some synchronous updates or other related operations. The observer will provide an interface that receives notification from the subject, such as synchronizeState(), which the subject can invoke in the method notify().

Observer—Applicability Use the Observer pattern when in any of the following situations: When an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently. When a change to one object requires changing others, and you do not know how many objects need to be changed. When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don’t want these objects tightly coupled.

Observer—Example Assume that there are two groups of animals: tigers and antelopes. Both groups of animals are observing the grassy state of a prairie. When the grass turns green, the antelope group and the tiger group will all come to the grassland; when the grassland turns yellow, both the antelope group and the tiger group will leave. Design a simple program with a graphical interface to simulate the above scenario. According to the structure of the observer pattern, the design is shown in the figure below.

Observer Simulate boiler temperature display. The observerd represents the boiler with TemperatureGUI. In order to display the temperature values in different ways, several observers were designed: the Celsius graphic display interface—CelsiusGUI, the FahrenheitGUI object of the Fahrenheit temperature display interface, and the KelvinGUI object of the Kevin temperature display interface to observe the observed TemperatureGUI object. When the user enters Celsius, Fahrenheit or Kevin temperature in the TemperatureGUI graphical interface, accordingly, according to the temperature conversion, several temperature observer interfaces will display Celsius, Fahrenheit, or Kevin temperature, respectively, and also display the temperature color, That is, different colors indicate different temperatures. The design is shown in Figure2 .

Observer Figure 2 In this program, the setChanged() and notifyObservers(bTem) methods are used to notify observers of changes in temperature. The parameter bTem represents the temperature value entered by the user. This parameter passes the temperature value to the observer's update method's second parameter. After reading this value in the observer object, the current user input temperature value can be obtained.

Strategy

Strategy One of the basic principles that object-oriented programming follows is separation of duties, and the other is high cohesion and low coupling. Let‘s first observe an example. This is a sort of Java program. This sorter contains 4 different sort methods. Each method will return an ordered sequence of integers. First design the program as a independent class. The design includes both the main method and some sorting methods. Advantages: The entire program is designed as a independent class, making it easier to write code; Disadvantages: There is no separation of duties and there are also problems with scalability and maintainability.

Strategy In order to overcome the above-mentioned disadvantages, the above design class is split into two classes. This design strips the customer class Sorting with the main method from the sorting SortingAlgorithms. Therefore, the separation of duties was achieved. All the sorting algorithms in the design are encapsulated in a class. These methods all accomplish the same task- sorting an integer sequence, so the design has a high cohesive nature. Advantages: When you need to modify the methods in SortingAlgorithms, you do not have to make any changes to the customer class Sorting. Problem: When a new algorithm is added to the SortingAlgorithms or when an algorithm is modified, the entire SortingAlgorithms class needs to be recompiled.

Strategy Solution: Split class SortingAlgorithms further, encapsulating each sorting algorithm into a single class, that is, splitting a class into several classes, each encapsulating an algorithm separately. Advantages: Modifying an algorithm only requires recompiling the class involved in the algorithm without having to recompile other classes. If you want to add a new algorithm, you only need to add a new class that encapsulates the algorithm in the collection of subclasses. The design is universal, and it can be attributed to this type of pattern— the strategy pattern.

Strategy Intent Define a family of algorithm, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Structure

Strategy Participants Strategy Declare an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy. ConcreteStrategy Implements the algorithm using the Strategy interface. Context Is configure with a ConcreteStrategy object. Maintains a reference to a Strategy object May define an interface that lets Strategy access its data

Strategy —Applicability Use the Strategy pattern when Many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors. You need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade- offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms. An algorithm uses data that clients shouldn’t know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures. A class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.

Strategy Advantage Disadvantage Obtain a series of reusable algorithms, these algorithms inherit a common abstract class, so the common functions can be put into the super class; Encapsulate different algorithms in different strategy subclasses to make the logic clearer and each algorithm can change independently; It is easier to change or extend functions. Modifying an algorithm does not require recompiling “Client” and “Context”. Disadvantage Clients must be aware of different Strategies. The pattern has a potential drawback in that a client must understand how Strategies differ before it can select the appropriate one. Clients might be exposed to implementation issues. Therefore you should use the Strategy pattern only when the variation in behavior is relevant to clients.

Strategy—Example Chinese Zodiac query system. Use the strategy pattern to design this example. Encapsulate each of the twelve Zodiac as a class and design a common interface (ChineseZodiac) for these classes. Users enter their date of birth using the user's graphical interface, and when creating a Context object, the date is passed as a parameter to the Context class. The Context class performs calculations based on user input, derives the initial Zodiac class, and is responsible for creating the Zodiac object. The Context class uses this object to call the subclass's say() function to introduce the features of the Zodiac.

Strategy Chinese Zodiac query system designed by strategy pattern