Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1.

Similar presentations


Presentation on theme: "The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1."— Presentation transcript:

1 The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1

2 Motivation A user interface toolkit needs to support multiple look-and-feel options such as Windows, OS-X, Qt, or GTK+. Different look-and-feels define different appearances and behaviors for user interface “widgets” such as windows, scroll bars, buttons, etc. We need a way to instantiate widgets throughout an application with a specific look-and-feel. Solution: –abstract widget factory that declares an interface for creating each basic kind of widget –abstract class for each kind of widget with concrete implementations for different look-and-feels ©SoftMoore ConsultingSlide 2

3 Motivation (continued) ©SoftMoore ConsultingSlide 3 Client WidgetFactory createScrollbar() createWindow() Window OSXWindowWinWindow Scrollbar OSXScrollbarWinScrollbar WindowsFactory createScrollbar() createWindow() OSXFactory createScrollbar() createWindow()

4 Abstract Factory Pattern Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Also Known As: Kit 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 reveal just their interfaces, not their implementations. Slide 4©SoftMoore Consulting

5 Abstract Factory Pattern (continued) ©SoftMoore ConsultingSlide 5 Client AbstractFactory createProductA() createProductB() Structure AbstractProductA ProductA2ProductA1 AbstractProductB ProductB2ProductB1 ConcreteFactory1 createProductA() createProductB() ConcreteFactory2 createProductA() createProductB()

6 Abstract Factory Pattern (continued) Participants AbstractFactory –declares an interface for operations that create abstract product objects. ConcreteFactory –implements the operations to create concrete product objects. AbstractProduct –declares an interface for a type of product object. Slide 6©SoftMoore Consulting

7 Abstract Factory Pattern (continued) Participants (continued) ConcreteProduct –defines a product object to be created by the corresponding concrete factory. –implements the AbstractProduct interface. Client –uses only interfaces declared by AbstractFactory and AbstractProduct classes. Slide 7©SoftMoore Consulting

8 Abstract Factory Pattern (continued) Collaborations Normally a single instance of a ConcreteFactory class is created at run-time. This concrete factory creates product objects having a particular implementation. –To create different product objects, clients should use a different concrete factory. AbstractFactory defers creation of product objects to its ConcreteFactory subclass. Slide 8©SoftMoore Consulting

9 Abstract Factory Pattern (continued) Consequences: The Abstract Factory pattern Isolates clients from implementation classes. –Helps control the classes of objects that an application creates. –Clients manipulate instances through their abstract interfaces. Makes exchanging product families easy. –The class of a concrete factory instance appears only once in an application – where it is instantiated. –Easy to change the concrete factory that an application uses. Promotes consistency among products. Makes it difficult to support new kinds of products. –requires modification of AbstractFactory and all subclasses. Slide 9©SoftMoore Consulting

10 Abstract Factory Pattern (continued) Implementation Issues An application typically needs only one instance of a ConcreteFactory per product family (Singleton) AbstractFactory only declares an interface for creating products – ConcreteProduct subclasses actually create them. The most common way to do this is to define a factory method for each product. (See Factory Method pattern.) ©SoftMoore ConsultingSlide 10

11 Abstract Factory Pattern (continued) Implementation Issues (continued) Adding a new kind of product can be problematic since it requires changing the AbstractFactory interface and all ConcreteFactory subclasses. An alternative design is to have a single “createProduct()” method with a parameter to specify the kind of product to be created. createProduct(String productName); This design introduces a new problem. What class is returned by this method? The createProduct() method must return a subclass of a common abstract interface. If clients need to perform subclass-specific operations, they will not be able to do so without downcasting. ©SoftMoore ConsultingSlide 11

12 Example: MazeFactory public class MazeFactory { public Maze makeMaze() { return new Maze(); } public Wall makeWall() { return new Wall(); } public Room makeRoom(int n) { return new Room(n); } public Door makeDoor(Room r1, Room r2) { return new Door(r1, r2); } } ©SoftMoore ConsultingSlide 12 MazeFactory is simply a collection of factory methods. It acts as both an AbstractFactory and a ConcreteFactory.

