Abstract Factory
跟Simple Factory不同的是
進而將Factory抽象化,實現不同的工廠
必要時工廠能夠快速的抽換
例如在版本控制或是跨平台
class Iproduct_Food {
public:
virtual void eat(){}
};
class Iproduct_Game {
public:
virtual void play(){}
};
class abstract_factory {
public:
virtual Iproduct_Food* create_Food(){}
virtual Iproduct_Game* create_Game(){}
};
class good_food : public Iproduct_Food {
public:
virtual void eat()
{
cout << "eating good food\n";
}
};
class good_game : public Iproduct_Game {
public:
virtual void play()
{
cout << "playing good game\n";
}
};
class good_factory : public abstract_factory {
public:
virtual Iproduct_Food* create_Food()
{
return new good_food();
}
virtual Iproduct_Game* create_Game()
{
return new good_game();
}
};
class bad_food : public Iproduct_Food {
public:
virtual void eat()
{
cout << "eating bad food\n";
}
};
class bad_game : public Iproduct_Game {
public:
virtual void play()
{
cout << "playing bad game\n";
}
};
class bad_factory : public abstract_factory {
public:
virtual Iproduct_Food* create_Food()
{
return new bad_food();
}
virtual Iproduct_Game* create_Game()
{
return new bad_game();
}
};
int main()
{
abstract_factory* f1 = new good_factory();
abstract_factory* f2 = new bad_factory();
f1->create_Food()->eat();
f1->create_Game()->play();
f2->create_Food()->eat();
f2->create_Game()->play();
return 0;
}
2008年4月4日 星期五
Abstract Factory
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言