Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns Part 2: Factory, Builder, & Memento

Similar presentations


Presentation on theme: "Design Patterns Part 2: Factory, Builder, & Memento"— Presentation transcript:

1 Design Patterns Part 2: Factory, Builder, & Memento

2 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 me today if you want to present for extra credit

3 Last time Observer: Singleton: Adapter: Keeping up with the times
Making sure there is only one Adapter: Filling the gaps

4 Today’s Objectives: Three more patterns
Factory Builder Memento

5 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

6 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

7 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

8 Factory

9 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

10 Running Example: Tiger Dining

11 Recall: Adapter Pattern Solution to Support Multiple Payment Methods

12 Goal: To add additional payment methods
PaymentsController new create DiningDollars charge(uid)

13 Solution: Adapter Interface which bridge a gap in payment methods
Key objects are Target, Adapter and Adaptee Target request() Client Adapter request() Adaptee specificrequest()

14 DiningDollarsAdapter
PaymentProcessor process_payment() PaymentsController DiningDollarsAdapter process_payment() VisaPayAdapter process_payment() DiningDollars charge(uid) VisaPay pay(debitcard, cvv, exp)

15

16

17

18

19

20

21

22

23 Visa processes the payment
Control Flow PaymentsController create new vpa:VisaPayAdapter new vp:VisaPay process_payment pay Visa processes the payment

24 Yuck!

25 Clean up payment initialization
Design problem: Clean up payment initialization

26 Solution: Separate construction code
Needs to separate construction of a complex object Same construction interface creates different representation

27 Solution: Builder Director construct() Builder build() ConcreteBuilder
Product

28 … PaymentsController create() PaymentProcessorBuilder build(params)
DiningDollarsAdapterBuilder build(params) VisaPayAdapterBuilder build(params) DiningDollarsAdapter VisaPayAdapter

29 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

30 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

31

32 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

33

34

35

36 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

37

38 @builder = … @builder = …

39 vpab:VisaPayAdapterBuilder
Control Flow : PaymentsController create new vpab:VisaPayAdapterBuilder build

40 vpab:VisaPayAdapterBuilder
Control Flow : PaymentsController create new vpab:VisaPayAdapterBuilder build new vpa:VisaPayAdapter process_payment

41 Delete from cart and Undo
Design problem: Delete from cart and Undo

42

43 Cannot retrieve back

44 OrdersController new create destroy

45 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

46 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

47 Summary: Keep one or more previous states of an object
Provide a way to revert to those states

48 Java Example

49 Java Example

50 Goal: Implement memento pattern
Order “create_memento” and “set_memento” OrderMemento initialize OrderMementoCaretaker “push_memento” and “pop_memento”

51 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?

52 For more design patterns, see the “Gang of Four” book (GoF)

53 Challenge for Iteration 3: Use a design pattern
Probably already using several Identify them and clean up the code to match the “pattern”


Download ppt "Design Patterns Part 2: Factory, Builder, & Memento"

Similar presentations


Ads by Google