abstract factory petterns

16
Department of Computer Science & Engineering in Hanyang University SINCE 2006 1 Abstract Factory HPC and OT Lab. HPC and OT Lab. 23.Jan.2007. 23.Jan.2007. M.S. 1 M.S. 1 st st Choi, Hyeon Seok Choi, Hyeon Seok

Upload: hyeonseok-choi

Post on 20-May-2015

350 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Abstract factory petterns

Department of Computer Science & Engineering in Hanyang University SINCE 2006

11

Abstract Factory

HPC and OT Lab.HPC and OT Lab.23.Jan.2007.23.Jan.2007.

M.S. 1M.S. 1stst Choi, Hyeon Seok Choi, Hyeon Seok

Page 2: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 22

ContentsContents

Creational PatternsCreational Patterns

IntroductionIntroduction

Structure, Participants and CollaborationsStructure, Participants and Collaborations

ConsequencesConsequences

ImplementationImplementation

Sample codeSample code

Related PatternsRelated Patterns

ReferencesReferences

Page 3: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 33

Creational PatternsCreational Patterns

ThemesThemes

All encapsulate knowledge about which concrete classes All encapsulate knowledge about which concrete classes the system usesthe system uses

Hide how instances of these classes are created and put Hide how instances of these classes are created and put togethertogether

Creational patterns scopeCreational patterns scope

ClassClass : : Use inheritanceUse inheritance to vary the class that’s to vary the class that’s instantiatedinstantiated

ObjectObject : Delegate instantiation to another object : Delegate instantiation to another object

Page 4: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 44

+CreateScrollBar()+CreateWindow()

WidgetFactory

+CreateScrollBar()+CreateWindow()

MotifWidgetFactory

+CreateScrollBar()+CreateWindow()

PMWidgetFactory

Clien

Window

ScrollBar

PMWindow MotifWindonw

PMScrollBar MotifScrollBar

Intent of Abstract FactoryIntent of Abstract Factory

Provide an interface for Provide an interface for creating families of related creating families of related or dependent objectsor dependent objects without specifying their without specifying their concrete classes.concrete classes.

Page 5: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 55

ApplicabilityApplicability

IndependentIndependent of how its objects are created, of how its objects are created, composed, and representedcomposed, and represented

Configured with Configured with one of multiple familiesone of multiple families of products. of products.

A family of related objects is designed to be used A family of related objects is designed to be used together, and you need to together, and you need to enforce this constraintenforce this constraint..

Provide a class library of products, and you want to Provide a class library of products, and you want to reveal just their reveal just their interfaces, not implementationsinterfaces, not implementations..

Page 6: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 66

Structure, Participants and Structure, Participants and CollaborationsCollaborations

+CreateProductA()+CreateProductB()

AbstractFactory

+CreateProductA()+CreateProductB()

ConcreteFactory1

+CreateProductA()+CreateProductB()

ConcreteFactory2

Client

AbstractProductA

AbstractProductB

ProductA2 ProductA1

ProductB2 ProductB1

Declare an interface(create product object)

Implement operation(create product object)

Declare an interface(product object)

Implement operation(product object)

Page 7: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 77

ConsequencesConsequences

AdvantagesAdvantages

IsolateIsolate concrete classes. concrete classes.

Make Make exchangingexchanging product families easy. product families easy.

PromotePromote consistency among products. consistency among products.

DisadvantagesDisadvantages

Supporting new kinds of products is Supporting new kinds of products is difficultdifficult..

Page 8: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 88

Implementation (1/2)Implementation (1/2)

Factories as singletonsFactories as singletons Application needs Application needs only one instanceonly one instance of a ConcreteFactory of a ConcreteFactory

Creating the productsCreating the products ConcreteProduct define a ConcreteProduct define a factory methodfactory method for each object. for each object. If many product families are possible, the concrete factory can be If many product families are possible, the concrete factory can be

implemented using the implemented using the prototype patternprototype pattern..

Defining extensible factoriesDefining extensible factories Add a parameter to operation that create objectsAdd a parameter to operation that create objects ex) Object make (ex) Object make (ObjectTypeObjectType type); type);

Page 9: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 99

Implementation (2/2)Implementation (2/2)

Factory method and Prototype Factory method and Prototype

+CreateProductA()+CreateProductB()

AbstractFactor

+CreateProductA()+CreateProductB()

-ProductA : AbstractProductA-ProductB : AbstractProductB

