欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

设计模式之工厂模式

程序员文章站 2024-01-21 14:32:52
...
特征:封装对象的创建.
1,给一个对象添加新类型时,类的创建必然要发生改变,导致了问题的出现.
2,解决办法:强制用一个通用的工厂(factory)来创建对象,而不允许将创建对象的代码遍布于整个程序.
3,如果创建对象的代码都装到这个工厂执行,那么在增加新对象时,所要做的全部工作就是只需修改工厂.
4,每个面向对象的应用程序都需要创建对象,并且因为人们可能添加新类来扩展应用程序,因此[b]工厂模式可能是所有设计模式中最有用的模式之一.[/b]

5,工程模式的实现方法之一:在基类中定义一个静态成员函数.
实例代码:

#include <iostream>
#include <stdexcept>
#include <vector>
using namespace std;

class Shape
{
public:
virtual void draw()=0;
virtual void erase()=0;
virtual ~Shape(){}
class shape_error : public logic_error
{
public:
shape_error(string type):logic_error("Cann't create shape: "+type){};
};
static Shape* factory(const string& type) throw(shape_error);
};

class Circle : public Shape
{
Circle(){}//构造函数设为私有,保证只有静态工厂方法可以创建对象
friend Shape* Shape::factory(const string& type);
public:
void draw(){ cout<<"Circle:Draw"<<endl; }
void erase(){ cout<<"Circle:Erase"<<endl; }
~Circle(){ cout<<"Circle:~Circle"<<endl; }
};

class Square : public Shape
{
Square(){}//构造函数设为私有,保证只有静态工厂方法可以创建对象
friend Shape* Shape::factory(const string& type);
public:
void draw(){ cout<<"Square:Draw"<<endl; }
void erase(){ cout<<"Square:Erase"<<endl; }
~Square(){ cout<<"Square:~Circle"<<endl; }
};


Shape* Shape::factory(const string& type) throw(shape_error)
{
if(type=="Circle") return new Circle;
if(type=="Square") return new Square;
throw shape_error(type);
}

char* sl[] = { "Circle", "Square", "Square", "Circle", "Circle", "Circle", "Square" };
int main()
{
vector<Shape*> shapes;
try
{
for(size_t i = 0; i < sizeof sl / sizeof sl[0]; i++)
shapes.push_back(Shape::factory(sl[i]));
} catch(Shape::shape_error& e)
{
cout << e.what() << endl;
for(vector<Shape*>::iterator ite=shapes.begin();ite!=shapes.end();ite++)
delete *ite;
return EXIT_FAILURE;
}
for(size_t i = 0; i < shapes.size(); i++)
{
shapes[i]->draw();
shapes[i]->erase();
}
for(vector<Shape*>::iterator ite=shapes.begin();ite!=shapes.end();ite++)
delete *ite;
return 0;
}


6,多态工厂模式:工厂方法作为单独的类中的虚函数出现.
实例代码:

#include <iostream>
#include <stdexcept>
#include <vector>
#include <map>
using namespace std;

class Shape
{
public:
virtual void draw()=0;
virtual void erase()=0;
virtual ~Shape(){}
};

class ShapeFactory //类工厂
{
virtual Shape* create()=0; //虚函数
static map<string,ShapeFactory*> factories;
public:
virtual ~ShapeFactory(){}
friend class ShapeFactoryInitializer;
//核心方法
class shape_error : public logic_error
{
public:
shape_error(string type):logic_error("Cann't create shape: "+type){};
};
static Shape* createShape(const string& id) throw(shape_error)
{
if(factories.find(id)!=factories.end())
return factories[id]->create();
else
throw shape_error(id);
}
};
map<string,ShapeFactory*> ShapeFactory::factories;

