Slide #1.

Chapter 3 – Page 1 Creational Patterns
More slides like this


Slide #2.

Chapter 3 – Page 2 Creational Pattern: If an application needs to be portable, it Abstract needs to encapsulateFactory platform dependencies. 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.
More slides like this


Slide #3.

AbstractFactory Client create platform objects directly, they create abstract objects and utilize an abstract factory as an interface for createProductA() createProductB() AbstractProductA ConcreteFactory1 createProductA() createProductB() ProductA2 ProductA1 AbstractProductB ProductB2 ProductB1 ConcreteFactory2 createProductA() createProductB() Chapter 3 – Page 3 The Abstract Rather than Factory Pattern having clients
More slides like this


Slide #4.

The AnimalWorld client uses the abstract ContinentFactory to produce abstract Herbivore and Carnivore products. 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. ContinentFactory AnimalWorld createHerbivore() createCarnivore() Herbivore AfricaFactory createWildebeest() createLion() Bison Wildebeest Carnivore Wolf Lion AmericaFactory createBison() createWolf() Chapter 3 – Page 4 Non-Software Example
More slides like this


Slide #5.

The client uses the abstract ShapeFactory to produce abstract curved and straight shapes. 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. ShapeFactory Client createCurvedInstance() createStraightInstance() CurvedShape SimpleShapeFactory createCircle() createSquare() Ellipse Circle StraightShape Rectangle Square RobustShapeFactory createEllipse() createRectangle() Chapter 3 – Page 5 Primitive Software Example
More slides like this


Slide #6.

#include using namespace std; class Shape { public: Shape() { id = total++; } virtual void draw() = 0; protected: int id; static int total; }; 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; } }; 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 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 class Ellipse : public Shape { public: void draw() { cout << "ellipse " << id << ": draw" << endl; } }; class Rectangle : public Shape { public: void draw() { cout << "rectangle " << id << ": draw" << endl; } }; 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(); } Chapter 3 – Page 6 Shape Abstract Factory C++ Code
More slides like this


Slide #7.

#include using namespace std; #define MOTIF class Widget { public: virtual void draw() = 0; }; 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(); } class MotifButton : public Widget { public: void draw() { cout << "MotifButton\n"; } }; class MotifMenu : public Widget { public: void draw() { cout << "MotifMenu\n"; } }; 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(); } class WindowsButton : public Widget { public: void draw() { cout << "WindowsButton\n"; } }; class WindowsMenu : public Widget { public: void draw() { cout << "WindowsMenu\n"; } }; void main() { #ifdef MOTIF Widget* w = new MotifButton; #else // WINDOWS Widget* w = new WindowsButton; #endif w->draw(); display_window_one(); display_window_two(); } Chapter 3 – Page 7 Multi-Platform PreAbstract Factory
More slides like this


Slide #8.

#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(); Chapter 3 – Page 8 Multi-Platform w/Abstract Factory
More slides like this


Slide #9.

• 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. Chapter 3 – Page 9 Abstract Factory Design Advantages
More slides like this