创建型类模式
创建型类模式抽象了实例化过程
它们帮助一个系统
独立于如何创建组合和表示它们的
那些对象一个类创建型模式使用继承改变被实例化的类
一个对象创建型模式将实例化委托给一个对象( 如抽象工厂中产品的实例化就委托给工厂 原型中是使用一个原型进行Clone来实例化一个对象)
在这些模式中有两个不断出现的主旋律
1 它们都将关于系统使用哪些具体的类的信息进行了封装
2 它们隐藏了这些类的实例是如何被创建和放在一起的 整个系统所知道的是由这些抽象类所定义的接口
因此 创建型模式在什么被创建 由谁创建 它怎样被创建都给了你很大的灵活性
创建型模式有5种方法
1 工厂方法 factory method
2 生成器 builder
3 抽象工厂 abstract factory
4 原型 prototype
5 单件 singleton
http://www.cs.unb.ca/profs/wdu/cs4015w02/ch3a.htmCODE
下面是其它的说明
Creational design patterns
创建型设计模式
* Creational design patterns abstract the instantiation process.
创建型设计模式抽象了实例化过程
* Creational patterns become important as systems evolve to depend more on object composition than class inheritance. Thus creating objects with particular behaviors requires more than simply instantiating a class.
当系统依赖多个对象的组合而不是继承时 创建型模式就会变得很重要,因此使用特殊的方法创建一个对象比实例化一个类还要简单
* Two recurring themes in creational patterns.
在创建型模式中出现的两上主旋律
o They all encapsulate knowledge about which concrete classes the system uses.
他们都封装了系统使用的类的具体实现
o They hide how instances of these classes are created and put together.
他们隐藏了怎样实例化这些类并把他们放在一起的过程
* The creational patterns give you a lot of flexibility in what gets created, who creates it, how it gets created, and when.
* They let you configure a system with "product" objects that vary widely in structure and functionality.
* A common example—building a maze for a computer game—to illustrate their implementations.
enum Direction {North, South, East, West};
class MapSite {
public:
virtual void Enter() = 0;
};
class Room : public MapSite {
public:
Room(int roomNo);
MapSite* GetSide(Direction) const;
void SetSide(Direction, MapSite*);
virtual void Enter();
private:
MapSite* _sides[4];
int _roomNumber;
};
class Wall : public MapSite {
public:
Wall();
virtual void Enter();
};
class Door : public MapSite {
public:
Door(Room* = 0, Room* = 0);
virtual void Enter();
Room* OtherSideFrom(Room*);
private:
Room* _room1;
Room* _room2;
bool _isOpen;
};
class Maze {
public:
Maze();
void AddRoom(Room*);
Room* RoomNo(int) const;
private:
// ...
};
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);
r1->SetSide(North, new Wall);
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;
}
* The real problem with this member function isn't its size but its inflexibility. It hard-codes the maze layout..
* The creational patterns show how to make this design more flexible, not necessarily smaller.
* Examples: The enchanted maze game with new kinds of components, e.g. DoorNeedingSpell and EnchantedRoom.
* The creational patterns provide different ways to remove explicit references to concrete classes from code that needs to instantiate them:
o If CreateMaze calls virtual functions instead of constructor calls to create the rooms, walls, and doors it requires, then you can change the classes that get instantiated by making a subclass of MazeGame and redefining those virtual functions. This approach is an example of the Factory Method pattern.
o If CreateMaze is passed an object as a parameter to use to create rooms, walls, and doors, then you can change the classes of rooms, walls, and doors by passing a different parameter. This is an example of the Abstract Factory pattern.
o If CreateMaze is passed an object that can create a new maze in its entirety using operations for adding rooms, doors, and walls to the maze it builds, then you can use inheritance to change parts of the maze or the way the maze is built. This is an example of the Builder pattern.
o If CreateMaze is parameterized by various prototypical room, door, and wall objects, which it then copies and adds to the maze, then you can change the maze's composition by replacing these prototypical objects with different ones. This is an example of the Prototype pattern.
o The singleton pattern ensure there's only one maze per game and that all game objects have ready access to it, and makes it easy to extend or replace the maze without touching existing code.