ConcreteFactory1

Client

+clone()

AbstractProductA

+clone()

AbstractProductB

+clone()

ProductA2

+clone()

ProductA1

+clone()

ProductB2

+clone()

ProductB1

+clone()

ProductA3

+clone()

ProductB3

Prototype(clone)

Factorymethod

Page 10: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 1010

Sample code (1/5)Sample code (1/5)

---[MazeGame::CreateMaze]--------------------------------------------

Maze *MazeGame::CreateMaze() { Maze *aMaze = new Maze(); Room *r1 = new Room(1); Room *r2 = new Room(2); Door *theDoor = new Door(r1, r2); aMaze->AddRoom(r1); aMaze->AddRoom(r2); // add room

r1->SetSide(North, new Wall); // set side r1->SetSide(East, theDoor); r1->SetSide(South, new Wall); r1->SetSide(West, new Wall);

r2->SetSide(North, new Wall); r2->SetSide(East, new Wall); r2->SetSide(South, new Wall); r2->SetSide(West, theDoor);

return aMaze;};

Page 11: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 1111

Sample code (2/5)Sample code (2/5)

---[Class MazeFactory]-----------------------------------------------

class MazeFactory { public:

MazeFactory();// Factory methodvirtual Maze *MakeMaze() const { return new Maze;}virtual Wall *MakeWall() const { return new Wall;}virtual Room *MakeRoom(int n) const { return new Room(n);}virtual Door *MakeDoor(Room *r1, Room *r2) const { return new Door(r1, r2);}

};

Page 12: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 1212

Sample code (3/5)Sample code (3/5)

---[MazeGame::CreateMaze]--------------------------------------------

// use to MazeFactoryMaze *MazeGame::CreateMaze(MazeFactory &factory) { Maze *aMaze = factory.MakeMaze(); Room *r1 = factory.MakeRoom (1); Room *r2 = factory.MakeRoom(2); Door *theDoor = factory.MakeDoor(r1, r2); aMaze->AddRoom(r1); aMaze->AddRoom(r2);

r1->SetSide(North, factory.MakeWall()); r1->SetSide(East, theDoor); r1->SetSide(South, factory.MakeWall()); r1->SetSide(West, factory.MakeWall());

r2->SetSide(North, factory.MakeWall()); r2->SetSide(East, factory.MakeWall()); r2->SetSide(South, factory.MakeWall()); r2->SetSide(West, theDoor);

return aMaze;}

Page 13: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 1313

Sample code (4/5)Sample code (4/5)

---[class EnchantedMazeFactory]--------------------------------------

class EnchantedMazeFactory: public MazeFactory { public:

EnchantedMazeFactory();

// overrided to factory methodvirtual 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;

};

Page 14: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 1414

Sample code (5/5)Sample code (5/5)

---[class BombedMazeFactory]--------------------------------------

class BombedMazeFactory: public MazeFactory { public:

BombedMazeFactory();

// overrided to factory methodvirtual Wall *MakeWall() const { return new BombedWall;}

virtual Room *MakeRoom(int n) const { return new RoomWithABomb(n);}

};

MazeGame game;BombedMazeFactory factory;game.CreateMaze(factory);

Page 15: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 1515

Related PatternsRelated Patterns

Abstract Factory classes are often implemented Abstract Factory classes are often implemented with with Factory methodFactory method

Abstract Factory and Factory can be implemented Abstract Factory and Factory can be implemented using using PrototypePrototype

A Concrete factory is often a A Concrete factory is often a SingletonSingleton

Page 16: Abstract factory petterns

High Performance Computing & Object Technology Laboratory in CSE 1616

ReferencesReferences

[1] Erich Gamma, Richard Helm, Ralph Johnson, John [1] Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides: Design Patterns Elements of Reusable Object-Vlissides: Design Patterns Elements of Reusable Object- Oriented Software. Addison Wesley, 2000Oriented Software. Addison Wesley, 2000

[2] [2] 장세찬 장세찬 : C++: C++ 로 배우는 패턴의 이해와 활용로 배우는 패턴의 이해와 활용 . . 한빛 미디어한빛 미디어 , 2004, 2004

[3] Eric Freeman, Elisabeth Freeman, [3] Eric Freeman, Elisabeth Freeman, 서환수 역 서환수 역 : Head First : Head First Design Patterns. Design Patterns. 한빛 미디어한빛 미디어 , 2005, 2005