6/26/20151. Copyright W. Howden2 Bridge, Chain of Responsibility, Mediator, Visitor, Memento, Strategy CSE 111 6/26/2015.

Slides:



Advertisements
Similar presentations
Testing Object Oriented Programs CSE 111 4/28/20151.
Advertisements

Copyright W. Howden1 Singleton, Wrapper, and Facade Patterns CSE 111.
Template, Command, Iterator and Composite CSE 111 5/9/20151Copyright W. Howden.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Copyright W. Howden1 Lecture 7: Functional and OO Design Descriptions.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Copyright W. Howden1 Lecture 6: Design Evaluation and Intro to OO Design Patterns.
Copyright W. Howden1 Lecture 11: UML Terminology and Additional Models and Notation.
Copyright W. Howden1 Lecture 13: Programming by Contract.
Design Patterns CS is not simply about programming
© 2005 Prentice Hall8-1 Stumpf and Teague Object-Oriented Systems Analysis and Design with UML.
Copyright W. Howden1 Lecture 14: Callbacks, Singletons and Wrappers.
Copyright W. Howden1 Lecture 17: Patterns Potpourri.
Copyright W. Howden1 Lecture 5: Collaboration Diagrams.
Lecture 5b: Basic Design Patterns CSE 111. Basic Design Patterns We know our subsystem interface classes and (some of) their methods We need to create.
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
1 Lecture 2: Elaboration Tasks and Domain Modeling.
Copyright W. Howden1 Lecture 15: Generalization, Polymorphism and States.
Copyright W. Howden1 Lecture 6: Collaboration Diagrams.
Copyright W. Howden1 Lecture 2: Elaboration Tasks and Domain Modeling.
Lecture a: Additional UML Models: Package, Activity, Deployment Lecture b: Generalization, Aggregation and Additional Domain Model Notation Copyright W.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Copyright W. Howden1 State Models and the State Pattern.
Lecture 7: UML Class Diagrams CSE 111 7/15/20151Copyright W. Howden.
More OOP Design Patterns
Chapter 25 More Design Patterns.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Systems Analysis and Design in a Changing World, Fifth Edition
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
12 Systems Analysis and Design in a Changing World, Fifth Edition.
1 ITEC 3010 “Systems Analysis and Design, I” LECTURE 10: Use Case Realizations [Prof. Peter Khaiter]
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Lexi case study (Part 2) Presentation by Matt Deckard.
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.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
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.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Sections Inheritance and Abstract Classes
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Low Budget Productions, LLC
Conception OBJET GRASP Patterns
Introduction to Design Patterns
Behavioral Design Patterns
Observer Design Pattern
Design Patterns with C# (and Food!)
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Behavioral and Structural Patterns
Design Patterns Part 2: Factory, Builder, & Memento
Object-Oriented PHP (1)
Presentation transcript:

6/26/20151

Copyright W. Howden2 Bridge, Chain of Responsibility, Mediator, Visitor, Memento, Strategy CSE 111 6/26/2015

Bridge Pattern Context and Problem Context –An abstract class A is subclassed with alternative implementations that define its abstract methods. (Call this generalization hierarchy H1) Problem –want to be able to vary the “front end” A without changing its implementations Copyright W. Howden36/26/2015

Bridge Pattern Solution New Implementation Hierarchy –abstract class I with abstract methods that are defined in subclasses that correspond to the implementation variations Front End Hierarchy –define a new class A which uses I and defines the desired front end variations 6/26/2015Copyright W. Howden4

Old Class Diagram 6/26/2015Copyright W. Howden5

Comments The original class FrontEnd has methods which are implemented using certain features that can have alternatives Those different alternatives are modeled using subclasses that contain the different alternatives 6/26/2015Copyright W. Howden6

New Class Diagram 6/26/2015Copyright W. Howden7

Comments The new FrontEnd class is abstract because it has an abstract class variable implementation, whose subtypes values correspond to differing implementations As far as the implementation differences are concerned, they are taken care of by the implementation subtype, so the methods in FrontEnd do not have to be abstract The subtypes of FrontEnd now correspond to front end differences, corresponding to additional methods. The new front ends may extend some or all of the supertype front end methods 6/26/2015Copyright W. Howden8

Sally, Pierre and Aiko Sally has a new career with a TV cooking show, where you see her whipping up her favorite dishes The show will done in different venues, with available cooking techniques. e.g. openFire, solar, electric, gas. So Sally has to be able to know how to “switch gears” and cook in several different ways Copyright W. Howden96/26/2015

