笔记:Gof设计模式--Factory Method
2012-05-29 22:17
375 查看
1、意图
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
2、适应性
Use the Factory Method pattern when
• a class can't anticipate the class of objects it must create.
• a class wants its subclasses to specify the objects it creates.
• classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
3、结构
4、示例代码
First we'll define factory methods in MazeGame for creating the maze, room, wall, and door objects:
MazeGame subclasses can redefine some or all of the factory methods to specify variations in products. For example, a BombedMazeGame can redefine the Room and Wall products to return the bombed varieties:
An EnchantedMazeGame variant might be defined like this:
Now we can rewrite CreateMaze to use these factory methods:
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
2、适应性
Use the Factory Method pattern when
• a class can't anticipate the class of objects it must create.
• a class wants its subclasses to specify the objects it creates.
• classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
3、结构
4、示例代码
First we'll define factory methods in MazeGame for creating the maze, room, wall, and door objects:
class MazeGame { public: Maze* CreateMaze(); // factory methods: virtual Maze* MakeMaze() const { return new Maze; } virtual Room* MakeRoom(int n) const { return new Room(n); } virtual Wall* MakeWall() const { return new Wall; } virtual Door* MakeDoor(Room* r1, Room* r2) const { return new Door(r1, r2); } };
MazeGame subclasses can redefine some or all of the factory methods to specify variations in products. For example, a BombedMazeGame can redefine the Room and Wall products to return the bombed varieties:
class BombedMazeGame : public MazeGame { public: BombedMazeGame(); virtual Wall* MakeWall() const { return new BombedWall; } virtual Room* MakeRoom(int n) const { return new RoomWithABomb(n); } };
An EnchantedMazeGame variant might be defined like this:
class EnchantedMazeGame : public MazeGame { public: EnchantedMazeGame(); 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; };
Now we can rewrite CreateMaze to use these factory methods:
Maze* MazeGame::CreateMaze () { Maze* aMaze = MakeMaze(); Room* r1 = MakeRoom(1); Room* r2 = MakeRoom(2); Door* theDoor = MakeDoor(r1, r2); aMaze->AddRoom(r1); aMaze->AddRoom(r2); r1->SetSide(North, MakeWall()); r1->SetSide(East, theDoor); r1->SetSide(South, MakeWall()); r1->SetSide(West, MakeWall()); r2->SetSide(North, MakeWall()); r2->SetSide(East, MakeWall()); r2->SetSide(South, MakeWall()); r2->SetSide(West, theDoor); return aMaze; }
相关文章推荐
- 设计模式学习笔记:factory method(工厂方法)
- 设计模式初学者笔记:Factory Method模式
- 设计模式学习笔记四:工厂方法(Factory Method)
- 步步为营 .NET 设计模式学习笔记 二十四、Factory Method(工厂方法模式)
- 设计模式笔记-创建型模式之三--工厂方法:Factory Method
- [设计模式笔记]一. 创建型模式--2.Factory Method模式(一)
- 设计模式学习笔记四:工厂方法(Factory Method)
- 设计模式笔记之工厂模式--工厂方法模式(Factory Method)
- 设计模式学习笔记-Factory Method
- 设计模式之Factory Method 之学习笔记
- 面向对象设计模式纵横谈:Factory Method 工厂方法模式(笔记记录)
- .Net设计模式学习笔记(五):工厂方法模式(Factory Method)
- GOF设计模式-创建型模式理解与思索(二)(Factory Method分析)
- GoF23种设计模式之 - Factory Method模式
- [设计模式笔记]一. 创建型模式--2.Factory Method模式(二)
- 阿Sam的设计模式学习笔记----Factory模式(之Factory Method)
- 设计模式学习笔记——工厂方法(Factory Method)
- [设计模式学习笔记二][创建型模式][工厂方法(Factory Method)]
- 设计模式学习笔记二:工厂方法模式(Factory Method)
- 【设计模式】学习笔记5:工厂模式(Factory Method)