Object-Oriented Oriented Programming Factory Method Pattern Abstract Factory Pattern. CSIE Department, NTUT Woei-Kae Chen

Similar documents
Factory Method Pattern Creational. » Define an interface for creating an object but lets subclasses decide the specific class to instantiate

Creational Patterns. Factory Method (FM) Abstract Factory (AF) Singleton (SI) Prototype (PR) Builder (BU)

Laboratorio di Progettazione di Sistemi Software Design Pattern Creazionali. Valentina Presutti (A-L) Riccardo Solmi (M-Z)

Creational Patterns for Variability

Design Pattern- Creational pattern 2015

Introduction and History

Factory Method. Comp435 Object-Oriented Design. Factory Method. Factory Method. Factory Method. Factory Method. Computer Science PSU HBG.

Lecture 21: Design Patterns III

Creational Design Patterns

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

What is Design Patterns?

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

GoF Design Pattern Categories

Second Midterm Review

Object-Oriented Oriented Programming

What is Design Patterns?

6.3 Patterns. Definition: Design Patterns

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Lecture 20: Design Patterns II

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

The GoF Design Patterns Reference

Object-Oriented Design

What is Design Patterns?

Software Design COSC 4353/6353 D R. R A J S I N G H

Design Patterns. Softwaretechnik. Matthias Keil. Albert-Ludwigs-Universität Freiburg

» Building a user interface toolkit that supports multiple look and feel standards WINDOWS XP, MAC OS X, Motif, Presentation Manager, X Window

Design Patterns. Software Engineering. Sergio Feo-Arenis slides by: Matthias Keil

Lecture 13: Design Patterns

OODP Session 5a. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

(Factory Method) (Intent) (Motivation) (instantiate) (defer) f. (application) (application) ƒ (document) ¾ (application-specific)

CSC 301H, Introduction to Software Engineering

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

Provide an interface for creating families of related or dependent objects without specifying their concrete classes

Dr. Xiaolin Hu. Review of last class

Design Patterns. GoF design patterns catalog

CS 2720 Practical Software Development University of Lethbridge. Design Patterns

CSE870: Advanced Software Engineering (Cheng) 1

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Creational Patterns. George Blankenship

Design for change. You should avoid

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

SDC Design patterns GoF

Design patterns. OOD Lecture 6

Object-Oriented Oriented Programming Command Pattern. CSIE Department, NTUT Woei-Kae Chen

The Factory Method Pattern

Softwaretechnik. Design Patterns. Matthias Keil. Albert-Ludwigs-Universität Freiburg

Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

Prototype Description. Interpreter. interpreter Calculator Design Rationales. Prototype Participants. Interpreter with Factory Method.

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Softwaretechnik. Design Patterns. Stephan Arlt SS University of Freiburg. Stephan Arlt (University of Freiburg) Softwaretechnik SS / 47

Object-Oriented Oriented Programming Adapter Pattern. CSIE Department, NTUT Woei-Kae Chen

Object-Oriented Oriented Programming Adapter Pattern

Singleton Pattern Creational. » Ensure a class has only one instance» Provide a global point of access

Singleton, Factory Method, Abstract Factory, Named Constructor. Using one or more base classes to hide details from the client

What are patterns? Design Patterns. Design patterns. Creational patterns. The factory pattern. Factory pattern structure. Lecture 10: OOP, autumn 2003

Design Patterns. Lecture 10: OOP, autumn 2003

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

State Pattern. CSIE Department, NTUT Woei-Kae Chen. State: Intent. Allow an object to alter its behavior when its internal state changes.

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Design Patterns! Acknowledgements!

ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns

Review of last class. 1.Class inheritance versus interface inheritance 2.Inheritance versus composition. Dr. Xiaolin Hu

An Introduction to Patterns

Department of Information Technology

Software Quality Improvement using Design Patterns

families of related or dependent objects

CHAPTER 6: CREATIONAL DESIGN PATTERNS

2.1 Design Patterns and Architecture (continued)

Design Pattern and Software Architecture: IV. Design Pattern

Reuse at Design Level: Design Patterns

Singleton Pattern Creational

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Brief Note on Design Pattern

The 'Gang of Four' Companion

Topics in Object-Oriented Design Patterns

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Page 1. Chapter 8, Object Design: Design Patterns II. Recall: Why reusable Designs? Definitions. A Taxonomy of Design Patterns

EECS 4314 Advanced Software Engineering. Topic 04: Design Pattern Review Zhen Ming (Jack) Jiang

