cdp-1 9. creational pattern. cdp-2 creational patterns abstracts instantiation process makes system...

42
CDP-1 Venkat Subramaniam Venkat Subramaniam 9. Creational Pattern

Upload: sophie-melton

Post on 05-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

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

CDP-1Venkat SubramaniamVenkat Subramaniam

9. Creational Pattern

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

CDP-2Venkat SubramaniamVenkat Subramaniam

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

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

CDP-3Venkat SubramaniamVenkat Subramaniam

Abstract Factory

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

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

CDP-4Venkat SubramaniamVenkat Subramaniam

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 CPUTypeA ModemTypeA

What if I want to build a Computer of Model B with Model B Memory,CPU and Modem?

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

CDP-5Venkat SubramaniamVenkat Subramaniam

Using Abstract FactoryComputerFactory

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

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

CDP-6Venkat SubramaniamVenkat Subramaniam

BuildComputer(Computer& comp, ComputerFactory& compFactory)

{comp.Add(compFactory.createMemory());comp.Add(compFactory.createCPU());comp.Add(compFactory.createModem());

}

Using Abstract Factory...

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

CDP-7Venkat SubramaniamVenkat Subramaniam

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

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

CDP-8Venkat SubramaniamVenkat Subramaniam

StructureAbstractFactory

createProdA()

createProdB()

ConcreateFactory1

createProdA()

createProdB()

ProdA1 ProdA2

Abstract ProductA

ProdB1 ProdB2

Abstract ProductB

Client

ConcreateFactory1

createProdA()

createProdB()

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

CDP-9Venkat SubramaniamVenkat Subramaniam

Consequences of using Abstract Factory

• Isolates concrete classes

• Makes exchanging products families easy

• Promotes consistency among products

• Supporting new kinds of products is difficult

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

CDP-10Venkat SubramaniamVenkat Subramaniam

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

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

CDP-11Venkat SubramaniamVenkat Subramaniam

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

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

CDP-12Venkat SubramaniamVenkat Subramaniam

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;

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

CDP-13Venkat SubramaniamVenkat Subramaniam

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

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

CDP-14Venkat SubramaniamVenkat Subramaniam

Structure

Product

concreateProduct

Creator

ConcreteCreator

FactoryMethod() anoperation()

Product = FactoryMethod();

FactoryMethod return newConcreateProduct;

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

CDP-15Venkat SubramaniamVenkat Subramaniam

Consequences of using Factory Method

• Provides hooks for subclasses

• Connects parallel class hierarchies

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

CDP-16Venkat SubramaniamVenkat Subramaniam

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

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

CDP-17Venkat SubramaniamVenkat Subramaniam

Builder Pattern

Separate the construction of a complex object from its representation so that the same construction process can create different representations

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

CDP-18Venkat SubramaniamVenkat Subramaniam

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

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

CDP-19Venkat SubramaniamVenkat Subramaniam

Example using Builder Patternclass ComputerBuilder{...

makeComputer();buildMemory();buildCPU(); };

class SimpleComputerBuilder : public ComputerBuilder { …

makeComputer();buildMemory();buildCPU(){comp->addCPU(new PROC);}

};

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

CDP-20Venkat SubramaniamVenkat Subramaniam

Example using Builder Pattern...class SuperComputerBuilder

: public ComputerBuilder {...getNumberOfProcessors();makeComputer();buildMemory();buildCPU(){

comp->addCPU(new PROC); comp->addCPU(new PROC); }

};

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

CDP-21Venkat SubramaniamVenkat Subramaniam

Example using Builder Pattern...

Computer* CreateComputer(ComputerBuilder& compBldr)

{compBldr.makeComputer();compBldr.buildMemory(); compBldr.buildCPU();return bldr.getComputer();

}

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

CDP-22Venkat SubramaniamVenkat Subramaniam

Example using Builder Pattern...

SimpleComputerBuilder bldr;

Computer* comp = CreateComputer(bldr);

….

SuperComputerBuilder bldr;Computer* comp =

CreateComputer(bldr);…bldr.getNumberOfProcessors();

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

CDP-23Venkat SubramaniamVenkat Subramaniam

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

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

CDP-24Venkat SubramaniamVenkat Subramaniam

Structure

Director

Construct()

Builder

buildPart()

Concreate Builder

buildPart() getResult()

ProductFor all objects in structure {

builder->buildPart();

}

builder

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

CDP-25Venkat SubramaniamVenkat Subramaniam

Consequences of using Builder

• Lets product’s internal representation to vary

• Isolates code for construction & representation

• Gives finer control over construction process

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

CDP-26Venkat SubramaniamVenkat Subramaniam

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

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

CDP-27Venkat SubramaniamVenkat Subramaniam

Prototype Pattern

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype

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

CDP-28Venkat SubramaniamVenkat Subramaniam

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

Computer Memory

Memory I Memory II

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

CDP-29Venkat SubramaniamVenkat Subramaniam

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...

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

CDP-30Venkat SubramaniamVenkat Subramaniam

Example using Prototype Pattern

Computer Memory

Memory I Memory II

Memory* Clone()

Memory* Clone()

Memory* mem = new MomoryII; *mem = *this; return mem;

Memory* Clone()

Computer (const Computer& otherComp){ memory = otherComp->memory.clone();}

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

CDP-31Venkat SubramaniamVenkat Subramaniam

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

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

CDP-32Venkat SubramaniamVenkat Subramaniam

Structure

Client

Operation()

Prototype

clone()

Concreate Prototype1

clone()

p = prototype->clone()

prototpye

Return copy of self

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

CDP-33Venkat SubramaniamVenkat Subramaniam

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

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

CDP-34Venkat SubramaniamVenkat Subramaniam

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

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

CDP-35Venkat SubramaniamVenkat Subramaniam

Prototype Vs. Other Patterns

• Abstract Factory is a competing Pattern, however, may work together as well

• Composite & Decorator benefit from Prototype

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

CDP-36Venkat SubramaniamVenkat Subramaniam

Singleton Pattern

Ensure a class only has one instance, and provide a global point of access to it

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

CDP-37Venkat SubramaniamVenkat Subramaniam

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.

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

CDP-38Venkat SubramaniamVenkat Subramaniam

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

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

CDP-39Venkat SubramaniamVenkat Subramaniam

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

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

CDP-40Venkat SubramaniamVenkat Subramaniam

Structure

Singleton

static Instance() SingletonOperation() GetSingletonData()

static uniqueInstance singletonData

return uniqueInstance;

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

CDP-41Venkat SubramaniamVenkat Subramaniam

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

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

CDP-42Venkat SubramaniamVenkat Subramaniam

Singleton Vs. Other Patterns

• Several patterns are implemented using Singleton Pattern. For instance AbstractFactory needs a singleton pattern for single instance of the factory