class Circle : public Shape
{
Circle(){}//构造函数设为私有,保证只有静态工厂方法可以创建对象
friend class ShapeFactoryInitializer; //友元
//注意私有友元类的使用
class Factory;
friend class Factory;
class Factory : public ShapeFactory
{
public:
Shape* create(){ return new Circle; }
friend class ShapeFactoryInitializer; //友元
};
public:
void draw(){ cout<<"Circle:Draw"<<endl; }
void erase(){ cout<<"Circle:Erase"<<endl; }
~Circle(){ cout<<"Circle:~Circle"<<endl; }
};

class Square : public Shape
{
Square(){}//构造函数设为私有,保证只有静态工厂方法可以创建对象
friend class ShapeFactoryInitializer; //友元
//注意私有友元类的使用
class Factory;
friend class Factory;
class Factory : public ShapeFactory
{
public:
Shape* create(){ return new Square; }
friend class ShapeFactoryInitializer; //友元
};
public:
void draw(){ cout<<"Square:Draw"<<endl; }
void erase(){ cout<<"Square:Erase"<<endl; }
~Square(){ cout<<"Square:~Circle"<<endl; }
};

class ShapeFactoryInitializer
{
static ShapeFactoryInitializer si;
ShapeFactoryInitializer()
{
ShapeFactory::factories["Circle"]=new Circle::Factory; //这里
ShapeFactory::factories["Square"]=new Square::Factory; //这里
}
~ShapeFactoryInitializer()
{
for(map<string,ShapeFactory*>::iterator ite=ShapeFactory::factories.begin();ite!=ShapeFactory::factories.end();ite++)
delete ite->second;
}
};
ShapeFactoryInitializer ShapeFactoryInitializer::si;

char* sl[] = { "Circle", "Square", "Square", "Circle", "Circle", "Circle", "Square" };
int main()
{
vector<Shape*> shapes;
try
{
for(size_t i = 0; i < sizeof sl / sizeof sl[0]; i++)
shapes.push_back(ShapeFactory::createShape(sl[i]));
} catch(ShapeFactory::shape_error& e)
{
cout << e.what() << endl;
for(vector<Shape*>::iterator ite=shapes.begin();ite!=shapes.end();ite++)
delete *ite;
return EXIT_FAILURE;
}
for(size_t i = 0; i < shapes.size(); i++)
{
shapes[i]->draw();
shapes[i]->erase();
}
for(vector<Shape*>::iterator ite=shapes.begin();ite!=shapes.end();ite++)
delete *ite;
return 0;
}


7,抽象工厂模式:
使用若干工厂方法模式,每个工厂方法模式创建一个不同类型的对象.

#include <iostream>
using namespace std;

class Obstacle
{
public:
virtual void action()=0;
};

class Fire : public Obstacle
{
void action(){ cout<<"Fire."<<endl; }
};

class Water : public Obstacle
{
void action(){ cout<<"Water."<<endl; }
};

class Player
{
public:
virtual void interactWith(Obstacle*)=0;
};

class MM : public Player
{
public:
void interactWith(Obstacle* ob)
{
cout<<"MM:";
ob->action();
}
};

class GG : public Player
{
public:
void interactWith(Obstacle* ob)
{
cout<<"GG:";
ob->action();
}
};

class GameFactory
{
public:
virtual Player* makePlayer()=0;
virtual Obstacle* makeObstacle()=0;
};

class GGandWater : public GameFactory
{
virtual Player* makePlayer(){ return new GG; }
virtual Obstacle* makeObstacle(){ return new Water; }
};

class MMandFire : public GameFactory
{
virtual Player* makePlayer(){ return new MM; }
virtual Obstacle* makeObstacle(){ return new Fire; }
};

class CreateGame //综合管理
{
GameFactory* gef;
Player* p;
Obstacle* ob;
public:
CreateGame(GameFactory* factory) : gef(factory),p(factory->makePlayer()),ob(factory->makeObstacle()){}
void player(){ p->interactWith(ob); }
~CreateGame()
{
delete gef;
delete p;
delete ob;
}
};

int main()
{
CreateGame g1(new GGandWater);
g1.player();
CreateGame g2(new MMandFire);
g2.player();
return 0;
}
相关标签: 设计模式 工作