Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
Plab – Tirgul 12 Design Patterns
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Factory Method Joey Richey Kevin Gorski. Definition Allows a class developer define the interface for creating an object while retaining control of which.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
© O. Nierstrasz, O. Greevy, A. Kuhn P2 — Clients and Servers 8.1 Roadmap  Generics  Abstract Factory  Annotations  Model-Driven Engineering.
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
1 Creational Patterns CS : Software Design Winter /T8.
Design Patterns Examples in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
1 Design Patterns CS 123/CS Outline zDefinition and Description of a Design Pattern zDiscussion of Selected Patterns zKinds of Patterns Reference:
Design Patterns.
Creational Patterns (1) CS350, SE310, Fall, 2010.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F
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.
Creational Patterns CSE Creational Patterns Class creational pattern ◦ uses inheritance to vary the class that is instantiated Object creational.
Factory Method Chris Colasuonno Also known as “Virtual Constructor”
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Advanced Object-oriented Design Patterns Creational Design Patterns.
CSC 480 Software Engineering Lab 5 – Abstract Factory Pattern Oct 30, 2002.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
 Creational design patterns abstract the instantiation process.  make a system independent of how its objects are created, composed, and represented.
The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1.
S.Ducasse Stéphane Ducasse 1 Abstract Factory.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Design Patterns: MORE Examples
Abstract Factory Pattern
Factory Method Pattern
Strategy Design Pattern
Factory Patterns 1.
Software Design and Architecture
Factory Method Pattern
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Design Patterns - A few examples
Software Engineering Lecture 7 - Design Patterns
Abstract Factory Pattern
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
CSC 480 Software Engineering
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Lesson 5: More on Creational Patterns
Creational Patterns.
CSC 480 Software Engineering
Design by Abstraction (Continuation) CS 3331 Spring 2005
Presentation transcript:

Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns

Intent of both patterns Separate the implementation of objects from their use by defining an interface for creating the objects without specifying their concrete classes Abstract Factory: focus is on allowing multiple implementations of a product Factory Method: focus is on generalizing the creator-product relationship Abstract Factory uses the Factory Method pattern

Abstract Factory Intent: provide an interface for creating objects without specifying their concrete classes Example: Stacks, Queues, and other data structures Want users to not know or care how these structures are implemented (separation) Example: UI toolkit to support multiple look-and-feel standards, e.g., Motif, PM Abstract class for widget, supporting class for specific platform widget

Solutions in C++ Use of header file (class declarations) and implementation file (method definitions) ok but limited Header file usually contains private declarations which are technically part of the implementation Change in implementation requires that the application using the data structure be recompiled Alternative: create an abstract superclass with (pure) virtual data structure methods

Design Solution for Abstract Factory Factory createProduct() AbstractProduct virtual methods ConcreteProdA methods ConcreteProdB methods Client Note: this is an abbreviated design

Participants Factory implements the operations to create concrete product objects actual pattern includes abstract and concrete factory classes that generalizes the relationship between factory and product (Abstract) Product: declares an interface for a type of product object Concrete Product defines a product object to be created by the corresponding concrete factory implements the abstract product interface Client: uses only Factory and Abstract Product

Stack Example StackFactory createStack() Stack push(), pop() ArrayStack push(), pop() LinkedStack Push(), pop() Client … Stack s; s = StackFactory.createStack(); … s.pop(); … return new ArrayStack();

Stack Example (C++) Stack class defines virtual methods push(), pop(), etc. ArrayStack and LinkedStack are derived classes of Stack and contain concrete implementations StackFactory class defines a createStack() method that returns a ptr to a concrete stack Stack *createStack() { return new ArrayStack(); } Client programs need to be aware of Stack and StackFactory classes only No need to know about ArrayStack()

Factories in Java Stack is an Interface ArrayStack and LinkedStack implement Stack StackFactory returns objects of type Stack through its factory methods Select class of the concrete prodcut it supplies to client objects If using info from requesting client, can hardcode selection logic and choice of factory objects Can separate selection logic for concrete factories from the data it uses to make the selection

Abstract Factory Consequences Factory class or method can be altered without affecting the application Concrete classes are isolated Factory class can be responsible for creating different types of objects e.g., DataStructure factory that returns stacks, queues, lists, etc. “product families” Promotes product consistency

Factory Method Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses Example: Generalizing the relationship between an application and the documents it processes Generalization: Application creates Documents Concretized by: MS Paint creates Gifs, MS Word creates Word Documents, MS Excel creates spreadsheets

Design Solution for Factory Method Factory factoryMethod() Product virtual methods ConcreteProduct methods ConcreteFactory factoryMethod()

Application-Document Example Application createDoc() Document virtual methods MyDocument methods MyApplication createDoc() return new MyDocument();

Factory Method Consequences Separation of interface from implementation, providing implementation flexibility Connects parallel hierarchies for consistency