Download presentation
Presentation is loading. Please wait.
Published byMaximilian Stokes Modified over 8 years ago
2
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation, trying to create objects in a manner that is suitable to the situation Abstract Factory Intent: Provide an interface for creating families of related objects without specifying their concrete classes Builder Intent: Separate the construction of a complex object from its representation so that the same construction process can create different representations Factory Method Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate Prototype Intent: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype Singleton Intent: Ensure that a class has only one instance, and provide a global point of access to it
3
Creational Pattern: Abstract Factory If an application needs to be portable, it needs to encapsulate platform dependencies. C h a p t e r 3 – P a g e 15 These platforms might include the operating system, the database management system, the windowing system, the web browser, etc. Frequently, this encapsulation isn’t engineered in advance, resulting in a massive proliferation of #ifdef case statements with options for all currently supported platforms.
4
The Abstract Factory Pattern Rather than having clients create platform objects directly, they create abstract objects and utilize an abstract factory as an interface for producing the concrete objects. C h a p t e r 3 – P a g e 16
5
Non-Software Example The AnimalWorld client uses the abstract ContinentFactory to produce abstract Herbivore and Carnivore products. C h a p t e r 3 – P a g e 17 When the concrete platform is an AfricaFactory, concrete Wildebeest and Lion objects are created, while when the concrete platform is an AmericaFactory, concrete Bison and Wolf objects are created.
6
Primitive Software Example The client uses the abstract ShapeFactory to produce abstract curved and straight shapes. C h a p t e r 3 – P a g e 18 If the platform has been established as “simple”, concrete Circle and Square objects are created, while if the platform has been established as “robust”, concrete Ellipse and Rectangle objects are created.
7
Shape Abstract Factory C++ Code C h a p t e r 3 – P a g e 19 #include using namespace std; class Shape { public: Shape() { id = total++; } virtual void draw() = 0; protected: int id; static int total; }; int Shape::total = 0; class Circle : public Shape { public: void draw() { cout << "circle " << id << ": draw" << endl; } }; class Square : public Shape { public: void draw() { cout << "square " << id << ": draw" << endl; } }; class Ellipse : public Shape { public: void draw() { cout << "ellipse " << id << ": draw" << endl; } }; class Rectangle : public Shape { public: void draw() { cout << "rectangle " << id << ": draw" << endl; } }; class Factory { public: virtual Shape* createCurvedInstance() = 0; virtual Shape* createStraightInstance() = 0; }; class SimpleShapeFactory : public Factory { public: Shape* createCurvedInstance() { return new Circle; } Shape* createStraightInstance() { return new Square; } }; class RobustShapeFactory : public Factory { public: Shape* createCurvedInstance() { return new Ellipse; } Shape* createStraightInstance() { return new Rectangle; } }; void main() { #ifdef SIMPLE Factory* factory = new SimpleShapeFactory; #else Factory* factory = new RobustShapeFactory; #endif Shape* shapes[3]; shapes[0] = factory->createCurvedInstance(); shapes[1] = factory->createStraightInstance(); shapes[2] = factory->createCurvedInstance(); for (int i=0; i < 3; i++) shapes[i]->draw(); }
8
Multi-Platform Pre-Abstract Factory C h a p t e r 3 – P a g e 20 #include using namespace std; #define MOTIF class Widget { public: virtual void draw() = 0; }; class MotifButton : public Widget { public: void draw() { cout << "MotifButton\n"; } }; class MotifMenu : public Widget { public: void draw() { cout << "MotifMenu\n"; } }; class WindowsButton : public Widget { public: void draw() { cout << "WindowsButton\n"; } }; class WindowsMenu : public Widget { public: void draw() { cout << "WindowsMenu\n"; } }; void display_window_one() { #ifdef MOTIF Widget* w[] = { new MotifButton, new MotifMenu }; #else // WINDOWS Widget* w[] = { new WindowsButton, new WindowsMenu }; #endif w[0]->draw(); w[1]->draw(); } void display_window_two() { #ifdef MOTIF Widget* w[] = { new MotifMenu, new MotifButton }; #else // WINDOWS Widget* w[] = { new WindowsMenu, new WindowsButton }; #endif w[0]->draw(); w[1]->draw(); } void main() { #ifdef MOTIF Widget* w = new MotifButton; #else // WINDOWS Widget* w = new WindowsButton; #endif w->draw(); display_window_one(); display_window_two(); }
9
Multi-Platform w/Abstract Factory C h a p t e r 3 – P a g e 21 #include using namespace std; #define WINDOWS class Widget { public: virtual void draw() = 0; }; class MotifButton : public Widget { public: void draw() { cout << "MotifButton\n"; } }; class MotifMenu : public Widget { public: void draw() { cout << "MotifMenu\n"; } }; class WindowsButton : public Widget { public: void draw() { cout << "WindowsButton\n"; } }; class WindowsMenu : public Widget { public: void draw() { cout << "WindowsMenu\n"; } }; class Factory { public: virtual Widget* create_button() = 0; virtual Widget* create_menu() = 0; }; class MotifFactory : public Factory { public: Widget* create_button() { return new MotifButton; } Widget* create_menu() { return new MotifMenu; } }; class WindowsFactory : public Factory { public: Widget* create_button() { return new WindowsButton; } Widget* create_menu() { return new WindowsMenu; } }; Factory* factory; void display_window_one() { Widget* w[] = { factory->create_button(), factory->create_menu() }; w[0]->draw(); w[1]->draw(); } void display_window_two() { Widget* w[] = { factory->create_menu(), factory->create_button() }; w[0]->draw(); w[1]->draw(); } void main() { #ifdef MOTIF factory = new MotifFactory; #else // WINDOWS factory = new WindowsFactory; #endif Widget* w = factory->create_button(); w->draw(); display_window_one(); display_window_two(); }
10
Abstract Factory Design Advantages C h a p t e r 3 – P a g e 22 Since the factory only provides an abstract pointer, the client doesn’t need to include any header files or class declarations relating to the concrete type. Objects of a concrete type are created by the factory, but the client accesses such objects only through their abstract interface. Adding new concrete types is done by modifying the client code to use a different factory (usually a one-line change in the code). This factory creates objects of a different concrete type, but still returns a pointer of the same abstract type as before, insulating the client from change, eliminating the need to change every location in the code where a new object is created.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.