Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.

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.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
Plab – Tirgul 12 Design Patterns
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
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.
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.
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.
UML - Patterns 1 Design Patterns. UML - Patterns 2 Becoming Good OO Developers Developing good OO Software is hard Takes a lot of time to take advantage.
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.
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 -
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
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.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns I.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
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.
The Abstract Factory Pattern (Creational) ©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
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Factory Patterns 1.
Software Design and Architecture
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Presented by Igor Ivković
Design Patterns - A few examples
Software Engineering Lecture 7 - Design Patterns
Abstract Factory Pattern
Object Oriented Design Patterns - Creational Patterns
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Creational Patterns.
Informatics 122 Software Design II
Presented by Igor Ivković
Software Design Lecture 9.
Presentation transcript:

Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern

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.

Motivation(4) WidgetFactory CreateScrollBar() … MotifWidgetFactory CreateScrollBar() … PMWidgetFactory CreateScrollBar() … Client Window PMWindowMotifWindow ScrollBar PMScrollBarMotifScrollBar

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.

Structure AbstractFactory CreateProductA() … ConcreteFactory1 CreateProductA() … ConcreteFactory2 CreateProductA() … Client AbProductA ProductA2ProductA1 AbProductB ProductB2ProductB1

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