2.1 Design Patterns and Architecture (continued)

Design Patterns Reid Holmes

Factory Method Pattern Tutorial Written Date : September 28, 2009

Design issues: abstraction and patterns Strategy Pattern Singleton Pattern. CSIE Department, NTUT Woei-Kae Chen

Object-Oriented Oriented Programming

Design Patterns. Overview

Design Patterns. Produced by. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

GoF Design Pattern Categories

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Exam Questions. Object-Oriented Design, IV1350. Maximum exam score is 100, grade limits are as follows. Score Grade 90 A 80 B 70 C 60 D 50 E

Software Engineering Design & Construction

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

Prototype Pattern Creational

Using Design Patterns in Java Application Development

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

If we aren t supposed to program to an implementation then how can we actually create new things? Reptile reptile = new Turtle(); Software Engineering

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Template Method Pattern. George Blankenship

Material and some slide content from: - GoF Design Patterns Book. Design Patterns #1. Reid Holmes. Lecture 11 - Tuesday October

Prototype Pattern Creational

Lecture Material. Design Patterns. Visitor Client-Server Factory Singleton

Idioms for Building Software Frameworks in AspectJ

Transcription:

Object-Oriented Oriented Programming Factory Method Pattern Abstract Factory Pattern CSIE Department, NTUT Woei-Kae Chen

Factory Method Pattern Factory Method Pattern Creational pattern

Factory Method: Intent 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

Factory Method: Motivation (1) Document Open() Close() Save() Revert() Framework for office applications Application CreateDocument() NewDocument() OpenDocument() Document* doc=createdocument(); docs.add(doc); doc->open(); Template Method Primitive operation Factory Method MyDocument MyApplication CreateDocument() return new MyDocument;

Factory Method: Motivation (2) GUI for Petri Net simulator Simulator GUIButton CreateNode() GraphNode AddPlace AddTransition AddLink Place Transition Link

Factory Method: Motivation (3) GUI for Logic simulator Dialog to modify the property of a device PropertyDialog Simulator device CreatePropertyDialog() orproperty andproperty CktProperty gateor gateand Circuit

Factory Method: 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.

Factory Method: Structure Client Product OR Creator FactoryMethod() AnOperation()... product = FactoryMethod();... ConcreteProduct ConcreteCreator FactoryMethod() Return new ConcreteProduct;

Factory Method: Participants Product (document) defines the interface of objects the factory method creates. ConcreteProduct (MyDocument) implements the product interface Creator (Application) declares the factory method, which returns an object of the product. Creator may also define a default implementation that returns a default ConcreteProduct object. may call the factory method to create a Product object. ConcreteCreator (MyApplication) overrides the factory method to return an instance of a ConcreteProduct.

Factory Method: Collaboration Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct.

Factory Method: Consequences (1) Benefit eliminates the need to bind application-specific classes into your code (client). client deals with the product interface, therefore it can work with any user-defined ConcreteProduct classes. Potential disadvantage clients might have to subclass the Creator class just to create a particular ConcreteProduct object. fine if the client has to subclass the Creator class anyway

Factory Method: Consequences (2) Provide hooks for subclasses the factory method is called only by Creators Creating objects with a factory method is always more flexible than creating an object directly. Factory Method gives subclasses a hook for providing an extended version of an object. Example: the Document class could define a factory method called CreateFileDialog for opening an existing document. Connects parallel class hierarchies Strategy client call factory method directly parallel class hierarchies result when a class delegates some of its responsibilities to a separate class

Factory Method: Consequences (3) Parallel class hierarchies example manipulation of graphical figures Figure CreateManipulator() Client Manipulator DownClick() Drag() UpClick() LineFigure TextFigure LineManipulator DownClick() Drag() UpClick() TextManipulator DownClick() Drag() UpClick()

Factory Method: Implementation (1) Two major varieties Creator does not provide an implementation for the factory method Creator provides a default implementation for the factory method designers of subclass can change the class of objects their parent class instantiates if necessary Parameterized factory methods create multiple kinds of products the factory method takes a parameter that identifies the kind of object to create the object share the Product interface

Factory Method: Implementation (2) Language-specific variants and issues C++ Factory methods are always virtual functions Factory methods are often pure virtual functions Creator s constructor can not call factory method they are not available yet. lazy initialization: access products solely through accessor operations constructor initialize concrete product as 0 accessor creates the product if it does not exist Naming conventions Use naming conventions to make it clear that you are using factory methods DoMakeClass() // Primitive operation+factory Method DoCreateXXX() // Primitive operation+factory Method

