Abstract factory creational design pattern Creational design patterns
Abstract factory creational design pattern
Creational design patterns abstract the instantiation process. make a system independent of how its objects are created, composed, and represented. Main goal is: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Motif & presentation manager (as 2 different look &feel standards )
Structure
Participants in this structure(1) Abstract. Factory (Widget. Factory) declares an interface for operations that create abstract product objects. Concrete. Factory (Motif & PMWidget. Factory) implements the operations to create concrete product objects.
Participants in this structure(2) Abstract. Product (Window, Scroll. Bar) declares an interface for a type of product object. Concrete. Product (Motif. Window, Motif. Scroll. Bar) 1 - defines a product object to be created by the corresponding concrete factory. 2 -implements the Abstract. Product interface. Client uses only the interfaces declared by Abstract. Factory and Abstract. Product
Applicability a system should be independent of how its products are created. 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 implementations.
Consequences(1) It isolates concrete classes. ◦ It isolates clients from implementation classes. Clients manipulate instances through their abstract interfaces. It makes exchanging product families easy. ◦ It can use different product configurations simple by changing the concrete factory.
Consequences(2) It promotes consistency among products ◦ To enforce that an application use objects from only one family at a time. Supporting new kinds of products is difficult Adding a new product requires extending the abstract interface which implies that all of its derived concrete classes also must change. Essentially everything must change to support and use the new product family abstract factory interface is extended derived concrete factories must implement the extensions a new abstract product class is added a new product implementation is added client has to be extended to use the new product
Sample Code(1) Abstract Factory pattern to creating mazes class Maze. Factory { public: Maze. Factory(); virtual Maze * Make. Maze() const; virtual Wall * Make. Wall() const; virtual Room * Make. Room(int n) const; virtual Door* Make. Door(Room *r 1, Room* r 2) const; }
Sample Code(2) Create. Maze taking a Maze. Factory as a parameter Maze *Maze. Game: : Create. Maze(Maze. Factory& factory) { Maze * a. Maze = factory. Make. Maze(); Room * r 1 = factory. Make. Room(1); Room *r 2 = factory. Make. Room(2); … … }
Sample Code(3) Enhanted. Maze. Factory class Enchanted. Maze. Factory: public Maze. Factory { public: Enchanted. Maze. Factory(); virtual Room* Make. Room(int n) const { return new Enchanted. Room(n, Cast. Spell()); virtual Door* Make. Door(Room *r 1, Room* r 2) const { return new Door. Needing. Spell(r 1, r 2); } protected: Spell* Cast. Spell() const; } }
Sample Code(4) Bombed. Maze. Factory Wall * Bombed. Maze. Factory: : Make. Wall() const { return new Bombed. Wall; } Room *Bombed. Maze. Factory: : Make. Room(int n) const { return new Room. With. ABomb(n); }
Sample Code(5) Code using Bombed. Maze. Factory Maze. Game game; Bombed. Maze. Factory factory; game. Create. Maze(factory); Create. Maze can take an instance of Enchanted. Maze. Factory just as well to build enchanted mazes.
Example interface GUIFactory { public Button create. Button(); } class Win. Factory implements GUIFactory { public Button create. Button() { return new Win. Button(); } } class OSXFactory implements GUIFactory { public Button create. Button() { return new OSXButton(); } }
Example(cont. ) interface Button { public void paint(); } class Win. Button implements Button { public void paint() { System. out. println("I'm a Win. Button"); } } class OSXButton implements Button { public void paint() { System. out. println("I'm an OSXButton"); } }
Example(cont. ) class Application { public Application(GUIFactory factory) { Button button = factory. create. Button(); button. paint(); } } public class Application. Runner { public static void main(String[] args) { new Application(create. Os. Specific. Factory()); } } public static GUIFactory create. Os. Specific. Factory() { int sys = read. From. Config. File("OS_TYPE"); if (sys == 0) { return new Win. Factory(); } else { return new OSXFactory(); } }
- Slides: 17