13 Example: MazeFactory (continued) public class MazeGame { public Maze createMaze(MazeFactory factory) { Maze maze = factory.makeMaze(); Room r1 = factory.makeRoom(1); Room r2 = factory.makeRoom(2); Door door = factory.makeDoor(r1, r2); maze.addRoom(r1); maze.addRoom(r2); ©SoftMoore ConsultingSlide 13 (continued on next page)

14 Example: MazeFactory (continued) r1.setSide(MazeGame.North, factory.makeWall()); r1.setSide(MazeGame.East, door); r1.setSide(MazeGame.South, factory.makeWall()); r1.setSide(MazeGame.West, factory.makeWall()); r2.setSide(MazeGame.North, factory.makeWall()); r2.setSide(MazeGame.East, factory.makeWall()); r2.setSide(MazeGame.South, factory.makeWall()); r2.setSide(MazeGame.West, door); return maze; } ©SoftMoore ConsultingSlide 14 Note that createMaze() delegates the creation maze objects to the MazeFactory.

15 Example: MazeFactory (continued) public class EnchantedMazeFactory extends MazeFactory { public Room makeRoom(int n) { return new EnchantedRoom(n); } public Wall makeWall() { return new EnchantedWall(); } public Door makeDoor(Room r1, Room r2) { return new EnchantedDoor(r1, r2); } } ©SoftMoore ConsultingSlide 15 Extending MazeFactory to create an EnchantedMazeFactory

16 Abstract Factory Pattern in Java The Java Abstract Window Toolkit (AWT) is designed to provide a GUI interface in a heterogeneous environment AWT uses an Abstract Factory to generate all of the required peer components for the specific platform being used. The getToolkit() method is inherited from class Component and returns a reference to the factory object used to create all AWT widgets. Note: Component is a superclass (directly or indirectly) of most AWT widgets including Button, Scrollbar, Window, etc. ©SoftMoore ConsultingSlide 16

17 Abstract Factory Pattern in Java (continued) // in class Component public Toolkit getToolkit() { return getToolkitImpl(); } ©SoftMoore ConsultingSlide 17

18 Abstract Factory Pattern in Java (continued) // in class Component final Toolkit getToolkitImpl() { // If we already have a peer, return its Toolkit. ComponentPeer peer = this.peer; if ((peer != null) && !(peer instanceof LightweightPeer)) return peer.getToolkit(); // If we are already in a container, return its Toolkit. Container parent = this.parent; if (parent != null) return parent.getToolkitImpl(); // default case return Toolkit.getDefaultToolkit(); } ©SoftMoore ConsultingSlide 18

19 Abstract Factory Pattern in Java (continued) // in class Toolkit public static synchronized Toolkit getDefaultToolkit() { if (toolkit == null) {... String nm = System.getProperty("awt.toolkit", "sun.awt.X11.XToolkit"); Class cls = Class.forName(nm);... if (cls != null) toolkit = (Toolkit)cls.newInstance();... } return toolkit; } ©SoftMoore ConsultingSlide 19 Note: On my computer property awt.toolkit has the value sun.awt.windows.WToolkit.

20 Other Examples of Abstract Factory in Java Class javax.xml.parsers.DocumentBuilderFactory static DocumentBuilderFactory newInstance() –returns new instance of a DocumentBuilderFactory Class javax.xml.xpath.XPathFactory static XPathFactory newInstance() –returns a new instance of XPathFactory using the default object model ©SoftMoore ConsultingSlide 20 Abstract Factory is often recognizable by creational methods that return the factory itself, which in turn can be used to create another abstract/interface type.

21 Related Patterns AbstractFactory classes are often implemented with factory methods, but they can also be implemented using Prototype. A concrete factory is often a singleton. ©SoftMoore ConsultingSlide 21

22 References Abstract factory pattern (Wikipedia) http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract Factory Pattern (Object-Oriented Design) http://www.oodesign.com/abstract-factory-pattern.html Design Patterns Uncovered: The Abstract Factory Pattern (Java Lobby) http://java.dzone.com/articles/design-patterns-abstract-factory ©SoftMoore ConsultingSlide 22


Download ppt "The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1."

Similar presentations


Ads by Google