Abstract Factory Stphane Ducasse Stephane Ducasseunivsavoie fr http
Abstract Factory Stéphane Ducasse Stephane. Ducasse@univ-savoie. fr http: //www. listic. univ-savoie. fr/~ducasse/ S. Ducasse 1
License: CC-Attribution-Share. Alike 2. 0 http: //creativecommons. org/licenses/by-sa/2. 0/ S. Ducasse 2
Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes Also known as: Kit S. Ducasse 3
Abstract Factory Intent • S. Ducasse Provide an interface for creating families of related or dependent objects without specifying their concrete classes 4
Abstract Factory Motivation You have an application with different looks and feels. How to avoid to hardcode all the specific widget classes into the code so that you can change from Motifs to Mac. Os. X? S. Ducasse 5
Abstract Factory Motivation Abstract factory introduce an interface for creating each basic kind of widget S. Ducasse 6
Abstract Factory Applicability 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 reveal just their interfaces, not their implementations S. Ducasse 7
Abstract Factory Structure S. Ducasse 8
Abstract Factory Participants Abstract. Factory (Widget. Factory) declares an interface for operations that create abstract product objects Concrete. Factory (Motif. Widget. Factory, PMWidget. Factory) implements the operations to create concrete product objects S. Ducasse 9
Abstract Factory Participants Abstract. Product (Window, Scroll. Bar) defines a product object to be created by the corresponding concrete factory implements the Abstract. Product interface Client uses only interfaces declared by Abstract. Factory and Abstract. Product classes S. Ducasse 10
Implementation: Specifying the Factory Maze. Game class>>create. Maze. Factory ^ (Maze. Factory new add. Part: Wall named: #wall; add. Part: Room named: #room; add. Part: Door named: #door; yourself) Enchanted. Maze. Game class>>create. Maze. Factory ^ (Maze. Factory new add. Part: Wall named: #wall; add. Part: Enchanted. Room named: #room; add. Part: Door. Needing. Spell named: #door; yourself) S. Ducasse 11
Single. Factory Maze. Game class>>create. Maze. Factory ^ (Maze. Factory new add. Part: Wall named: #wall; add. Part: Room named: #room; add. Part: Door named: #door; yourself) Maze. Game class>>create. Enchanted. Maze. Factory ^ (Maze. Factory new add. Part: Wall named: #wall; add. Part: Enchanted. Room named: #room; add. Part: Door. Needing. Spell named: #door; yourself) S. Ducasse 12
Implementation: Using the Factory Maze. Factory>>create. Maze: a. Factory | room 1 room 2 a. Door | room 1 : = (a. Factory make: #room) number: 1. room 2 : = (a. Factory make: #room) number: 2. a. Door : = (a. Factory make: #door) from: room 1 to: room 2. room 1 at. Side: #north put: (a. Factory make: #wall). room 1 at. Side: #east put: a. Door. . room 2 at. Side: #south put: (a. Factory make: #wall). room 2 at. Side: #west put: a. Door. ^ Maze new add. Room: room 1; add. Room: room 2; yourself Maze. Factory>>make: part. Name ^ (part. Catalog at: part. Name) new S. Ducasse 13
Abstract Factory Collaborations Normally a single instance of Concrete. Factory is created at run-time Abstract. Factory defers creation of product objects to its Concrete. Factory subclass S. Ducasse 14
Consequences It isolates concrete classes It makes exchanging product families easy It promotes consistency among products Supporting new kinds of products is difficult (set of products is somehow fixed) The class factory “controls” what is created S. Ducasse 15
Using Prototypes The concrete factory stores the prototypes to be cloned in a dictionary called part. Catalog. make: part. Name ^ (part. Catalog at: part. Name) copy The concrete factory has a method for adding parts to the catalog. add. Part: part. Template named: part. Name part. Catalog at: part. Name put: part. Template Prototypes are added to the factory by identifying them with a symbol: S. Ducasse 16 a. Factory add. Part: a. Prototype named: #ACMEWidget
In Relations Builder and Abstract Factory are closely related But Builder is in charge of assembling parts Abstract. Factory is responsible of producing parts that work together S. Ducasse 17
Known Uses Visual. Works UILook. Policy S. Ducasse 18
- Slides: 18