How to Troubleshoot Apps for the Modern Connected Worker
Abstract factory petterns
1. Department of Computer Science & Engineering in Hanyang University SINCE 2006
Abstract Factor y
HPC and OT Lab.
23.Jan.2007.
M.S. 1st Choi, Hyeon Seok
1
2. Contents
Creational Patterns
Introduction
Structure, Participants and Collaborations
Consequences
Implementation
Sample code
Related Patterns
References
High Performance Computing & Object Technology Laboratory in CSE 2
3. Creational Patterns
Themes
All encapsulate knowledge about which concrete classes
the system uses
Hide how instances of these classes are created and put
together
Creational patterns scope
Class : Use inheritance to vary the class that’s
instantiated
Object : Delegate instantiation to another object
High Performance Computing & Object Technology Laboratory in CSE 3
4. Intent of Abstract Factory
Provide an interface for creating families of related
or dependent objects without specifying their
concrete classes.
WidgetFactory
+CreateScrollBar() Clien
+CreateWindow()
Window
PMWindow MotifWindonw
MotifWidgetFactory PMWidgetFactory
+CreateScrollBar() +CreateScrollBar()
+CreateWindow() +CreateWindow()
ScrollBar
PMScrollBar MotifScrollBar
High Performance Computing & Object Technology Laboratory in CSE 4
5. Applicability
Independent of how its objects are created,
composed, and represented
Configured with one of multiple families of
products.
A family of related objects is designed to be used
together, and you need to enforce this constraint.
Provide a class library of products, and you want to
reveal just their interfaces, not implementations.
High Performance Computing & Object Technology Laboratory in CSE 5
7. Consequences
Advantages
Isolate concrete classes.
Make exchanging product families easy.
Promote consistency among products.
Disadvantages
Supporting new kinds of products is difficult.
High Performance Computing & Object Technology Laboratory in CSE 7
8. Implementation (1/2)
Factories as singletons
Application needs only one instance of a
ConcreteFactory
Creating the products
ConcreteProduct define a factory method for each
object.
If many product families are possible, the concrete
factory can be implemented using the prototype pattern.
Defining extensible factories
Add a parameter to operation that create objects
ex) Object make (ObjectType type);
High Performance Computing & Object Technology Laboratory in CSE 8
10. 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;
};
High Performance Computing & Object Technology Laboratory in CSE 10
11. Sample code (2/5)
---[Class
MazeFactory]-----------------------------------------------
class MazeFactory
{
public:
MazeFactory();
// Factory method
virtual 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);
}
};
High Performance Computing & Object Technology Laboratory in CSE 11
13. Sample code (4/5)
---[class
EnchantedMazeFactory]--------------------------------------
class EnchantedMazeFactory: public MazeFactory
{
public:
EnchantedMazeFactory();
// overrided to factory method
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;
};
High Performance Computing & Object Technology Laboratory in CSE 13
14. Sample code (5/5)
---[class BombedMazeFactory]--------------------------------------
class BombedMazeFactory: public MazeFactory
{
public:
BombedMazeFactory();
// overrided to factory method
virtual Wall *MakeWall() const
{
return new BombedWall;
}
virtual Room *MakeRoom(int n) const
{
return new RoomWithABomb(n);
}
};
MazeGame game;
BombedMazeFactory factory;
game.CreateMaze(factory);
High Performance Computing & Object Technology Laboratory in CSE 14
15. Related Patterns
Abstract Factory classes are often implemented
with Factory method
Abstract Factory and Factory can be implemented
using Prototype
A Concrete factory is often a Singleton
High Performance Computing & Object Technology Laboratory in CSE 15
16. References
[1] Erich Gamma, Richard Helm, Ralph Johnson, John
Vlissides: Design Patterns Elements of Reusable Object-
Oriented Software. Addison Wesley, 2000
[2] 장세찬 : C++ 로 배우는 패턴의 이해와 활용 . 한빛 미디어 ,
2004
[3] Eric Freeman, Elisabeth Freeman, 서환수 역 : Head First
Design Patterns. 한빛 미디어 , 2005
High Performance Computing & Object Technology Laboratory in CSE 16