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

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Creational Patterns (2) CS350/SE310 Fall, Lower the Cost of Maintenance Economic Goal Coupling-Cohesion, Open-Close, Information-Hiding, Dependency.
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Creational Patterns - Page L4-1 PS95&96-MEF-L11-1 Dr. M.E. Fayad Creationa l Paradigm.
Overview of Design Patterns
CSE3308/CSC Software Engineering: Analysis and DesignLecture 5B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/CSC3080/DMS/2000/12.
Plab – Tirgul 12 Design Patterns
CSE Software Engineering: Analysis and Design, 2005Lecture 8A.1 Software Engineering: Analysis and Design - CSE3308 Design and Analysis Patterns.
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Design Patterns A brief introduction to what they are, why they are useful, and some examples of those that are commonly used.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Nov, 1, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and its.
CSE Software Engineering: Analysis and Design, 2002Lecture 7B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/DMS/2002/15.
Prototype Pattern Intent:
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
1 Creational Patterns CS : Software Design Winter /T8.
Design Patterns Examples in C++ Moshe Fresko Bar-Ilan University Object Oriented Programming
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Pattern Abstract Factory
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns II.
Creational Patterns (1) CS350, SE310, Fall, 2010.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
Software Components Creational Patterns.
Abstract Factory Abstract Factory using Factory Method.
Creational Patterns CSE Creational Patterns Class creational pattern ◦ uses inheritance to vary the class that is instantiated Object creational.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Define an interface for creating an object, but let subclasses decide which class to instantiate.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
CSC 480 Software Engineering Lab 5 – Abstract Factory Pattern Oct 30, 2002.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
 Creational design patterns abstract the instantiation process.  make a system independent of how its objects are created, composed, and represented.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
S.Ducasse Stéphane Ducasse 1 Abstract Factory.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
SOFTWARE DESIGN Design Patterns 1 6/14/2016Computer Science Department, TUC-N.
07 - Creational PatternsCSC4071 Creational Patterns Patterns used to abstract the process of instantiating objects. –class-scoped patterns uses inheritance.
Overview of Design Patterns
Design Pattern.
Abstract Factory Pattern
Design Patterns A brief introduction to what they are, why they are useful, and some examples of those that are commonly used.
Factory Patterns 1.
JAVA Design Patterns.
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Design Patterns - A few examples
Software Engineering Lecture 7 - Design Patterns
Abstract Factory Pattern
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Creational Patterns.
Software Design Lecture 9.
Presentation transcript:

The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1

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

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

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

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

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

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

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

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

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

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

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.

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)

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.

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

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

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

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

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.

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.

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

References Abstract factory pattern (Wikipedia) Abstract Factory Pattern (Object-Oriented Design) Design Patterns Uncovered: The Abstract Factory Pattern (Java Lobby) ©SoftMoore ConsultingSlide 22