赞
踩
目录
网上关于这3个模式的文章很多,这里主要是加入本人自己的通俗理解。
就好比造军舰,当你没有条件搞那么多造船厂的时候,只有一个造船厂,那你这个造船厂就得什么军舰都造。来了航母订单造航母,来了驱逐舰订单就造驱逐舰。这就是简单工厂模式。
当你发达了,有条件搞造船厂了,你就建了航母船厂、驱逐舰船厂、护卫舰船厂,他们各司其职。来了订单一看,是航母就交给航母船厂;是驱逐舰就交给驱逐舰船厂。这就是工厂模式。
再后来你面临严峻外部挑战,需要造舰队了,你给每个舰队都配了造船厂,每个船厂都可以造多类型军舰。东海舰队要负责一统河山,要打鬼子,要打鹰酱。所以东海舰队船厂需要造003、055。北海舰队负责渤海、黄海,拱卫京师。所以北海舰队船厂造001、054就够了。这就是抽象工厂模式。
题外话,
江南造船厂 代表产品:003型航母、055型万吨大驱、052D驱逐舰、039常规动力潜艇。
大连造船厂 代表产品:“辽宁”号航母、“山东”号航母、055型万吨大驱、052D驱逐舰。
沪东造船厂 代表产品:075型两栖攻击舰、071型综合登陆舰、054A护卫舰、726气垫登陆艇。
⭐ 简单工厂模式,其内部包含必要的逻辑判断,可以根据客户端的选择动态实例化相关的类。 |
class Warship { public: Warship() { std::cout << "Warship class construction" << endl; } virtual ~Warship() { std::cout << "Warship class destruction" << endl; }
public: virtual void Completed() = 0; };
//航母 class Carrier :public Warship { public: Carrier() { std::cout << "Carrier construction" << endl; } ~Carrier() { std::cout << "Carrier destruction" << endl; } void Completed() { std::cout << "Carrier Completed! " <<endl; } };
//驱逐舰 class Destroyer: public Warship { public: Destroyer() { std::cout << "Destroyer construction" << endl; } ~Destroyer() { std::cout << "Destroyer destruction" << endl; }
void Completed() { std::cout << "Destroyer Completed! " << endl; } };
//护卫舰 class Firgate : public Warship { public: Firgate() { std::cout << "Firgate construction" << endl; } ~Firgate() { std::cout << "Firgate destruction" << endl; }
void Completed() { std::cout << "Firgate Completed! " << endl; } };
class WarshipSimpleFactory { public: Warship *CreateWarship(int nType) { Warship *oper; switch (nType) { case 1: oper = new Carrier; break; case 2: oper = new Destroyer; break; case 3: default: oper = new Firgate; break; } return oper; }
}; int main() { //简单工厂模式 WarshipSimpleFactory *factory = new WarshipSimpleFactory(); Warship * warship = factory->CreateWarship(1); warship->Completed(); delete warship; warship = NULL; delete factory; factory = NULL;
return 0; } |
⭐ 定义一个用于创建对象的接口,让子类决定实例化哪一个类。使一个类的实例化延迟到其子类。 |
class Warship { public: Warship() { std::cout << "Warship class construction" << endl; } virtual ~Warship() { std::cout << "Warship class destruction" << endl; }
public: virtual void Completed() = 0; };
//航母 class Carrier :public Warship { public: Carrier() { std::cout << "Carrier construction" << endl; } ~Carrier() { std::cout << "Carrier destruction" << endl; } void Completed() { std::cout << "Carrier Completed! " <<endl; } };
//驱逐舰 class Destroyer: public Warship { public: Destroyer() { std::cout << "Destroyer construction" << endl; } ~Destroyer() { std::cout << "Destroyer destruction" << endl; }
void Completed() { std::cout << "Destroyer Completed! " << endl; } };
//护卫舰 class Firgate : public Warship { public: Firgate() { std::cout << "Firgate construction" << endl; } ~Firgate() { std::cout << "Firgate destruction" << endl; }
void Completed() { std::cout << "Firgate Completed! " << endl; } };
class WarshipFactory { public: virtual Warship* CreateWarship() = 0;
};
class CarrierFactory :public WarshipFactory { public: virtual Warship* CreateWarship() { return new Carrier(); } };
class DestoryFactory :public WarshipFactory { public: virtual Warship* CreateWarship() { return new Destroyer(); } };
class FirgateFactory :public WarshipFactory { public: virtual Warship* CreateWarship() { return new Firgate(); } };
int main() { //工厂模式 WarshipFactory *cfactory = new CarrierFactory(); Warship *cwarship = cfactory->CreateWarship(); cwarship->Completed(); delete cwarship; cwarship = NULL; delete cfactory; cfactory = NULL;
WarshipFactory *dfactory = new DestoryFactory(); Warship *dwarship = dfactory->CreateWarship(); dwarship->Completed(); delete dwarship; dwarship = NULL; delete dfactory; dfactory = NULL;
WarshipFactory *ffactory = new FirgateFactory(); Warship *fwarship = ffactory->CreateWarship(); fwarship->Completed(); delete fwarship; fwarship = NULL; delete ffactory; ffactory = NULL;
return 0; } |
⭐ 提供一个创建一系列相关或者相互依赖对象的接口,而无需指定他们具体的类。 |
class Warship { public: Warship() { std::cout << "Warship class construction" << endl; } virtual ~Warship() { std::cout << "Warship class destruction" << endl; }
public: virtual void Completed() = 0; };
//001航母 class Carrier001 :public Warship { public: Carrier001() { std::cout << "Carrier001 construction" << endl; } ~Carrier001() { std::cout << "Carrier001 destruction" << endl; } void Completed() { std::cout << "Carrier001 Completed! " << endl; } }; //003航母 class Carrier003 :public Warship { public: Carrier003() { std::cout << "Carrier003 construction" << endl; } ~Carrier003() { std::cout << "Carrier003 destruction" << endl; } void Completed() { std::cout << "Carrier003 Completed! " << endl; } };
//055驱逐舰 class Destroyer055 : public Warship { public: Destroyer055() { std::cout << "Destroyer055 construction" << endl; } ~Destroyer055() { std::cout << "Destroyer055 destruction" << endl; }
void Completed() { std::cout << "Destroyer055 Completed! " << endl; } }; //054驱逐舰 class Destroyer054 : public Warship { public: Destroyer054() { std::cout << "Destroyer054 construction" << endl; } ~Destroyer054() { std::cout << "Destroyer054 destruction" << endl; }
void Completed() { std::cout << "Destroyer054 Completed! " << endl; } };
class AbstractWarshipFactory { public: virtual Warship* CreateCarrier() = 0; virtual Warship* CreateDestroy() = 0; };
//东海舰队的造船厂 class DHJDFactory :public AbstractWarshipFactory { public: virtual Warship* CreateCarrier() { return new Carrier003(); } virtual Warship* CreateDestroy() { return new Destroyer055(); } }; //北海舰队的造船厂 class BHJDFactory :public AbstractWarshipFactory { public: virtual Warship* CreateCarrier() { return new Carrier001(); } virtual Warship* CreateDestroy() { return new Destroyer054(); } };
int main() { //抽象工厂模式 AbstractWarshipFactory *dhjdfactory = new DHJDFactory(); Warship *dCarrier = dhjdfactory->CreateCarrier(); dCarrier->Completed(); Warship *dDestroy = dhjdfactory->CreateDestroy(); dDestroy->Completed(); delete dCarrier; dCarrier = NULL; delete dDestroy; dDestroy = NULL;
AbstractWarshipFactory *bhjdfactory = new BHJDFactory(); Warship *bCarrier = bhjdfactory->CreateCarrier(); bCarrier->Completed(); Warship *bDestroy = bhjdfactory->CreateDestroy(); bDestroy->Completed(); delete bCarrier; bCarrier = NULL; delete bDestroy; bDestroy = NULL;
return 0; }
|
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。