Design Patterns Part 2: Factory, Builder, & Memento
Housekeeping Review exams Class average is very high, 88% Presentations next Thursday Start with slides answering: What is the problem the tool solves? How does the tool solve it? (high-level) Where can we get it? And anything we need to know to use it. Demo how to use the tool and its main features Email me today if you want to present for extra credit
Last time Observer: Singleton: Adapter: Keeping up with the times Making sure there is only one Adapter: Filling the gaps
Today’s Objectives: Three more patterns Factory Builder Memento
Lots of patterns out there! Abstract Factory Builder Factory Method Prototype Singleton Adapter Bridge Composite Decorator Strategy Template Method Visitor Façade Flyweight Proxy Chain of responsibility Command Interpreter Iterator Mediator Memento Observer State
Lets talk video games… How to create a dynamic number of varying objects? Handling all of the initialization can be messy Clash of Clans – spawn units Candy Crush – generate endless blocks Fortnite – create projectiles
Solution: Factory Decouple the “owner” from creating the “object” Encapsulation Would be difficult to change later Works well with subtyping E.g., the level class would use specific block classes Instead, Factory knows how to create it Owner passes parameters to Factory
Factory
Lets talk video games… Clash Royale: Candy Crush: Fortnite: Each unit type has a Factory, player has 8 factories Pass info to Factory, such as card, level, and location Candy Crush: Each level has a Factory Level uses info like difficulty, probability, and what blocks should be allowed Fortnite: Each player can have a Factory Generates the correct projectile based on the weapon
Running Example: Tiger Dining
Recall: Adapter Pattern Solution to Support Multiple Payment Methods
Goal: To add additional payment methods PaymentsController new create … DiningDollars charge(uid) …
Solution: Adapter Interface which bridge a gap in payment methods Key objects are Target, Adapter and Adaptee Target request() Client Adapter request() Adaptee specificrequest()
DiningDollarsAdapter PaymentProcessor process_payment() PaymentsController … DiningDollarsAdapter process_payment() VisaPayAdapter process_payment() DiningDollars charge(uid) VisaPay pay(debitcard, cvv, exp)
Visa processes the payment Control Flow PaymentsController create new vpa:VisaPayAdapter new vp:VisaPay process_payment pay Visa processes the payment
Yuck!
Clean up payment initialization Design problem: Clean up payment initialization
Solution: Separate construction code Needs to separate construction of a complex object Same construction interface creates different representation
Solution: Builder Director construct() Builder build() ConcreteBuilder Product
… PaymentsController create() PaymentProcessorBuilder build(params) DiningDollarsAdapterBuilder build(params) VisaPayAdapterBuilder build(params) DiningDollarsAdapter VisaPayAdapter
Goal: Implement Builder pattern Step 1: Create Builder Classes Step 2: Move constructor code out of controller inside build Step 3: instantiate build class inside construct
Goal: Implement Builder pattern Step 1: Create Builder Classes Step 2: Move constructor code out of controller inside build Step 3: instantiate build class inside construct
Goal: Implement Builder pattern Step 1: Create Builder Classes Step 2: Move constructor code out of controller inside build Step 3: instantiate build class inside construct
Goal: Implement Builder pattern Step 1: Create Builder Classes Step 2: Move constructor code out of controller inside build Step 3: instantiate build class inside construct
@builder = … @builder = …
vpab:VisaPayAdapterBuilder Control Flow : PaymentsController create new vpab:VisaPayAdapterBuilder build
vpab:VisaPayAdapterBuilder Control Flow : PaymentsController create new vpab:VisaPayAdapterBuilder build new vpa:VisaPayAdapter process_payment
Delete from cart and Undo Design problem: Delete from cart and Undo
Cannot retrieve back
OrdersController new create … destroy
Solution: Memento Needs to store the state of the order Caretaker Originator state … create_memento(): Memento set_memento(memento) Memento state … getters and setters … In Java, the Memento is often a nested class inside the Originator class Caretaker
OrderMementoCaretaker Originator state … create_memento(): Memento set_memento(memento) Memento state … getters and setters … Caretaker Order … create_memento set_memento(version) OrderMemento order … initialize OrderMementoCaretaker
Summary: Keep one or more previous states of an object Provide a way to revert to those states
Java Example
Java Example
Goal: Implement memento pattern Order “create_memento” and “set_memento” OrderMemento initialize OrderMementoCaretaker “push_memento” and “pop_memento”
Memento : Examples Editor (VS Code, Sublime, Notepad++) Provides features of undo and redo What actions create Mementos? Does each editor have its own stack of Mementos?
For more design patterns, see the “Gang of Four” book (GoF)
Challenge for Iteration 3: Use a design pattern https://en.wikipedia.org/wiki/Software_design_pattern Probably already using several Identify them and clean up the code to match the “pattern”