Design Patterns Part 2: Factory, Builder, & Memento

Slides:



Advertisements
Similar presentations
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Advertisements

18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Patterns Reusable solutions to common object-oriented programming problems When given a programming problem, re-use an existing solution. Gang of Four.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CSSE 374: 3½ Gang of Four Design Patterns These slides derived from Steve Chenoweth, Shawn Bohner, Curt Clifton, and others involved in delivering 374.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Design Patterns in Java Chapter 1 Introduction Summary prepared by Kirk Scott 1.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
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.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Design Pattern Dr. Zhen Jiang West Chester University url:
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
Creational Patterns
What to know for the exam. Smalltalk will be used for questions, but there will not be questions about the grammar. Questions might ask – how particular.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Proxy.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Scott Cate CloudDB.com Session Code: DPR401.
Interface Patterns. Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
7 April 2004CSci 210 Spring Design Patterns 2 CSci 210.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns Spring 2017.
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Common Design Patterns
Design Patterns Lecture part 2.
Introduction to Design Patterns
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 Design Patterns
Design Patterns Part 1: Observer, Singleton, & Adapter
Design Patterns Part 2: Builder & Memento
How to be a Good Developer
Design Patterns with C# (and Food!)
Chapter 8, Design Patterns Bridge
object oriented Principles of software design
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
How to be a Good Developer
Design Patterns in Game Design
Informatics 122 Software Design II
CSE 403 Software Design.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Informatics 122 Software Design II
CIS 644 Tues. Nov. 30, 1999 W15A … patterns.
Presentation transcript:

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”