1 / 9

Creational Patterns

Creational Patterns. Chapter 3 – Page 14. Creational Pattern: Abstract Factory. Chapter 3 – Page 15. If an application needs to be portable, it needs to encapsulate platform dependencies.

dino
Download Presentation

Creational Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creational Patterns Chapter 3 – Page 14

  2. Creational Pattern: Abstract Factory Chapter 3 – Page 15 If an application needs to be portable, it needs to encapsulate 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 #ifdefcase statements with options for all currently supported platforms.

  3. The Abstract Factory Pattern Chapter 3 – Page 16 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.

  4. Non-Software Example Chapter 3 – Page 17 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.

  5. Primitive Software Example Chapter 3 – Page 18 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.

  6. Shape Abstract Factory C++ Code Chapter 3 – Page 19 #include <iostream> using namespace std; class Shape { public: Shape() { id = total++; } virtual void draw() = 0; protected: int id; static inttotal; }; 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; }; classSimpleShapeFactory : public Factory { public: Shape* createCurvedInstance() { returnnew Circle; } Shape* createStraightInstance() { return new Square; } }; classRobustShapeFactory : public Factory { public: Shape* createCurvedInstance() { return new Ellipse; } Shape* createStraightInstance() { return new Rectangle; } }; void main() { #ifdefSIMPLE Factory* factory = newSimpleShapeFactory; #else Factory* factory = newRobustShapeFactory; #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(); }

  7. Multi-Platform Pre-Abstract Factory Chapter 3 – Page 20 #include <iostream> using namespace std; #define MOTIF class Widget { public: virtual void draw() = 0; }; classMotifButton : public Widget { public: void draw() { cout << "MotifButton\n"; } }; classMotifMenu : public Widget { public: void draw() { cout << "MotifMenu\n"; } }; classWindowsButton : public Widget { public: void draw() { cout << "WindowsButton\n"; } }; classWindowsMenu : public Widget { public: void draw() { cout << "WindowsMenu\n"; } }; voiddisplay_window_one() { #ifdefMOTIF Widget* w[] = { newMotifButton, newMotifMenu }; #else // WINDOWS Widget* w[] = { newWindowsButton, newWindowsMenu }; #endif w[0]->draw(); w[1]->draw(); } voiddisplay_window_two() { #ifdefMOTIF Widget* w[] = { newMotifMenu, newMotifButton }; #else // WINDOWS Widget* w[] = { newWindowsMenu, newWindowsButton }; #endif w[0]->draw(); w[1]->draw(); } void main() { #ifdefMOTIF Widget* w = new MotifButton; #else // WINDOWS Widget* w = new WindowsButton; #endif w->draw(); display_window_one(); display_window_two(); }

  8. Multi-Platform w/Abstract Factory Chapter 3 – Page 21 #include <iostream> using namespace std; #define WINDOWS class Widget { public: virtual void draw() = 0; }; classMotifButton : public Widget { public: void draw() { cout << "MotifButton\n"; } }; classMotifMenu : public Widget { public: void draw() { cout << "MotifMenu\n"; } }; classWindowsButton : public Widget { public: void draw() { cout << "WindowsButton\n"; } }; classWindowsMenu : public Widget { public: void draw() { cout << "WindowsMenu\n"; } }; class Factory { public: virtual Widget* create_button() = 0; virtual Widget* create_menu() = 0; }; classMotifFactory : public Factory { public: Widget* create_button() { return newMotifButton; } Widget* create_menu() { return new MotifMenu; } }; classWindowsFactory : public Factory { public: Widget* create_button() { return new WindowsButton; } Widget* create_menu() { return new WindowsMenu; } }; Factory* factory; voiddisplay_window_one() { Widget* w[] = { factory->create_button(), factory->create_menu() }; w[0]->draw(); w[1]->draw(); } voiddisplay_window_two() { Widget* w[] = { factory->create_menu(), factory->create_button() }; w[0]->draw(); w[1]->draw(); } void main() { #ifdefMOTIF factory = newMotifFactory; #else // WINDOWS factory = newWindowsFactory; #endif Widget* w = factory->create_button(); w->draw(); display_window_one(); display_window_two(); }

  9. Abstract Factory Design Advantages Chapter 3 – Page 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.

More Related