s.ducasse stéphane ducasse 1 abstract factory

18
S.Ducasse 1 QuickTime™ TIFF (Uncompr are needed t Stéphane Ducasse [email protected] http://www.listic.univ-savoie.f r/~ducasse/ Abstract Factory

Upload: lynne-page

Post on 20-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

S.Ducasse 3 Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes Also known as: Kit

TRANSCRIPT

Page 1: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 1

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Stéphane [email protected]://www.listic.univ-savoie.fr/~ducasse/

Abstract Factory

Page 2: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 2

License: CC-Attribution-ShareAlike 2.0http://creativecommons.org/licenses/by-sa/2.0/

Page 3: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 3

Abstract FactoryProvide an interface for creating families of related or dependent objects without specifying their concrete classes

Also known as: Kit

Page 4: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 4

Abstract Factory Intent• Provide an interface for creating families of

related or dependent objects without specifying their concrete classes

Page 5: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 5

Abstract Factory MotivationYou 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 MacOsX?

Page 6: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 6

Abstract Factory MotivationAbstract factory introduce an interface for creating each basic kind of widget

Page 7: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 7

Abstract Factory Applicability

a system should be independent of how its products are created, composed, and representeda system should be configured with one of multiple families of productsa family of related product objects is designed to be used together, and you need to enforce this constraintyou want to provide a class library of products, and you want to reveal just their interfaces, not their implementations

Page 8: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 8

Abstract Factory Structure

Page 9: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 9

Abstract Factory Participants

AbstractFactory (WidgetFactory)declares an interface for operations that create abstract product objects

ConcreteFactory (MotifWidgetFactory, PMWidgetFactory)

implements the operations to create concrete product objects

Page 10: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 10

Abstract Factory Participants

AbstractProduct (Window, ScrollBar)defines a product object to be created by the corresponding concrete factoryimplements the AbstractProduct interface

Clientuses only interfaces declared by AbstractFactory and AbstractProduct classes

Page 11: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 11

Implementation: Specifying the FactoryMazeGame class>>createMazeFactory ^ (MazeFactory new addPart: Wall named: #wall; addPart: Room named: #room; addPart: Door named: #door; yourself) EnchantedMazeGame class>>createMazeFactory ^ (MazeFactory new addPart: Wall named: #wall; addPart: EnchantedRoom named: #room; addPart: DoorNeedingSpell named: #door; yourself)

Page 12: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 12

SingleFactoryMazeGame class>>createMazeFactory ^ (MazeFactory new addPart: Wall named: #wall; addPart: Room named: #room; addPart: Door named: #door; yourself) MazeGame class>>createEnchantedMazeFactory ^ (MazeFactory new addPart: Wall named: #wall; addPart: EnchantedRoom named: #room; addPart: DoorNeedingSpell named: #door; yourself)

Page 13: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 13

Implementation: Using the FactoryMazeFactory>>createMaze: aFactory

| room1 room2 aDoor | room1 := (aFactory make: #room) number: 1. room2 := (aFactory make: #room) number: 2. aDoor := (aFactory make: #door) from: room1 to: room2. room1 atSide: #north put: (aFactory make: #wall). room1 atSide: #east put: aDoor. ... room2 atSide: #south put: (aFactory make: #wall). room2 atSide: #west put: aDoor. ^ Maze new addRoom: room1; addRoom: room2; yourselfMazeFactory>>make: partName ^ (partCatalog at: partName) new

Page 14: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 14

Abstract Factory Collaborations

CollaborationsNormally a single instance of ConcreteFactory is created at run-timeAbstractFactory defers creation of product objects to its ConcreteFactory subclass

Page 15: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 15

ConsequencesIt isolates concrete classesIt makes exchanging product families easyIt promotes consistency among productsSupporting new kinds of products is difficult (set of products is somehow fixed)The class factory “controls” what is created

Page 16: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 16

Using PrototypesThe concrete factory stores the prototypes to be cloned in a dictionary called partCatalog.

make: partName ^ (partCatalog at: partName) copy

The concrete factory has a method for adding parts to the catalog.

addPart: partTemplate named: partName partCatalog at: partName put: partTemplate

Prototypes are added to the factory by identifying them with a symbol:

aFactory addPart: aPrototype named: #ACMEWidget

Page 17: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 17

In RelationsBuilder and Abstract Factory are closely relatedBut Builder is in charge of assembling partsAbstractFactory is responsible of producing parts that work together

Page 18: S.Ducasse Stéphane Ducasse  1 Abstract Factory

S.Ducasse 18

Known UsesVisualWorks UILookPolicy