Download presentation
Presentation is loading. Please wait.
Published byBrett Bond Modified over 8 years ago
1
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes
2
Abstract Factory pattern Motivation MotifWidgetFactory CreateScrollBar() CreateWindow() PMWidgetFactory CreateScrollBar() CreateWindow() PMWindowMotifWindow PMScrollBarMotifScrollBar WidgetFactory (abstract) CreateScrollBar() CreateWindow() Window ScrollBar Client creates
3
Abstract Factory Motivation
4
Abstract Factory Structure Provide an interface for creating families of related or dependent objects without specifying their concrete class
5
Abstract Factory pattern Structure ConcreteFactory1 CreateProductA() CreateProductB() ConcreteFactory2 CreateProductA() CreateProductB() ProductA2 ProductA1 ProductB2 ProductB1 AbstractFactory CreateProductA() CreateProductB() AbstractProductA AbstractProductB Client creates
6
Abstract Factory pattern 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 designated 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
7
Abstract Factory pattern 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) – declared an interface for a type of product object ConcreteProduct (MotifWindow, MotifScrollBar) – 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
8
Abstract Factory - consequences Isolates concrete classes Makes exchanging product families easy – the class of concrete factory appears only once in the application - that is, where it’s instantiated Promotes consistency among products – enforces, that products from one family are used together Supporting new kinds of products is difficult – AbstractFactory interface fixes the set of products that can be created – involves changing AbstractFactory and all its subclasses interfaces
9
Abstract Factory - implementation Factories as singletons Creating the products – use Factory Method pattern declare FactoryMethod for each kind of product in AbstractFactory override FactoryMethod in ConcreteFactory – use Prototype pattern for ConcreteFactory if many product families are possible initialize the concrete factory with a prototypical instance of each product in the family concrete factory will create a new product by cloning the prototype no need for a new concrete factory class for each new family Defining extensible factories – Via parameterization -- I.e., object = factory.make ( typeA ) ; – Via mixing concrete and abstract factory hierarchies.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.