Abstract factory petterns

555 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
555
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Abstract factory petterns

  1. 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. 2. Contents  Creational Patterns  Introduction  Structure, Participants and Collaborations  Consequences  Implementation  Sample code  Related Patterns  ReferencesHigh Performance Computing & Object Technology Laboratory in CSE 2
  3. 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 objectHigh Performance Computing & Object Technology Laboratory in CSE 3
  4. 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 MotifScrollBarHigh Performance Computing & Object Technology Laboratory in CSE 4
  5. 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
  6. 6. Structure, Participants and Collaborations Declare an interface (create product object) Declare an interface AbstractFactory (product object) +CreateProductA() Client +CreateProductB() AbstractProductA ProductA2 ProductA1 ConcreteFactory1 ConcreteFactory 2 +CreateProductA() +CreateProductA() +CreateProductB() +CreateProductB() AbstractProductB Implement operation (create product object) ProductB2 ProductB1 Implement operation (product object)High Performance Computing & Object Technology Laboratory in CSE 6
  7. 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. 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
  9. 9. Implementation (2/2)  Factory method and Prototype AbstractFactor +CreateProductA() Client +CreateProductB() Prototype (clone) AbstractProductA +clone() ConcreteFactory 1 -ProductA : AbstractProductA -ProductB : AbstractProductB ProductA2 ProductA1 ProductA3 +CreateProductA () +clone() +clone() +clone() +CreateProductB () Factory method AbstractProductB +clone() ProductB2 ProductB1 ProductB3 +clone() +clone() +clone()High Performance Computing & Object Technology Laboratory in CSE 9
  10. 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. 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
  12. 12. Sample code (3/5) --- [MazeGame::CreateMaze]-------------------------------------------- // use to MazeFactory Maze *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; }High Performance Computing & Object Technology Laboratory in CSE 12
  13. 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. 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. 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 SingletonHigh Performance Computing & Object Technology Laboratory in CSE 15
  16. 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. 한빛 미디어 , 2005High Performance Computing & Object Technology Laboratory in CSE 16

×