Lecture 18: Design patterns (Command, decorator, state, proxy). The main contents of this chapter include all of the following: Command pattern, decorator pattern, state pattern, proxy pattern. | Ivan Marsic Rutgers University LECTURE 18: Design Patterns Command, Decorator, State, Proxy Topics Command Pattern Decorator Pattern State Pattern Proxy Pattern Protection Proxy Command Pattern Motivation Client A Server B doAction( params ) (a) Client A execute() Receiver B doAction( params ) Command create( params ) (b) unexecute() (Server) Command Pattern Motivation Motivation: To separate parameter preparation from passing program control (decision on when to call) Client A Server B doAction( params ) Client A execute() 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 . | Ivan Marsic Rutgers University LECTURE 18: Design Patterns Command, Decorator, State, Proxy Topics Command Pattern Decorator Pattern State Pattern Proxy Pattern Protection Proxy Command Pattern Motivation Client A Server B doAction( params ) (a) Client A execute() Receiver B doAction( params ) Command create( params ) (b) unexecute() (Server) Command Pattern Motivation Motivation: To separate parameter preparation from passing program control (decision on when to call) Client A Server B doAction( params ) Client A execute() 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 ., 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) Preparation Execution Command Pattern Motivation Problem: Variable and evolving method signature If Server code changes, Client code needs to change, too Method 1 Method 2 Client Server (Receiver) Before: Command Pattern Improvement Method 1 Method 2 Client Server (Receiver) Before: After: The interface to the Server object is much simpler. execute() execute() Client Command 1 Command 2 Method 1 Method 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. Command Pattern Improvement Command .