12/6/20041 The Factory Method Pattern Presenters 王世賀 F9006029 陳祐毓 F9006047 張峻銘 F9006065 吳佩達 F9006084 林俊成 F9006097 鄭榮智 F9006100 許書豪 F9006205.

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.
T O K ILL A S INGLETON F ACTORY M ETHOD P ATTERN Josh Mason 6/18/09.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Nov, 1, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and its.
Factory Method By Judith Mziray And Jerry Cipolla.
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Prototype Pattern Intent:
DESIGN PATTERNS Redesigning Applications And
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Examples in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Design Patterns.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Creational Patterns (1) CS350, SE310, Fall, 2010.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
GoF Sections Design Problems and Design Patterns.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
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.
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
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.
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns I.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Factory Pattern Sanjay Yadav (ISE ).
Advanced Object-oriented Design Patterns Creational Design Patterns.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
SOFTWARE DESIGN Design Patterns 1 6/14/2016Computer Science Department, TUC-N.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Factory Method Pattern. Admin SCPI Patner Day Feb. 21 Lunch count Presentation (4-8 min.) Practice on Feb. 16. Morning availablity on Feb21 Brief overview.
Design Patterns: MORE Examples
Abstract Factory Pattern
Factory Method Pattern
Strategy Design Pattern
Low Budget Productions, LLC
Factory Patterns 1.
Behavioral Design Patterns
Software Design and Architecture
Software Design and Architecture
Memento Pattern F 羅世東 F 李天彥 F 鄧義佐 F 林尚霖
Factory Method Pattern
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Lesson 5: More on Creational Patterns
Creational Patterns.
Design by Abstraction (Continuation) CS 3331 Spring 2005
Presentation transcript:

12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F

12/6/20042 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference

12/6/20043 Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 許書豪

12/6/20044 Motivation ( 1/4 ) Useful to Frameworks for applications Consider a framework for applications that can present multiple documents Two key abstractions Application managing Documents : create Open or New from a menu Document application-specific implementations To subclass and to implement

12/6/20045 Motivation ( 2/4 )

12/6/20046 Motivation ( 3/4 ) A drawing application Class DrawingApplication extends Application Class DrawingDocument extends Document A imgzip application Class ImgzipApplication extends Application Class ImgzipDocument extends Document particular Document subclass

12/6/20047 Motivation ( 4/4 ) Particular Document subclass to instantiate is application-specific when and what kind questions to the Application class

12/6/20048 Problem The framework must instantiate classes, but it only knows about abstract classes, which it cannot instantiate

12/6/20049 In Real World ( 1/2 ) A Bakery

12/6/ In Real World ( 2/2 ) A Fruit Gardener

12/6/ Solution ( 1/3 ) The Factory Method pattern encapsulates the knowledge of which Document subclass to create and moves this knowledge out of the framework Lets a class ( abstractions ) defer instantiation to subclasses

12/6/ Solution ( 2/3 ) Product & Creator

12/6/ Solution ( 3/3 ) Example

12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 吳佩達

12/6/ Intent Intent by the book “ Design Patterns ” Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Also known as Virtual Constructor

12/6/ Example ( 1/2 ) Class Diagram in the book “ Design Patterns ” Factory Method

12/6/ Example ( 2/2 ) Application subclasses redefine an abstract CreateDocument operation on Application to return the appropriate Document subclass We call CreateDocument a factory method because it's responsible for "manufacturing" an application-specific object

12/6/ Applicability Use the Factory Method pattern when a class can't anticipate the class of objects it must create a class wants its subclasses to specify the objects it creates classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate ( Composite )

12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 張峻銘

12/6/ Participants ( 1/2 ) Abstract Creator declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object ( see Consequences 1 )

12/6/ Participants ( 2/2 ) Abstract Product defines the interface of objects the factory method creates ConcreteCreator overrides the factory method to return an instance of a ConcreteProduct ConcreteProduct implements the Product interface

12/6/ Structure Class Diagram form “ Design Patterns ” To instantiate a Creator-specific Product instance

12/6/ Collaborations Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct

12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 林俊成

12/6/ Consequences ( 1/3 ) Factory methods eliminate the need to bind application-specific classes into your code. A potential disadvantage of factory methods is that clients might have to subclass the Creator class just to create a particular ConcreteProduct object.

12/6/ Consequences ( 2/3 ) Two additional consequences of the Factory Method pattern Provides hooks for subclasses Connects parallel class hierarchies

12/6/ Consequences ( 3/3 ) Connects parallel class hierarchies

12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 鄭榮智

12/6/ Implementation ( 1/5 ) Two major varieties Creator class is an abstract class and does not provide an implementation for the factory method it declares Creator class is a concrete class and provides a default implementation for the factory method

12/6/ Implementation ( 2.1/5 ) Parameterized factory methods Another variation lets the factory method create multiple kinds of Products Takes a parameter that identifies the kind of object to create All objects the factory method creates will share the Product interface Application might support different kinds of Documents

12/6/ Implementation ( 2.2/5 ) class Creator { public: virtual Product* Create( ProductId ); }; Product* Creator::Create ( ProductId id ) { if ( id == MINE ) return new MyProduct; if ( id == YOURS ) return new YourProduct; // repeat for remaining products... return 0; }

12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Presenter 王世賀

12/6/ Implementation ( 3.1/5 ) Language-specific variants and issues Factory methods in C++ are always virtual functions and are often pure virtual. Just be careful not to call factory methods in the Creator's constructor — the factory method in the ConcreteCreator won't be available yet

12/6/ Implementation ( 3.2/5 ) Lazy initialization Instead of creating the concrete product in the constructor, the constructor merely initializes it to 0. The accessor returns the product. But first it checks to make sure the product exists, and if it doesn't, the accessor creates it

12/6/ Implementation ( 3.3/5 )

12/6/ Implementation ( 4.1/5 ) Using templates to avoid subclassing Another potential problem with factory methods is that they might force you to subclass just to create the appropriate Product objects

12/6/ Implementation ( 4.2/5 )

12/6/ Implementation ( 4.3/5 )

12/6/ Outline Motivation Problem In Real World Solution Intent Example Applicability Participants Structure Collaborations Consequences Implementation Related Patterns Reference Presenter 陳祐毓

12/6/ Implementation ( 5/5 ) Naming conventions make it clear you're using factory methods For example, the MacApp Macintosh application framework ( declares the abstract operation that defines the factory method as Class* DoMakeClass(), where Class is the Product class )

12/6/ Related Patterns Abstract Factory Factory method: other example

12/6/ Reference 閻宏, Java 與樣式理論, 碁峰資訊股份有 限公司, ISBN , 結城 浩, Design Patterns 於 Java 語言上的 實習應用, 博碩文化股份有限公司, ISBN ,

12/6/ The End