Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.

Similar presentations


Presentation on theme: "Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer."— Presentation transcript:

1 Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer

2 Creational Patterns Abstract 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 Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes

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 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 BuildComputer(Computer& comp, ComputerFactory& compFactory) { comp.Add(compFactory.createMemory()); comp.Add(compFactory.createCPU()); comp.Add(compFactory.createModem()); } Using Abstract Factory...

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 Structure AbstractFactory createProdA() createProdB() ConcreteFactory1 createProdA() createProdB() ProdA1 ProdA2 Abstract ProductA ProdB1 ProdB2 Abstract ProductB Client ConcreteFactory1 createProdA() createProdB()

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 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 Want to develop a framework of a Computer that has memory, CPU, modem. Actual memory, CPU, and modem used depends on actual computer model 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 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 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 Structure Product concreateProduct Creator ConcreteCreator FactoryMethod() anoperation() Product = FactoryMethod(); FactoryMethod return newConcreateProduct;

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

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


Download ppt "Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer."

Similar presentations


Ads by Google