1 Ivan Marsic Rutgers University LECTURE 18: Design Patterns Command, Decorator, State, Proxy.

Slides:



Advertisements
Similar presentations
Testing Relational Database
Advertisements

The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
OOAD Using the UML - Use-Case Analysis, v 4.2 Copyright  Rational Software, all rights reserved 1/18 Use Case Analysis – continued Control Classes.
Computer Science 313 – Advanced Programming Topics.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Zaki Alasadi Supervisor:Dr noorhosseini.
CS 290C: Formal Models for Web Software Lecture 10: Language Based Modeling and Analysis of Navigation Errors Instructor: Tevfik Bultan.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
FIREWALL TECHNOLOGIES Tahani al jehani. Firewall benefits  A firewall functions as a choke point – all traffic in and out must pass through this single.
Dependency Injection and Model-View-Controller. Overview Inversion of Control Model-View-Controller.
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.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
UML Collaboration Diagram. Recap System Sequence Diagrams (SSD) UML for SSD Examples.
Faculty of Informatics and Information Technologies Slovak University of Technology Peter Kajsa and Ľubomír Majtás Design.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
What is Architecture  Architecture is a subjective thing, a shared understanding of a system’s design by the expert developers on a project  In the.
Structural Pattern: Decorator There are times when the use of subclasses to modify the behavior of individual objects is problematic. C h a p t e r 4.
Architecture styles Pipes and filters Object-oriented design Implicit invocation Layering Repositories.
1 ITEC 3010 “Systems Analysis and Design, I” LECTURE 10: Use Case Realizations [Prof. Peter Khaiter]
CSC 313 – Advanced Programming Topics. Design Pattern Intent  Each design pattern is a tool  Like all tools, have reason for being.
Abstract Factory Pattern. What Is an Abstract Factory Pattern?  The Abstract Factory pattern is a creative way to expand on the basic Factory pattern.
ARCH-4: The Presentation Layer in the OpenEdge® Reference Architecture Frank Beusenberg Senior Technical Consultant.
Analysis of SQL injection prevention using a filtering proxy server By: David Rowe Supervisor: Barry Irwin.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Software Construction Lecture 18 Software Testing.
Lexi case study (Part 2) Presentation by Matt Deckard.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Chapter 38 Persistence Framework with Patterns 1CS6359 Fall 2011 John Cole.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
Structural Design Patterns
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Title Carolina First Steering Committee October 9, 2010 Online Voting System Design Yinpeng Li and Tian Cao May 3, 2011.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
INFO 620Lecture #71 Information Systems Analysis and Design Design Class Diagrams and others INFO 620 Glenn Booker.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
I/O Software CS 537 – Introduction to Operating Systems.
Communication Diagrams Lecture 8. Introduction  Interaction Diagrams are used to model system dynamics  How do objects change state?  How do objects.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
A Controlled Experiment in Maintenance Comparing Design Patterns to Simpler Solutions By Prechelt, Unger, Tichy, Brössler, and Votta Presentation by Chris.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns – Group 2 Iterator, Proxy, Adapter, Decorator.
Computer System Structures
Abstract Factory Pattern
LECTURE 18: Design Patterns Command, Decorator, State, Proxy
Chapter 7 – Object-Oriented Design
MPCS – Advanced java Programming
Behavioral Design Patterns
Abstract Factory Pattern
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Intent (Thanks to Jim Fawcett for the slides)
Model-View-Controller (MVC) Pattern
Software Architecture
LECTURE 17: Design Patterns Publisher-Subscriber
LECTURE 17: Design Patterns Publisher-Subscriber
Object Oriented Design & Analysis
Proxy Pattern Definition of “Proxy”: Authority or power to act for another Original Gang of Four pattern, much used Stands in for a “real object” (similar.
Presentation transcript:

1 Ivan Marsic Rutgers University LECTURE 18: Design Patterns Command, Decorator, State, Proxy

2 Topics  Command Pattern  Decorator Pattern  State Pattern  Proxy Pattern –Protection Proxy

3 Command Pattern Motivation Client A Client A Server B Server B doAction( params ) (a) Client A Client A execute() Receiver B Receiver B doAction( params ) Command create( params ) (b) unexecute() (Server)

4 Command Pattern Motivation  Motivation: To separate parameter preparation from passing program control (decision on when to call) Client A Client A Server B Server B doAction( params ) Client A Client A execute() Receiver B Receiver B doAction( params ) Command create( params ) unexecute()  Reasons for separation: –Separate preparation of calling parameters (which may become available much before the execution time or may become available incrementally) All calling parameters become localized in a Command object (“encapsulated”) Parameters may be prepared for the Client by a different object (“Custodian”) Client and Custodian objects’ codes may evolve separately –I.e., different developers develop and maintain or upgrade these classes –May prepare all Commands in a list (with different parameters or different Receivers) and simply iterate through the list to execute all –For un-execute (roll-back) capability Parameters may be prepared by a different object (“Custodian”) (Server) PreparationExecution

5 Command Pattern Motivation  Problem: Variable and evolving method signature –If Server code changes, Client code needs to change, too Client Server (Receiver) Before:

6 Command Pattern Improvement Client Server (Receiver) Before: After:  The interface to the Server object is much simpler. Client Command 1 Command 2 Server (Receiver) The change towards the Cmd. pattern may not appear radical when viewed overall, but looking from the client’s standpoint, the simplification is significant.

7 Command Pattern Improvement  Command provides a uniform method signature (“interface”) to the Server –The Command interface never changes, so Server changes do not force Client changes  Client only decides when to execute()  Client evolution is decoupled from Server implementation and Command implementation –Client versus Server/Command can be responsibilities of different developers

8 Command Pattern (a) Forward execution (do) (c) (b)

9 Command Advantages  Execution is usually called with other business logic –Now it is decoupled from parameter preparation, which can be done at another place (“staging area”), not interfering with business logic (“execution area”) –  Business logic is decoupled from parameter preparation Client and Custodian codes may evolve independently, by different developers

10 Command Pattern Interaction  Many times commands can be reversed/undone  Extended interface to check for reversibility and, if true, undo (a) (b) Reverse execution (undo)

11 Decorator Pattern Motivation  Motivation: To separate essential from non- essential functions and allow easy adding of new non-essential functions –Implies only one Subject — one essential function, one “responsibility”  Solution: Client only has a pointer to the “head of the list” (of services) and does not know the true identity of the head object –All services in the list look the same since all implement the same interface(here, “list” is not a list data structure!!)  Advantages: –When a new optional function/service is added, client code does not need to change –Only need to program the new function/service and insert it in the “linked list” (  “dependency injection”)

12 Decorator  Client only knows the head-of-the-list –But doesn’t know it’s identity (RealSubject vs. Decorator), because all list elements implement the same abstract interface (Subject) –Client doesn’t know how many Decorators are in the list  The list can seamlessly expand or shrink (a) (b)

13 Decorator (a) (c) (b) Uniform method calling, regardless of the head-of-the-list object identity Pre-processing Post-processing Pre- versus Post-processing is defined relative to the essential feature: the request() of RealSubject

14 Decorator Example – GUI Options

15 Decorator - Example

16 Deco Example – Unlock Use Case

17 Example: Midterm #2, Spring 2013 State diagram for Display Interaction: Ready Faulty measurement-initiated / failure-detected / Measuring [battery-level  threshold] / Discharged measurement-completed / battery-charged / button-pressed / display msg [battery-level  threshold] / button-pressed / display msg button-pressed / display msg  button-pressed / display msg  button-pressed / display msg  Not-Worn wearing-detected / Problem: Design the UML sequence diagram; apply design patterns Given:

18 Student solution: Class diagram client «interface» DeviceCheck + check() ButtonCheck + increment() Subject and Decorator interface BatteryCheck + checkBattery() DeviceWearChk + checkWearing() FaultyCheck + checkSensor() Concrete Decorators MeasuringCheck + chkMeasuring() BP_Check + checkBP() + display() HR_Check + checkBP() + display() AL_Check + checkAL() + display() BL_Check + checkBL() + display() Real Subjects BP = blood pressure HR = heart rate AL = activity level BL = battery level

19 Student solution: Class diagram  What went wrong? –Decorators and Subjects don’t implement the top-level interface Methods are named differently –More than 1 Subject Not in the spirit of this pattern client «interface» DeviceCheck + check() ButtonCheck + increment() Subject and Decorator interface BatteryCheck + checkBattery() DeviceWearChk + checkWearing() nextDevice FaultyCheck + checkSensor() Concrete Decorators MeasuringCheck + chkMeasuring() BP_Check + checkBP() + display() HR_Check + checkBP() + display() AL_Check + checkAL() + display() BL_Check + checkBL() + display() Real Subjects

20 display Student sol’n: Sequence diagram : DeviceWearChk: FaultCheck: Measuring Check checkFault() client : display error msg alt [else] buttonPress( ) device not worn OR faulty OR measuring checkWearing() checkMeasuring() : BPCheck checkBP() : HRCheck checkHR() display

21 display Student sol’n: Sequence diagram  What went wrong? –Decorator objects are not forming a “linked list” and calling each other uniformly — instead, the Client is calling all decorators in sequence!! : DeviceWearChk: FaultCheck: Measuring Check checkFault() client : display error msg alt [else] buttonPress( ) device not worn OR faulty OR measuring checkWearing() checkMeasuring() : BPCheck checkBP() : HRCheck checkHR() display

22 Another Student Solution  Correct use of the Decorator pattern: All Decorators implement the same interface client «interface» Subject + request() Measurement + request() Decorator + request() next object SafetyZoneChecker + request() ActivityLevelChecker + request()

23 Another Student Solution  Uniform calling approach — client calls only the head of the “linked list” : ActivityLevelChecker: Measurement result checkActivityLevel( ) : SafetyZoneCheckerclient : request( args ) checkSafetyZones( ) result ‡  request( args ) result   and ‡ denote added special- case processing

24 State Pattern Motivation  Motivation: To separate state-dependent event- handling functions from each other and allow easy adding of new states and events  Solution: Event-handling object (“server” or “context”) externalizes its state-dependent functionality into different “state objects” –Context only has a reference to the current state object and does not know its true identity ( knows that it’s a state, but not which one ) –All State objects look the same (all implement the same interface)  Advantages: –When a new state or event is added, client code does not need to change –Instead of implementing a single big state-transition table, say 10 states by 15 events, we implement 10 State objects Each State object maintains only a small part of the big table relevant to it (input-event / next-state) –Only need to program the new State object and link it with other states according to the transition diagram (  “dependency injection”)

25 State Pattern (a) (b) (c)

26 State Pattern (a) (b) (c)(d)

27 State Pattern  All State objects are instantiated and mutually interlinked with each other (“dependency injection”) –A State object knows the next states (depending on input events and guard conditions) –State’s method handle(Event) returns the next state  The Context object knows only about one State object (representing the “current state”) and keeps updating it as told by the return value from currentState.handle(Event).  Context does not know (nor need to know!) the true identity of the current state object – all State objects implement the same abstract interface

28 Proxy Notes:  Proxy is structurally the same as Decorator, but has different intention  We could have a “linked list” of Proxies, like with Decorators (a) (b) (c)

29 Protection Proxy – Example (1)  What if we wanted to add a Maintenance role & access privileges? –Instead of hard-coding the new role privileges, we just define a new Protection Proxy

30 Protection Proxy – Example (2)

31 Protection Proxy – Example (3)