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.

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

Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,
ITEC200 – Week03 Inheritance and Class Hierarchies.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Template Method By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Abstract Factory Design Pattern making abstract things.
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Software Components Creational Patterns.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Behavioral Pattern: Strategy C h a p t e r 5 – P a g e 205 The Open-Closed Principle advocates designing software in such a way that it will absorb new.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
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.
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.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.
Introduction to Object-Oriented Programming Lesson 2.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Factory Pattern Sanjay Yadav (ISE ).
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation,
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.
Unit II-Chapter No. : 5- design Patterns
Factory Method Pattern
CS 3370 – C++ Object-oriented Programming
Design Patterns C++ Java C#.
Factory Patterns 1.
Creational Pattern: Prototype
Design Patterns C++ Java C#.
Software Design and Architecture
C++ Classes & Object Oriented Programming
Factory Method Pattern
Lecture 23 Polymorphism Richard Gesick.
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Programming II Polymorphism A.AlOsaimi.
Inheritance: Polymorphism and Virtual Functions
Chapter 14 Abstract Classes and Interfaces
Fundaments of Game Design
CS 325: Software Engineering
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

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 allowing the individual applications to define how such objects are created. C h a p t e r 3 – P a g e 31 This makes the design of the objects more customizable without resorting to new classes. Instead, this pattern defines a separate method for creating objects, letting subclasses override the derived type of product that is created.

The Factory Method Pattern The Product is an abstract interface for the objects being created. C h a p t e r 3 – P a g e 32 The ConcreteProduct is the actual implementation of the objects being created. The Creator defines the factory method, returning an object of type Product; it can also define a default implementation of the factory method, returning a default ConcreteProduct object. The ConcreteCreator overrides the Creator’s factory method to return an instance of a ConcreteProduct.

Non-Software Example The Document is the abstract Page creator, instantiating those Page objects in a manner consistent with the particular Document subclass (Resume or Report) that has been established. C h a p t e r 3 – P a g e 33 Report documents will instantiate Title, Text, and Bibliography pages, while Resume documents will instantiate Skills, Education, and Experience pages.

C h a p t e r 3 – P a g e 34 Note the potential compilation benefit here. If we move the abstract Computer into a separate header file with the ComputerFactory, we can then move the implementation of the NewComputer() function (the factory method) into a separate implementation file. That implementation file would be the only one needing knowledge of the derived classes. Thus, if a change is made to any derived class of Computer, or a new Computer type is added, only the implementation file for NewComputer() would need to be recompiled. Computer Factory Method Example Instead of instantiating the computers directly, the client uses the ComputerFactory to generate new computers, specifying whether they should be laptops or desktops during the process.

Computer Factory Method Code C h a p t e r 3 – P a g e 35 #include using namespace std; class Computer { public: virtual void Run() = 0; virtual void Stop() = 0; }; class Laptop: public Computer { public: virtual void Run() { cout << "Laptop Active" << endl; } virtual void Stop() { cout << "Laptop Hibernating" << endl; } }; class Desktop: public Computer { public: virtual void Run() { cout << "Desktop Active" << endl; } virtual void Stop() { cout << "Desktop Off" << endl; } }; class ComputerFactory { public: // This is the factory method, used // instead of a Computer constructor // in order to enable instances of // one of the Computer subclasses to // be returned. static Computer *NewComputer (const string &description) { if (description == "laptop") return new Laptop; if (description == "desktop") return new Desktop; return NULL; } }; void main() { Computer *C1 = ComputerFactory::NewComputer("laptop"); Computer *C2 = ComputerFactory::NewComputer("desktop"); C1->Run(); C2->Run(); C1->Stop(); C2->Stop(); C1->Run(); C2->Stop(); }

Factory Method Design Advantages C h a p t e r 3 – P a g e 36 The Factory Method pattern reduces duplication of code that would result from having separate methods in multiple classes just to perform custom object creation. While the implementation of a Factory Method certainly isn’t simpler than just calling a new operator, it does effectively communicate where instantiation occurs and how it may be overridden. By completely decoupling the client from the implementation details of the derived classes, polymorphic creation is made possible.