260 likes | 409 Views
The Abstract Factory Pattern. ZHAO Jianhua Dept. of Computer Sci&Tech, Nanjing University. Intent. Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Also Known As Kit. Motivation.
E N D
The Abstract Factory Pattern ZHAO Jianhua Dept. of Computer Sci&Tech, Nanjing University
Intent • Provide an interface for creating families of related or dependent objects without specifying their concrete classes. • Also Known As • Kit
Motivation • Consider a user interface toolkit that supports multiple look-and-feel standards. Different look-and-feels defines different appearances and behaviors for user interface. • An application should not hard-code its widgets for a particular look-and-feel. • Instantiating look-and-feel-specific classes of widgets throughout the application makes it hard to change the look and feel later.
Motivation(2) • We can solve this problem by defining an abstract WidgetFactory class that declares an interface for creating each basic kind of widget. • There is also an abstract class for each kind of widget, and concrete subclasses implement widgets for specific look-and-feel standards.
Motivation(3) • Client call these operations to obtain widget instances, but clients aren’t aware of the concrete classes they are using. • There is a concrete subclass of WidgetFactory for each look-and-feel standard. • A WidgetFactory also enforces dependencies between the concrete widget classes.
WidgetFactory CreateScrollBar() … MotifWidgetFactory PMWidgetFactory CreateScrollBar() … CreateScrollBar() … Motivation(4) Client Window PMWindow MotifWindow ScrollBar PMScrollBar MotifScrollBar
Applicability • Use the Abstract Factory pattern 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 reval just their interfaces, not their implementations.
AbstractFactory CreateProductA() … ConcreteFactory1 ConcreteFactory2 CreateProductA() … CreateProductA() … Structure Client AbProductA ProductA2 ProductA1 AbProductB ProductB2 ProductB1
Participants • AbstractFactory (WidgetFactory) • declares an interface for operations that create abstract product objects. • ConcreteFactory (MotifWidgetFactory, …) • implements the operations to create concrete product objects. • AbstractProduct (Window, ScrollBar) • declare an interface for a type of product object.
Participants(2) • ConcreteProduct(MotifWindow, MotifScrollBar) • defines a product object to be created by the corresponding concrete factory. • implements the AbstractProduct interface. • Client • use only interfaces declared by AbstractFactory and AbstractProduct classes
Collaborations • Normally a single instance of a ConcreteFactory class is created at run-time. • AbstractFactory defers creations of product objects to its ConcreteFactory subclass.
Consequences • 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 • Because the AbstractFactory interface fixes the set of products that can be created. • Supporting new kinds of products requires extending the factory interface, which involves changing the AbstractFactory class.
Implementation • Factories as singletons. • An application typically needs only one instance of a ConcreteFactory per product family.
Implementation(2) • Creating the products • AbstractFactory only declares an interface for creating products. A concrete factory will specify its products by overriding the factory method for each. • Such implementation requires a new concrete factory subclass for each product family, even if the product families differ only slightly.
Implementation(3) • Creating the products(2) • If many product families are possible, the concrete factory can be implemented using the Prototype pattern. • The concrete factory is initialized with a prototypical instance of each product in the family, and it creates a new product by cloning its prototype. • The Prototype-based approach eliminates the need for a new concrete factory class for each new family.
Implementation(4) • Defining extensible factories • The kinds of products are encoded in the operation signatures of AbstractFactory. Adding a new kind of product requires changing the AbstractFactory interface and all the classes that depend on it. • A more flexible but less safe design is to add a parameter to operations that create objects. This parameter specifies the kind of object to be created.
Implementation(5) • Defining extensible factories(2) • This variation is easier to use in a dynamically typed language like Smalltalk than in a statically typed language like C++. • An inherent problem remains: All products are returned to the client with the same abstract interface as given by the return type. The client will not be able to differentiate or make safe assumptions about the class of a product.
Sample Code(1) • Abstract Factory pattern to creating mazes class MazeFactory { public: MazeFactory(); virtual Maze * MakeMaze() const; virtual Wall * MakeWall() const; virtual Room * MakeRoom(int n) const; virtual Door* MakeDoor(Room *r1, Room* r2) const; }
Sample Code(2) • CreateMaze taking a MazeFactory as a parameter Maze *MazeGame::CreateMaze(MazeFactory& factory){ { Maze * aMaze = factory.MakeMaze(); Room * r1 = factory.MakeRoom(1); Room *r2 = factory.MakeRoom(2); … … }
Sample Code(3) • EnhantedMazeFactory class EnchantedMazeFactory: public MazeFactory{ public: EnchantedMazeFactory(); virtual Room* MakeRoom(int n) const { return new EnchantedRoom(n, CastSpell());} virtual Door* MakeDoor(Room *r1, Room* r2) const {return new DoorNeedingSpell(r1,r2);} protected: Spell* CastSpell() const; }
Sample Code(4) • BombedMazeFactory Wall * BombedMazeFactory::MakeWall() const { return new BombedWall; } Room *BombedMazeFactory::MakeRoom(int n) const{ return new RoomWithABomb(n); }
Sample Code(5) • Code using BombedMazeFactory MazeGame game; BombedMazeFactory factory; game.CreateMaze(factory); • CreateMaze can take an instance of EnchantedMazeFactory just as well to build enchanted mazes.
Known Uses • InterViews uses “Kit” to denote AbstractFActory classes • WidgetKit, DialogKit, LayoutKit • ET++ uses the Abstract Factory pattern to achieve protability across different window systems.
Related Patterns • AbstractFactory classes are often implemented with Factory Methods • They can also be implemented using Prototype. • A concrete factory is often a singleton