Foreign Interfaces When Sally goes to different countries it is better for a local host to be used Sally’s way of cooking, with the different techniques, is still valid, but the “presentation” has to vary So in France, Pierre will the cook and Japan it will be Aiko 6/26/2015Copyright W. Howden10

Foreign Interfaces, cont Pierre and Aiko will employ all of Sally’s capabilities, which will include techniques for each of the different possible ways of cooking They may also add other local flair capabilities or alter something that Sally used to do 6/26/2015Copyright W. Howden11

Sally, Pierre and Aiko Comments Sally originally has alternative subclasses for each of the cooking methods A new version of Sally uses different cooking techniques by calling on methods in an instance of a cooking techniques subclass, whose subtype variations correspond to different techniques –the instance is passed in via the Sally constructor Pierre and Aiko subclass Sally which allows them all her cooking capabilities, to which they can make local flavor additions and alterations –When they are created with an instance of the cooking techniques subclass, they inherit common methods from Sally 6/26/2015Copyright W. Howden12

DS Example Original Version 1: Suppose we had designed our DS so that we had an abstract GUI with abstract methods for calls on the DL. For a particular implementation of the DL, we subclass the GUI to get a concrete GUI where the abstract methods are replaced by concrete method calls on the chosen implementation methods of the DL Allows us to vary the implementations of the requests made via the GUI Copyright W. Howden136/26/2015

DS Example with Bridge DL variations –Abstract DL interface with abstract methods. Subclassed with a concrete DL’s, for different implementations of DL actions. GUI calls methods in the abstract DL. When an instance of GUI is created, an instance of a concrete DL subclass is handed –GUI can be subclassed with variations that have the same DL interface, but present different GUI behavior Copyright W. Howden146/26/2015

Real Uses of Bridge Graphics objects such as in Window have a WindowPeer component that is constructed to work for a particular graphics platform Instead of subclassing the user interface classes with peer classes, instances of the user interface (sub) classes are instantiated with an instance of a particular peer subclass Copyright W. Howden156/26/2015

Chain of Responsibility, Mediator Chain of Responsibility –context: tasks to be done, multiple agents –problem: how to parcel out –solution: linear ordering of agents, each does it or passes it on to the next (what if no one?) Mediator –context: multiple agents who need to interact –problem: hard to keep track of who knows what –solution: central mediator who handles all communications, and knows agents’ current status Copyright W. Howden166/26/2015

Pamela/Fred/Jane/Sally/Henry– Chain of Responsibility Start up a personal services corp. They all receive broadcast with requests –start shouting at each other across the room to see who is going to do it Chain of responsibility solution –Request comes to Pamela who does or hands it to Fred. Fred does it or hands it to Jane.... Each may do part of the task or all of it. Copyright W. Howden176/26/2015

Pamela/Fred/Jane/Sally/Henry– Mediator They appoint Henry as the office manager –Henry knows their schedules and capabilities Requests come into Henry, who looks at schedules and asks relevant, ideal agents if they can take care of all or part of the task –Mediator reminiscent of the Controller pattern, with use of system controller Copyright W. Howden186/26/2015