Factory Method: Implementation (3) Using templates to avoid subclassing class Creator { public: virtual Product * CreateProduct()=0; }; template <class TheProduct> class StandardCreator : public Creator { virtual Product * CreateProduct(); } template <class TheProduct> Product * StandardCreator<TheProduct>::CreateProduct() { return new TheProduct; }

Factory Method: Related Patterns Abstract Factory Abstract Factory is often implemented with factory methods. Template Method Factory methods are usually called within Template methods. Prototype Prototype and Abstract Factory are competing patterns. prototype storing objects in advance and clone them later factory method create objects by subclassing Client s view (difference) prototype often require an initialize operation factory method do not need an initialize operation Client s view (similarity) prototype creating itself factory method creating another concrete class object

Abstract Factory Pattern Abstract Factory Pattern Creational pattern

Abstract Factory: Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Also known as Kit

Abstract Factory: Motivation Multiple look-and-feel for Motif and Presentation Manager WidgetFactory CreateScrollBar() CreateWindow() Window Client MotifWidgetFactory CreateScrollBar() CreateWindow() PMWidgetFactory CreateScrollBar() CreateWindow() PMWindow ScrollBar MotifWindow PMScrollBar MotifScrollBar

Abstract Factory: Applicability Use the Abstract Factory when a system should be independent of how its products are created, composed, and represented. a system should be configured with one of multiple families of products. a family of related product objects is designed to be used together, and you need to enforce this constraint. you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Abstract Factory: Structure (1) AbstractFactory CreateProductA() CreateProductB() AbstractProductA Client ConcreteFactory1 CreateProductA() CreateProductB() ConcreteFactory2 CreateProductA() CreateProductB() ProductA2 ProductA1 AbstractProductB ProductB2 ProductB1

Abstract Factory: Structure (2) AbstractFactory CreateProductA() CreateProductB() AbstractProductA Client ConcreteFactory1 CreateProductA() CreateProductB() ConcreteFactory2 CreateProductA() CreateProductB() ProductA2 ProductA1 AbstractProductB Factory Method ProductB2 ProductB1

Abstract Factory: Participants AbstractFactory (WidgetFactory) declares an interface for operations that create abstract product objects. ConcreteFactory (PMWidgetFactory, etc.) implements the operations to create concrete product objects. AbstractProduct (Window, ScrollBar) declares an interface for a type of product object. ConcreteProduct (PMWindow, PMScrollBar) defines a product object to be created by the corresponding concrete factory. implements the AbstractProduct interface Client uses only interfaces declared by AbstractFactory and AbstractProduct classes.

Abstract Factory: Collaboration A single instance of a ConcreteFactory class is created (by client) at run-time. This ConcreteFactory is then used to create product objects for a particular implementation. To create different product objects, client should use a different concrete factory. Abstract Factory defers creation of product objects to its ConcreteFactory subclass.

Abstract Factory: Consequences Benefits It isolates concrete (product) classes (from clients) Product class names do not appear in the client code; they are isolated in the implementation of the concrete factory. It makes exchanging product families easy. Client can use different concrete product family simply by changing the concrete factory. It promotes consistency among products. Easy to enforce an application to use objects from only one family at a time. Liability Supporting new kinds of products is difficult To support a new kind of product, the factory interface must be extended changing AbstractFactory class and all its subclasses.

Abstract Factory: Implementation (1) Factories as Singletons An application typically needs only one instance of a ConcreteFactory. If a global point of access is required. Creating the products Using Factory Method pattern Define a factory method for each product requires a concrete factory subclass for each product family Using Prototype pattern If many product families are possible, the concrete factory can be implemented using Prototype pattern. Initialize concrete factory with a prototypical instance of each product in the family create a new product instance by cloning eliminates the need of subclassing for each product family.

Abstract Factory: Implementation (2) Defining extensible factories Usual design Define a different operation for each kind of product it can produce. Adding a new kind of product requires changing the AbstractFactory, ConcreteFactory, and all the classes that depend on it. A more flexible, but less safe design Add a parameter to operations that create objects. This parameter specifies the kind of product to be created. All products are returned to the client with the same abstract interface as given by the return type all product objects must have the same abstract base class.

Abstract Factory: Related Patterns Factory Method AbstractFactory classes are often implemented with factory methods. Prototype AbstractFactory classes can also be implemented using Prototype. Singleton A concrete factory is often a singleton.