CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.

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.
Plab – Tirgul 12 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.
Nov, 1, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and its.
Design Patterns I 1. Creational Pattern Singleton: intent and structure Ensure a class has one instance, and provide a global point of access to it 2.
Prototype Pattern Intent:
Oct, 16, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and.
1 Creational Patterns CS : Software Design Winter /T8.
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Software Components Creational Patterns.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
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.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
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.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
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.
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.
SOFTWARE DESIGN Design Patterns 1 6/14/2016Computer Science Department, TUC-N.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns: MORE Examples
Abstract Factory Pattern
Design Pattern Catalogues
Factory Patterns 1.
Design Patterns Lecture 1
Software Design and Architecture
object oriented Principles of software design
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Presented by Igor Ivković
More Design Patterns 1.
More Design Patterns 1.
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
Object Oriented Design Patterns - Creational Patterns
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Lesson 5: More on Creational Patterns
Creational Patterns.
Informatics 122 Software Design II
Presented by Igor Ivković
Presentation transcript:

CDP-1 9. Creational Pattern

CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed –represented Encapsulates knowledge about which concrete classes the system uses Hides how instances of these classes are created and put together

CDP-3 Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes

CDP-4 Example that would benefit from Abstract Factory BuildComputer(ComputerModelA& comp) {comp.Add(new MemoryTypeA); comp.Add(new CPUTypeA); comp.Add(new ModemTypeA); } ComputerModelA MemoryType A CPUTypeAModemTypeA What if I want to build a Computer of Model B with Model B Memory,CPU and Modem?

CDP-5 Using Abstract Factory ComputerFactory createComputer() createMemory() createCPU() createModem() CompFactoryB createComputer() createMemory() createCPU() createModem() CompFactoryA createComputer() createMemory() createCPU() createModem() Computer ModelA Computer ModelB Computer Memory ModelA Memory ModelB Memory Client

CDP-6 BuildComputer(Computer& comp, ComputerFactory& compFactory) { comp.Add(compFactory.createMemory()) ; comp.Add(compFactory.createCPU()); comp.Add(compFactory.createModem()); } Using Abstract Factory...

CDP-7 When to use Abstract Factory? Use Abstract Factory when: –system should be independent of how its products are created, composed and represented –system should be configured with one of multiple families of products –a family of related product objects must be used together and this constraint need to be enforced –you want to reveal only the interface, and not the implementation, of a class library of products

CDP-8 Structure AbstractFactory createProdA() createProdB() ConcreateFactory1 createProdA() createProdB() ProdA1 ProdA2 Abstract ProductA ProdB1 ProdB2 Abstract ProductB Client ConcreateFactory1 createProdA() createProdB()

CDP-9 Consequences of using Abstract Factory Isolates concrete classes Makes exchanging products families easy Promotes consistency among products Supporting new kinds of products is difficult

CDP-10 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. Also known as Virtual Constructor

CDP-11 We want to develop a framework of a Computer that has memory, CPU and Modem. The actual memory, CPU, and Modem that is used depends on the actual computer model being used. We want to provide a configure function that will configure any computer with appropriate parts. This function must be written such that it does not depend on the specifics of a computer model or the components. Example that would benefit from Factory Method

CDP-12 Example using Factory Method Memory MemoryA Computer ComputerA createMemory() createCPU() Configure() Memory* mem =createMemory(); add (mem); CPU* cpu = createCPU(); add(cpu); createMemory() createCPU() return new MemoryA; return new CPUA;

CDP-13 When to use Factory Method? 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

CDP-14 Structure Product concreateProduct Creator ConcreteCreator FactoryMethod() anoperation() Product = FactoryMethod(); FactoryMethod return newConcreateProduct;

CDP-15 Consequences of using Factory Method Provides hooks for subclasses Connects parallel class hierarchies

CDP-16 Factory Method Vs. Other Pattern Abstract Factory often implemented with Factory Method Factory Methods usually called within Template Methods Prototypes don’t require subclassing the Creator. However, they often require initialize operation on the Product class. Factory Method doesn’t require such an operation Proliferation of subclasses

CDP-17 Builder Pattern Separate the construction of a complex object from its representation so that the same construction process can create different representations

CDP-18 A computer has memory and CPU. Building a Computer has two steps - building memory & building the CPU. However, one model has one memory unit & one CPU. Another has four memory units & two CPUs. How to write the code such that the same code can be used to build the two models of computers, & does not change if new models of computers are introduced. Example that would benefit from Builder Pattern

