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

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

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

Lecture 13: Design Patterns

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

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

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

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

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

GoF Design Pattern Categories

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

Design Pattern- Creational pattern 2015

Design Patterns. GoF design patterns catalog

Dr. Xiaolin Hu. Review of last class

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

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

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

CSE870: Advanced Software Engineering (Cheng) 1

Object-Oriented Design

An Introduction to Patterns

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

2.5.1: Reforms in Continuous Internal Evaluation (CIE) System at the Institutional Level

Second Midterm Review

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

Factory Method Pattern Tutorial Written Date : September 28, 2009

Design Pattern and Software Architecture: IV. Design Pattern

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

Using Design Patterns in Java Application Development

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

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

Prototype Pattern Creational

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

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

Topics in Object-Oriented Design Patterns

Design Patterns Cont. CSE 110 Discussion - Week 9

The GoF Design Patterns Reference

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

Singleton Pattern Creational

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

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

Reuse at Design Level: Design Patterns

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

Prototype Pattern Creational

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

What is Design Patterns?

Introduction to Design Patterns

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

SDC Design patterns GoF

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

Creational Patterns for Variability

Prototype Pattern Creational!

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design Patterns. CSC207 Fall 2017

Idioms for Building Software Frameworks in AspectJ

Object-Oriented Oriented Programming Adapter Pattern

CS111: PROGRAMMING LANGUAGE II

11/12/12. Objectives DESIGN PATTERNS. Design Pattern. Defined Design Patterns. Applying Design Patterns. Motivating Example

Template Method. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 24 11/15/2007. University of Colorado, 2007

Object-Oriented Oriented Programming

Object Oriented Programming. Java-Lecture 11 Polymorphism

Design Patterns. CSC207 Fall 2017

Design Patterns. CSC207 Winter 2017

Chapter 14 Abstract Classes and Interfaces

What is Design Patterns?

GoF Design Pattern Categories

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

CSCE3193: Programming Paradigms

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Programming II (CS300)

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

Chapter 10 Classes Continued. Fundamentals of Java

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

» Access elements of a container sequentially without exposing the underlying representation

Exam in TDDB84: Design Patterns,

Some instance messages and methods

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Brief Note on Design Pattern

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Appendix-A. A.1 Catalogues of Design Patterns. Below is the definition for each design pattern using the FINDER notation, followed

What is Design Patterns?

Applying the Factory Method Design Pattern

Creational Design Patterns

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Product line annotations with UML-F

Polymorphism. Arizona State University 1

Using Design Patterns in Education and Tutoring for the Software Systems Projects in Economic

Product Line Annotations with UML-F

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

Design Patterns Reid Holmes

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

Visitor Pattern.» Represent an operation to be performed on all of the components of an object structure

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Lecture 21: Design Patterns III

C++ Important Questions with Answers

Object-Oriented Concepts and Design Principles

Transcription:

Factory Method Pattern Creational Intent» Define an interface for creating an object but lets subclasses decide the specific class to instantiate > Delegate creation to the appropriate subclass Also known as» Virtual constructor Factory-1

Motivation Frameworks use abstract classes» To define and maintain relationships between objects» May be responsible for creating them Factory-2

Motivation 2 Consider an application that manages multiple document types» Application class manages documents create as required from New in menu > Knows when to create but not what to create» Document class is application specific > Drawing document > Text document > Spreadsheet document Factory-3

Motivation 3 Application subclasses redefine an abstract create operation to return the appropriate type of object We call these methods factory methods because they are responsible for manufacturing an object Factory-4

Example Architecture DOCUMENT * make * open * close * save * docs:[ ] doc = create_document( ) docs.add(doc) doc.open_document APPLICATION + create_document( ) open_document new_document MY_DOC + make open close save Result = mydoc.make MY_APP + create_document mydoc Factory-5

Abstract Architecture Product * product Creator * factorymethod( ) * anoperation() * product = factorymethod( ) ConcreteProduct + concreteproduct ConcreteCreator + factorymethod() Result = concreteproduct.make( ) Factory-6

Scenario A trivial scenario» The appropriate concrete method is invoked through polymorphism. Factory-7

Participants Product» Defines the interface of objects the factory method creates Concrete product» Implements the Product interface Factory-8

Participants 2 Creator» Declares the factory method, which returns an object of type Product.» May define a default implementation of the factory method» May call the factory method to create a Product object Concrete creator» Overrides the default factory method to return an instance of the appropriate subclass Factory-9

Applicability A class cannot anticipate the class of the 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-10

Consequences Provides hooks for subclasses» Avoids creating an object directly Connects parallel class hierarchies» A class may delegate some of its responsibilities to a separate class» For a given class hierarchy (e.g. Figures) delegated responsibilities may be different for each type of Figure (stretch a line is different from stretch a box) > As a consequence a parallel hierarchy of manipulator classes may be created Factory-11

Parallel Hierarchy Example Figure * createmanipulator * Manipulator * stretch * Line + Box + createmanipulator + createmanipulator + Line + stretch + Box + stretch + Factory-12

Related Patterns Abstract Factory is often implemented with Factory Methods Factory Methods are usually called within the Template pattern Prototypes do not require sub-classing Creator but they often require an initialize operation» Factory Method does not require an initialze operation Factory-13

Factory Method in Java API The routine getgraphics() on Java Component API obtains a Graphics object that draws on the specific Component object to which getgraphics() belongs. Box is a Container class that can be created as a horizontal or vertical container.» public static Box createhorizontalbox()» public static Box createverticalbox() Factory-14