Visitor Pattern Context –a collection of objects, each of which contains information in possibly different forms, from which we want to collect various kinds of information Problem –for each new kind of information we have to open up the objects and add new data gathering methods –we do not want the report maker to have a giant switch with (# of kinds of objects) x (# of kinds of reports) branches Copyright W. Howden196/26/2015

Solution Two abstract hierarchies –Report interface a subclass for each kind of report that we want to produce these are data gathering objects –Information object interface a subclass for each kind of information object from which we will gather information Report subclasses each report subclass has a visit() method for each information object subclass instances of report subclasses are called “visitors” Copyright W. Howden206/26/2015

Solution (cont’) Information subclasses each information subclass has an accept() method that takes a report subclass object x (the “visitor”) that collects the info Traverser for any kind of desired report, create a visitor/instance x, and traverse the information subclass objects. For each information object y, perform y.accept(x). This will cause x.visit(this) to be called Copyright W. Howden216/26/2015

Report Class Structure Copyright W. Howden226/26/2015

Information Class Structure Copyright W. Howden236/26/2015

Solution (cont’) Overloaded visit() methods in Report subclasses? –every subclass has a set of methods all called visit(), all with different definitions –Disambiguation how do you know which visit() method it is when you perform x.visit()? –use class and parameter disambiguation Copyright W. Howden246/26/2015

Disambiguation Class Disambiguation –if we see x.visit(), we look in the class of x to find definitions for this visit() method Parameter Disambiguation –if we see x.visit(y), we look in the visit() methods for class x to find the one which has a parameter of the type of y 6/26/2015Copyright W. Howden25

Observations To add new report types add a new reports subclass with new information gathering visit() methods for each kind of information object To add new information objects add a new information subclass add a new reporting visit() method for the new kind of information to each report subtype Copyright W. Howden266/26/2015

Pamela, Fred, Jane, Sally Henry and the Commune They form a commune where they pool their resources Once a year they collect certain kinds of information –total money assets –total money debts Copyright W. Howden276/26/2015

Commune and Personality Types Fred is suspicious of corporate America, Sally is lax, and Jane and Henry are prudent –Fred keeps his money under his mattress and only borrows privately with IOU’s –Sally just keeps her money in her purse and sponges off her parents –Jane and Henry keep their money in a Canadian Bank and have no debts Copyright W. Howden286/26/2015

Report Subclasses TotalAssets subclass –methods: visit(x Suspicious), visit(x Lax) visit(x Prudent) –e.g. a Suspicious person passed a TotalAssets object will call its visit(this) method, which will add the money under the person’s mattress to the total assets TotalDebts subclass –methods: visit(x Suspicious), visit(x Lax) visit(x Prudent) –e.g. a Suspicious person passed a TotalDebts object will call its visit(this) method, which will add in the IOUs to the total debt Copyright W. Howden296/26/2015

Information Subclasses Suspicious people –accept() method will call the visit(this) in the visitor, where “this” will be of type Suspicious Lax people –accept() method will call the visit(this) method in the visitor, where “this” will be of type Lax Prudent people –accept() method will call the visit(this) method in the visitor, where “visit” will be of type Prudent Copyright W. Howden306/26/2015

Commune Report Generation To generate total money assets report –We get Henry to carry an instance, or visitor, of TotalAssets to Fred, Jane and Sally. When they accept the visitor, they execute its visit(this) method. Fred will hence automatically execute the Suspicious version of visit, Sally the Lax one, and Jane the Prudent Copyright W. Howden316/26/2015

Commune Report Methods Since the report is of type TotalAssets, the suspicious version will count money under the mattress for the accepting information object The lax version will count money in information object’s purse The prudent version will count the money in the persons Canadian bank account Copyright W. Howden326/26/2015

Commune Final Report Henry then returns the TotalAssets report, which in this case has been designed to keep the totaled the amounts inside itself. To get a debt summary, Henry would carry around an instance of TotalDebts, and a similar process would occur. Copyright W. Howden336/26/2015

DS Example Dating system returns a free dinner voucher for each date The contracted restaurants submit a visit summary for each pair of visiting daters We have a data base of summaries Restaurants may submit differently formatted summaries, or even summaries with different information Copyright W. Howden346/26/2015

DS Example We want to create different reports from the collection of summaries Create a report interface, with subclasses for each kind of report Each instance of a report subclass has an information gathering visit() method for each kind of restaurant summary For a collection of summaries, we visit each one. The accept method of a summary will execute the visit() method in the visitor Copyright W. Howden356/26/2015

Other Alternatives to Commune Visitor Solution? Assume the information we want is total assets –Functional –information objects have an x.assets() method –iterate over the information object collection, applying a totaling program t(partialSum, x) to each object x –Object animation –iterate over the information object collection, for each information object x perform x.addIn(partialSum), where x.addIn adds in its assets amount to partial Sum 6/26/2015Copyright W. Howden36

Copyright W. Howden37 Functional Approach t() has a big switch statement with an entry for each of the different versions of information objects, because info differently formatted Different programs for different kinds of reports To add a new type of report, create a new program To add a new type of information object, add to the switch statements of each of the different reporting functions (ouch!) 6/26/2015

Copyright W. Howden38 Object Animation A program would iterate over the information objects, executing their.addIn() method To add a new report type –have to change the code in the information subclasses for each new kind of report (addIn may not be adequate for new kind of gathered information) –(ouch!) To add a new kind of information object –new subclass of information objects 6/26/2015

Assessment For functional approach, adding a new type of information object is a mess For object-animation approach, adding a new kind of report may be a mess –also strongly couples information objects with report types Visitor Pattern avoids these pitfalls 6/26/2015Copyright W. Howden39

Memento Pattern Context saving and restoring the state of an object Problem do not want to expose the objects innards to the outside world Solution object has a getState() and restoreState() which uses objects of class Memento. Done in a way so that client does not have access to contents. 6/26/2015Copyright W. Howden40

Copyright W. Howden41 Interfaces and Classes Strategy: object that might need to be rolled back supports a Rollbackee interface –has the getState() and restoreState() method object that recognizes that a rollbackee object may need to be rolled back is a Rollbacker subclass –gets the data structure used to save Rollbackee objects a Rollbacker sends a getState message to a Rollbackee object, which returns a Memento object that is saved by the Rollbacker Rollbacker sends a setState message to rollbackee with a Memento from which state can be restored 6/26/2015

Copyright W. Howden426/26/2015

Fred, Sally and Memento Sally is a CSE student with 2 difficult exams on one day. She can cram the day before, but when she takes exam 1 she will forget the material for exam 2 She gets Fred (who is on holiday from manufacturing souvenirs) to help her, using the Memento pattern 6/26/2015Copyright W. Howden43

Using the Mementos When Sally finishes studying for Exam 2, Fred sends her a getState() message, and saves the returned memento After Sally finishes exam 1, Fred sends a restoreState(m) message with the saved memento m. Sally’s method restores her state and she is ready for Exam 2. 6/26/2015Copyright W. Howden44

DS Memento Example 1 Recall that DL creates a DateRequest object in response to a request for a date Suppose that users can repeatedly ask for dates A single DR object is created for a session which keeps a history of responses Implement an undo facility wherein DL saves a state memento for each use of a DR 6/26/2015Copyright W. Howden45

DS Memento Example 2 DateRequest is the Rollbackee dL, the Domain Logic subsystems interface is the Rollbacker Could extend rollbacking to all messages, not just getDate 6/26/2015Copyright W. Howden46

Memento vs Command 1 Memento: needs to get state and restore it by direct access with object where state exists. Suppose you want to save state just before doing an AddMember request –Would dL need to save the whole DataBase? Command: command knows what part of state it should save. Can selectively use methods of target object to restore 6/26/2015Copyright W. Howden47

Memento vs Command 2 If we use the Command Pattern, the dL will receive different kinds of command objects which it will execute The command object can save, inside itself, necessary information for an undo dL will stack commands e.g. for AddMember, all it needs to remember is who was added, so if undo’s back us up to this command its undo() will be straightforward. 6/26/2015Copyright W. Howden48

Memento Useful? When is it better than a Command stack? –Not directly linked to state changing requests/commands, so can respond to a state change request at any time user demanded checkpointing periodic checkpointing 6/26/2015Copyright W. Howden49

Strategy Pattern Context alternative algorithms, procedures for a task Problem allow best choice of algorithms at run/initialization Solution different algorithms are instances of subclasses of the same class A. Program constructor has a parameter of type A used to pass the algorithm instance 6/26/2015Copyright W. Howden50

Copyright W. Howden51 Alternative Approaches? Functional: big switch that chooses an algorithm No, new choices will require recoding Factory pattern: instantiate abstract create() with concrete create for chosen algorithm No, this is subclassing at program creation time 6/26/2015

Copyright W. Howden52 Class Relationships 6/26/2015

Sally, Jane and Fred Fred, Sally, Jane, and Pamela have been quite successful in the their Souvenirs business and start to have fancy parties. The parties are pretty much the same and include a late night supper. They hire a chef named Henri to do the supper The each have a favorite recipe, so for a given party, an instance of Henry is created with the choice for the occasion 6/26/2015Copyright W. Howden53

DS Example We have different date matching algorithms, which we like to experiment with. We model them as concrete subclasses of an abstract DateRequest class At start up time, we create an instance of a subclass which is passed as a parameter into Start, and then into the constructor for DL who sets its dR class variable to the object. 6/26/2015Copyright W. Howden54

Copyright W. Howden55 Java Strategy Example Programmers can choose between different layout managers for a frame E.g. for the Message Dialog object, we used buttonPanel.setLayout(new FlowLayout()); Other possible layout managers: –Border, Grid, Card, etc. 6/26/2015

The Patterns Gang Bridge: International Cooking Show Alternative fronts and real cook implementers Chain: Personal Services Corp Pamela to Fred, Fred to Jane,.... Mediator: Personal Services Corp Office mediator Henry, also the controller Visitor: Inventory at the Commune, different kinds Memento: Fred resets Sally’s brain for the exam Strategy: Fancy party: giving the supper Menu to Henri 6/26/2015Copyright W. Howden56

Assignment Consider how the patterns in this lecture might be used in your project. Either adopt them or discuss how they could be used and what the pros and cons of those potential uses are. 6/26/2015Copyright W. Howden57