CDP-19 Example using Builder Pattern class ComputerBuilder{... makeComputer(); buildMemory(); buildCPU();}; class SimpleComputerBuilder : public ComputerBuilder { … makeComputer(); buildMemory(); buildCPU(){comp->addCPU(new PROC);} };

CDP-20 Example using Builder Pattern... class SuperComputerBuilder : public ComputerBuilder {... getNumberOfProcessors(); makeComputer(); buildMemory(); buildCPU(){ comp->addCPU(new PROC); } };

CDP-21 Example using Builder Pattern... Computer* CreateComputer( ComputerBuilder& compBldr) { compBldr.makeComputer(); compBldr.buildMemory(); compBldr.buildCPU(); return bldr.getComputer(); }

CDP-22 Example using Builder Pattern... SimpleComputerBuilder bldr; Computer* comp = CreateComputer(bldr ); …. SuperComputerBuilder bldr; Computer* comp = CreateComputer(bldr); … bldr.getNumberOfProcessors();

CDP-23 When to use Builder Pattern? Algorithm for creating complex object should be independent of the parts that make up the object and how they are assembled Construction process must allow different representation for the object that is constructed

CDP-24 Structure Director Construct() Builder buildPart() Concreate Builder buildPart() getResult() Product For all objects in structure { builder->buildPart(); } builder

CDP-25 Consequences of using Builder Lets product’s internal representation to vary Isolates code for construction & representation Gives finer control over construction process

CDP-26 Builder Vs. Other Patterns Builder takes care of complete creation of a complex product step by step Abstract factory emphasizes creation of families of products - one component at a time Builder builds a Composite

CDP-27 Prototype Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype

CDP-28 There are several types of Memory available. A computer may have Memory Model I or Memory Model II (or any other model that may be created later). If I want a Computer that is equivalent to a given Computer, how do I create it? Example that would benefit from Prototype Pattern ComputerMemory Memory I Memory II

CDP-29 Computer ( const Computer& otherComp) { memory = new MemoryI; *memory = *(otherComp.Memory); // Copy characteristics of the other // Computer’s Memory } What if other Computer had Memory Model II? Example that would benefit from Prototype Pattern...

CDP-30 Example using Prototype Pattern ComputerMemory Memory I Memory II Memory* Clone() Memory* mem = new MomoryII; *mem = *this; return mem; Memory* Clone() Computer (const Computer& otherComp) {memory = otherComp->memory.clone();}

CDP-31 When to use Prototype Pattern? System should be independent of how its products are created, composed and represented and the classes to instantiated are specified at runtime you want to avoid creating class hierarchy of factories that parallel the class hierarchy of products

CDP-32 Structure Client Operation() Prototype clone() Concreate Prototype1 clone() p = prototype->clone() prototpye Return copy of self

CDP-33 Another Example of Prototype An application that stores objects to disk Making the load method extensible for new types of classes being added to the system

CDP-34 Consequences of using Prototype Adding & removing products at run-time Specifying new objects by varying values Specifying new objects by varying structure Reduced subclassing configuring application with classes dynamically Each subclass must implement clone operation

CDP-35 Prototype Vs. Other Patterns Abstract Factory is a competing Pattern, however, may work together as well Composite & Decorator benefit from Prototype

CDP-36 Singleton Pattern Ensure a class only has one instance, and provide a global point of access to it

CDP-37 Example that would benefit from Singleton Pattern An application uses a Database Manager. It needs only one Database Manager object. It must not allow the creation of more than one object of this class.

CDP-38 Example that uses Singleton Pattern class DBMgr { static DBMgr* pMgr; private: DBMgr() { }// No way to create outside of this Class public: static DBMgr* getDBMgr() // Only way to create. {if (pMgr == 0) pMgr = new DBMgr; return pMgr; } }; DBMgr* DBMgr::pMgr = 0; Usage: DBMgr* dbmgrPtr = DBMgr::getDBMgr(); //Created first time called

CDP-39 When to use Singleton Pattern There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code

CDP-40 Structure Singleton static Instance() SingletonOperation() GetSingletonData() static uniqueInstance singletonData return uniqueInstance;

CDP-41 Consequences of using Singleton Controlled access to sole instance Reduced name space Permits refinement of operations & representation Permits a variable number of instances More flexible than class operations

CDP-42 Singleton Vs. Other Patterns Several patterns are implemented using Singleton Pattern. For instance AbstractFactory needs a singleton pattern for single instance of the factory