Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed."— Presentation transcript:

1 CDP-1 9. Creational Pattern

2 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

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

4 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?

5 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

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

7 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

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

9 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

10 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

11 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

12 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;

13 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

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

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

16 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

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

18 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

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

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

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

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

23 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

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

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

26 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

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

28 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

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

30 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();}

31 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

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

33 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

34 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

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

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

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

38 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

39 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

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

41 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

42 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


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

Similar presentations


Ads by Google