Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.

Similar presentations


Presentation on theme: "Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and."— Presentation transcript:

1 Design Patterns Creational Patterns

2 Abstract the instantiation process Help make the system independent of how its objects are created, composed and represented 5 design patterns Abstract the instantiation process Help make the system independent of how its objects are created, composed and represented 5 design patterns

3 Abstract Factory Intent –Provide an interface for creating families of related or dependent objects without specifying their concrete classes Also Known As –Kit Motivation –Write a toolkit to allow applications with GUI interfaces to run on multiple platforms (Mac, PC, OS/2, Unix motif ) Intent –Provide an interface for creating families of related or dependent objects without specifying their concrete classes Also Known As –Kit Motivation –Write a toolkit to allow applications with GUI interfaces to run on multiple platforms (Mac, PC, OS/2, Unix motif )

4 Applicability –use the Abstract Factory when: a system should be independent of how its products are created, composed and represented a system should be configured with one of multiple families of products A family of related product objects is designed to be used together, and you need to enforce this constraint You want to provide a class library of products, and you want to reveal just their interfaces, not their implementation Applicability –use the Abstract Factory when: a system should be independent of how its products are created, composed and represented a system should be configured with one of multiple families of products A family of related product objects is designed to be used together, and you need to enforce this constraint You want to provide a class library of products, and you want to reveal just their interfaces, not their implementation

5 Structure

6 Participants –AbstractFactory - declares an interface for operations that create abstract products –ConcreteFactory - implements the operations to create concrete product objects –AbstractProduct - declares an interface for a type of product object –Product - defines a product object to be created by the corresponding concrete factory, implements the AbstractProduct interface –Client - uses interfaces declared by AbstractFactory and AbstractProduct classes Participants –AbstractFactory - declares an interface for operations that create abstract products –ConcreteFactory - implements the operations to create concrete product objects –AbstractProduct - declares an interface for a type of product object –Product - defines a product object to be created by the corresponding concrete factory, implements the AbstractProduct interface –Client - uses interfaces declared by AbstractFactory and AbstractProduct classes

7 Collaborations –Normally a single instance of a ConcreteFactory class is created at run-time –This concrete factory creates product objects having a particular implementation –To create different product objects, clients should use a different concrete factory Consequences –It isolates concrete classes –It makes exchanging product families easy –It promotes consistency among products –Supporting new kinds of products is difficult Collaborations –Normally a single instance of a ConcreteFactory class is created at run-time –This concrete factory creates product objects having a particular implementation –To create different product objects, clients should use a different concrete factory Consequences –It isolates concrete classes –It makes exchanging product families easy –It promotes consistency among products –Supporting new kinds of products is difficult

8 Implementation & sample code abstract classes inheritance see code.... abstract classes inheritance see code....

9 Builder Separate the construction of a complex object from its representation so that the same construction process can create different representations Use the Builder pattern when: –the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled –the construction process must allow different representations for the object that's constructed Separate the construction of a complex object from its representation so that the same construction process can create different representations Use the Builder pattern when: –the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled –the construction process must allow different representations for the object that's constructed

10

11

12 Factory Method Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method is similar to Abstract Factory but without the emphasis on families Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method is similar to Abstract Factory but without the emphasis on families

13 Factory Method

14 Prototype Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype Use the Prototype pattern: –when the classes to instantiate are specified at run- time –to avoid building a class hierarchy of factories that parallels the class hierarchy of products –when instances of a class can have one of only a few different combinations of state. It may be easier to have the proper number of prototypes and clone them rather than instantiating the class manually each time Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype Use the Prototype pattern: –when the classes to instantiate are specified at run- time –to avoid building a class hierarchy of factories that parallels the class hierarchy of products –when instances of a class can have one of only a few different combinations of state. It may be easier to have the proper number of prototypes and clone them rather than instantiating the class manually each time

15 Prototype

16 Singleton We want to have only one instance of an object in the system Examples: –Printer Spooler –Window Manager We want to have only one instance of an object in the system Examples: –Printer Spooler –Window Manager

17 Solution 1 - Global Variable external DB* theDB; DB* theDB; int main() { theDB = new DB(); //… } db.h db.cpp main.cpp

18 Problems How do we make sure only one instance exists ? What if one global is using another ? What if we want to have 0..4 instances alive ? How do we make sure only one instance exists ? What if one global is using another ? What if we want to have 0..4 instances alive ?

19 The solution: A Singleton!


Download ppt "Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and."

Similar presentations


Ads by Google