Design Patterns, cont..

Slides:



Advertisements
Similar presentations
Identity and Equality Based on material by Michael Ernst, University of Washington.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Events, Listeners, and Callbacks (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
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.
Cohesion and Coupling CS 4311
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns Introduction
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 Events, Listeners, and Callbacks (slides by Mike Ernst and David Notkin) 1.
The Observer Pattern.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Slide design: Dr. Mark L. Hornick
Module dependences and decoupling CSE 331 University of Washington.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns (continued). Design Patterns So Far Creational patterns: Factories, Prototype, Singleton, Interning Problem: constructors in Java (and.
Observer Pattern Context:
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Software Design Refinement Using Design Patterns
Abstract Factory Pattern
Module Road Map Refactoring Why Refactoring? Examples
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Observer Design Pattern
Final review CSE331 Winter 2017.
Instructor: Dr. Hany H. Ammar
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Design Patterns - A few examples
CSE 331 Software Design and Implementation
CSE 331 Software Design and Implementation
Multiuser Protection and the Mediator Pattern
Design Patterns, conclusion
Software Engineering Lecture 7 - Design Patterns
Design Patterns A Case Study: Designing a Document Editor
Introduction to Behavioral Patterns (1)
CSE 331 Software Design and Implementation
Subtype Polymorphism, Subtyping vs
CSE 331 Software Design & Implementation
Structural Patterns: Adapter and Bridge
Advanced ProgramMING Practices
8. Observer Pattern SE2811 Software Component Design
Week 6, Class 2: Observer Pattern
Advanced ProgramMING Practices
CSE 331 Software Design & Implementation
Software Design Lecture : 40.
Presentation transcript:

Design Patterns, cont.

Design Patterns So Far Creational patterns: Factories, Prototype, Singleton, Interning Problem: constructors (in Java and other OO languages) are inflexible 1. Can’t return a subtype of the type they belong to. “Factory” patterns address the issue: Factory method (e.g. createBicycle()), Factory class/object, Prototype 2. Always return a fresh new object, can’t reuse. “Sharing” patterns address the issue: Singleton, Interning Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Design Patterns So Far Wrappers: Adapter, Decorator, Proxy Composite Structural patterns: when we want to change interface or functionality of an existing class, or restrict access to an object Composite A structural pattern: expresses whole-part structures, gives uniform interface to client Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Outline Behavioral patterns Dependences and coupling Observer Façade Dependences and coupling Behavioral patterns for traversing composites Interpreter Procedural Visitor

Observer Pattern Question: how to handle an object (model), which has many “observers” (views) that need to be notified and updated when the object changes state For example, an interface toolkit with various presentation formats (spreadsheet, bar chart, pie chart). When application data, e.g., stocks data (model) changes, all presentations (views) should change accordingly Spring 18 CSCI 2600, K. Kuzmin, A Milanova

A Naïve Design Client stores information in Data Then Data updates the views accordingly Problem: to add a view, or change a view, we must change Data. Better to insulate Data from changes to Views! Client Remember, this is a central issue in design. Where Design patterns help. We would like to build systems that are open for extension (easy to change, add Views), but close to modification (we want to insulate Data from the change). SpreadsheetView Data BarChartView

A Better Design Data class has minimal interaction with Views Only needs to update Views when it changes Old, naive design: Better design: class Data { … void updateViews() { spreadSheet.update(newData); barChart.update(newData); // Edit this method when // different views are added. // Bad! } class Data { List<Observer> observers; void notifyObservers() { for (obs : observers) obs.update(newData); } interface Observer { void update(…); Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on slide by Michael Ernst)

Class Diagram 1 * Data attach(Observer) detach(Observer) notifyObservers() Observer update(...) observers for (Observer obs : observers) obs.update(…) What can be a problem? SpreadsheedView update(…) BarChartView update(…) Client is responsible for creation: data = new Data(); data.attach(new BarChartView()); Data keeps list of Views, notifies them when change. Data is minimally connected to Views!

Even Better for (Observer obs : observers) obs.update(…) 1 * Observable attach(Observer) detach(Observer) notifyObservers(…) Observer update(...) observers for (Observer obs : observers) obs.update(…) What can be a problem? SpreadsheedView update(…) BarChartView update(…) Data … … // when a notable change occurs // calls notifyObservers(…)

Update Model Question: How does the object (Data in our case) know what info each observer (View) needs? Push model: Object sends the info to Observers Pull model: Object does not send info directly. It gives access to itself to the Observers and lets each Observer extract the data they need Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Observer Pattern 1 * Data attach(Observer) detach(Observer) notifyObservers(…) getStockPrice(); Observer update(Data) observers What can be a problem? for (Observer obs : observers) obs.update(this) SpreadsheedView update(Data) BarChartView update(Data) Pull model: observers have access to Data, they can pull the info they need.

Example of Observer THE MODEL From JDK public class SaleItem extends Observable { private String name; private float price; public SaleItem(String name, float price) { this.name = name; this.price = price; } public void setName(String name) { setChanged(); notifyObservers(name); public void setPrice(float price) { // analogous to setName From JDK. Marks that object has changed. From JDK. If object has changed, calls obs.update(this,name). THE MODEL

An Observer of Name Changes public class NameObserver implements Observer { private String name; public void update(Observable obj, Object arg) { if (arg instanceof String) { name = (String)arg; System.out.println("NameObserver: Name changed to " + name); } else System.out.println("NameObserver: Some other change to observable!"); From JDK update(Observable obj, Object arg) allows for both Push and Pull models. Update is called from notifyObservers, which is a library method! notifyObservers() calls update(this,null) and notifyObservers(arg) calls update(this,arg). An Observer (such as NameObserver) can choose to use the first argument of update, the Observable obj, cast it to the appropriate type and extract the info it needs (The Pull model) or it can choose to ignore Observable obj, and use the argument Object arg, which the data sends (the Push model). THE VIEW Implements update from JDK. Results in callback! Spring 18 CSCI 2600, K. Kuzmin, A Milanova

An Observer of Price Changes public class PriceObserver implements Observer { private Float price; public void update(Observable obj, Object arg) { if (arg instanceof Float) { price = (Float)arg; System.out.println(”PriceObserver: Price changed to " + price); } else System.out.println(”PriceObserver: Some other change to observable!"); We don’t care for this now. ANOTHER VIEW

The Client SaleItem si = new SaleItem("Corn Pops", 1.29f); NameObserver nameObs = new NameObserver(); PriceObserver priceObs = new PriceObserver(); // Now add observers si.addObserver(nameObs); si.addObserver(priceObs); // Make changes to the Subject. si.setName("Frosted Flakes"); si.setPrice(4.57f); si.setPrice(9.22f); si.setName("Sugar Crispies"); JDK. Since s is Observable! What happens here? Is this the push model or the pull model? This is the push model: SaleItem pushes the data observers need to know (name String or price Float). Observers don’t know SaleItem! THE CONTROLLER

Another Example An application that computes a path on a map and displays the path. When user requests different path, display changes Initially, application displays using a simple text-based UI Therefore, a text-based View (i.e., Observer) Later, application will display using a GUI interface A GUI-based View (another Observer) Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Another Example of Observer // Represents sign-up sheet of students public class SignupSheet extends Observable { private List<String> students = new ArrayList<String>(); public void addStrudent(String student) { students.add(student); notifyObservers(); } public int size() { return students.size(); Where is the bug? THE MODEL Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example due to Michael Ernst)

Example of Observer The SignupSheet observable was sent when notifyObservers called update(this,…) public class SignupObserver extends Observer { // called from notifyObservers, which // was called when SignupSheet changed public void update(Observable o, Object arg) { System.out.println(“Signup count: “ + ((SignupSheet)o).size()); } We don’t care for arg now. THE VIEW Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example due to Michael Ernst)

The Client SignupSheet s = new SignupSheet(); s.addStudent(“Ana”); // nothing visible happens. Why? s.addObserver(new SignupObserver()); s.addStudent(“Katarina”); // what happens now? What model’s used here? Push model or pull model? Pull model because the Observer needs to cast into the Observable type then call its methods. THE CONTROLLER Spring 18 CSCI 2600, K. Kuzmin, A Milanova (example due to Michael Ernst)

Where is the observable? Where is the observer? { propertyListeners.add(lis); } Sale addPropertyListener(PropertyListener lis) publishPropertyEvent(name,value) setTotal(Money newTotal) for each pl in propertyListeners pl.onPropertyEvent (this,name,value); notify() propertyListeners * <<interface>> PropertyListener onPropertyEvent(source, name, value) update() { if (name.equals(“sale.total”)) saleTextField. setText(value.toString()) } SaleTotalFrame onPropertyEvent(source, name,value) initialize(Sale sale) { sale.addPropertyListener(this); }

Model-view Principle Observer pattern known as Model-view or Model-view-controller “Model” objects (e.g., Sale, SignupSheet) should not know about concrete “view” objects (e.g., SaleTotalFrame, SignupObserver) Domain layer should be minimally connected with presentation layer Open/closed principle: if user decides to change/upgrade interface, the change shall trigger no modification to domain layer

Façade Pattern Question: how to handle the case, when we need a subset of the functionality of a powerful, extensive and complex library Example: We want to perform secure file copies to a server. There is a powerful and complex general purpose security library. What is the best way to interact with this library? Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Façade Pattern SecureCopy Build a Façade to the library, to hide its (mostly irrelevant) complexity. SecureCopy is the Façade. SecureCopy

Façade Pattern Façade reduces interactions between client and the complex library Façade hides (mostly irrelevant) complexity of the library If library changes, we’ll only need to change the Façade, the client remains insulated Open/closed principle: when change happens, the change has minimal impact Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Outline Behavioral patterns Dependences and coupling Observer Façade Dependences and coupling Behavioral patterns for traversing composites Interpreter Procedural Visitor

Interactions Between Modules Interactions between modules (in our designs, module = class) cause complexity To simplify, split design into classes that don’t interact much Coupling is the amount of interaction among classes Roughly, if class A calls methods/uses fields of class B, then there is coupling from A to B In design, we strive towards low (weak) coupling, i.e., minimal, necessary interactions Spring 18 CSCI 2600, K. Kuzmin, A Milanova (based on slide by Michael Ernst)

Low Coupling A’ A B’ C’ B C Arrow means dependency. Here, A depends on C. A’ Bad design: classes Better design: strongly coupled weakly coupled A B’ C’ B C Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Coupling is the Path to the Dark Side Coupling leads to complexity Complexity leads to confusion Confusion leads to suffering If once you start down the dark path, forever will it dominate your destiny, consume you it will Spring 18 CSCI 2600, K. Kuzmin, A Milanova (slide due to Mike Ernst)

Observer promotes low coupling Bad. Data does not need depend on Views Better: Weaken dependency of Data on Views Introduce a weaker spec in the form of interface Client SpreadsheetView Data BarChartView Observer Data SpreadsheetView BarChartView Spring 18 CSCI 2600, K. Kuzmin, A Milanova

Façade promotes low coupling Façade weakens the dependency between Client and library. Introduce Façade object: reduce #dependences from 3*5 to 3+5! SecureCopy