实验4:抽象工厂模式
[实验任务一]:人与肤色
源码:
#include<iostream> using namespace std; class Man; class Woman; class AbstractFactory{ public: virtual Man* createMan() = 0; virtual Woman* createWoman() = 0; }; class Man { public: virtual void maneat() = 0; }; class Woman { public: virtual void womaneat() = 0; }; class YellowMan :public Man { public: void maneat() { cout << "Yellow man" << endl; } }; class YellowWoman :public Woman { public: void womaneat() { cout << "Yellow woman" << endl; } }; class BlackMan :public Man { public: void maneat() { cout << "Black man" << endl; } }; class BlackWoman :public Woman { public: void womaneat() { cout << "Black woman" << endl; } }; class WhiteMan :public Man { public: void maneat() { cout << "white man" << endl; } }; class WhiteWoman :public Woman { public: void womaneat() { cout << "white woman" << endl; } }; class YellowHumanFactory :public AbstractFactory { public: YellowMan* createMan(){ return new YellowMan(); } YellowWoman* createWoman(){ return new YellowWoman(); } }; class BlackHumanFactory :public AbstractFactory { public: BlackMan* createMan(){ return new BlackMan(); } BlackWoman* createWoman(){ return new BlackWoman(); } }; class WhiteHumanFactory :public AbstractFactory { public: WhiteMan* createMan(){ return new WhiteMan(); } WhiteWoman* createWoman(){ return new WhiteWoman(); } }; class Client { public: AbstractFactory* abstractFactory; Man* man; Woman* woman; void excute(){ for (int i = 0; i < 3; i++) { if (i==0) abstractFactory = new YellowHumanFactory(); else if (i==1) abstractFactory = new WhiteHumanFactory(); else abstractFactory = new BlackHumanFactory(); man = abstractFactory->createMan(); woman = abstractFactory->createWoman(); man->maneat(); woman->womaneat(); } } }; class _TCHAR; int _tmain(int argc, _TCHAR* argv[]) { Client* client = new Client(); client->excute(); system("pause"); return 0; }
标签:Woman,maneat,软件设计,virtual,public,实验,class,Man From: https://www.cnblogs.com/lyf3701/p/16770310.html