欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

设计模式--抽象工厂模式(C++)

时间:2023-06-04

抽象工厂可以理解为简单工厂模式和工厂方法模式的结合,抽象工厂模式拥有多个工厂,每个工厂又可以生产多种产品;可大致理解为简单工厂模式是一个工厂,自己一对多;工厂方法模式是多个工厂,每个工厂一对一;抽象工厂模式是多个工厂,每个工厂一对多;

1.产品接口Human (人类,其子类有黄种人、白种人)

#pragma onceclass Human{public:Human();virtual ~Human();virtual void character() = 0;virtual void Sex() = 0;private:};Human::Human(){}Human::~Human(){}

2.抽象产品1 WhiteHuman (白种人,其子类有白种男孩、白种女孩)

#pragma once#include "Human.h"#include class WhiteHuman:public Human{public:WhiteHuman();virtual ~WhiteHuman();void character();virtual void Sex()=0;private:};WhiteHuman::WhiteHuman(){}WhiteHuman::~WhiteHuman(){}void WhiteHuman::character(){std::cout << "白种人性格贪婪" << std::endl;}

2.1 具体产品1 (白种男孩)

#pragma once#include "WhiteHuman.h"class WhiteBoyHuman:public WhiteHuman{public:WhiteBoyHuman();~WhiteBoyHuman();void Sex();private:};WhiteBoyHuman::WhiteBoyHuman(){}WhiteBoyHuman::~WhiteBoyHuman(){}void WhiteBoyHuman::Sex(){std::cout << "白种男孩的性别是男" << std::endl;}

2.2 具体产品2 (白种女孩)

#pragma once#include "WhiteHuman.h"class WhiteGirlHuman:public WhiteHuman{public:WhiteGirlHuman();~WhiteGirlHuman();void Sex();private:};WhiteGirlHuman::WhiteGirlHuman(){}WhiteGirlHuman::~WhiteGirlHuman(){}void WhiteGirlHuman::Sex(){std::cout << "白种女孩的性别是女" << std::endl;}

3.抽象产品2 YellowHuman(黄种人,其子类有黄种男孩、黄种女孩)

#pragma once#include "Human.h"#include class YellowHuman:public Human{public:YellowHuman();~YellowHuman();void character();void Sex()=0;private:};YellowHuman::YellowHuman(){}YellowHuman::~YellowHuman(){}void YellowHuman::character(){std::cout << "黄种人性格温良" << std::endl;}

3.1 具体产品1 (黄种男孩)

#pragma once#include "YellowHuman.h"class YellowBoyHuman:public YellowHuman{public:YellowBoyHuman();~YellowBoyHuman();void Sex();private:};YellowBoyHuman::YellowBoyHuman(){}YellowBoyHuman::~YellowBoyHuman(){}void YellowBoyHuman::Sex(){std::cout << "黄种男孩的性别是男" << std::endl;}

3.2 具体产品2 (黄种女孩)

#pragma once#include "YellowHuman.h"class YellowGirlHuman:public YellowHuman{public:YellowGirlHuman();~YellowGirlHuman();void Sex();private:};YellowGirlHuman::YellowGirlHuman(){}YellowGirlHuman::~YellowGirlHuman(){}void YellowGirlHuman::Sex(){std::cout << "黄种女孩的性别是女" << std::endl;}

4.抽象工厂

#pragma once#include "Human.h"#include "WhiteHuman.h"#include "YellowHuman.h"#include#include class CreatHumanFactory{public:CreatHumanFactory();virtual ~CreatHumanFactory();virtual Human* creatWhiteHuman() = 0;virtual Human* creatYellowHuman() = 0;private:};CreatHumanFactory::CreatHumanFactory(){}CreatHumanFactory::~CreatHumanFactory(){}

5.男人工厂 (生产白种男孩、黄种男孩)

#pragma once#include "CreatHumanFactory.h"#include "WhiteHuman.h"#include "WhiteBoyHuman.h"#include "YellowBoyHuman.h"class CreateBoyHumanFactory :public CreatHumanFactory{public:CreateBoyHumanFactory();~CreateBoyHumanFactory();Human* creatWhiteHuman();Human* creatYellowHuman();private:};CreateBoyHumanFactory::CreateBoyHumanFactory(){}CreateBoyHumanFactory::~CreateBoyHumanFactory(){}Human* CreateBoyHumanFactory::creatWhiteHuman(){return new WhiteBoyHuman();}Human* CreateBoyHumanFactory::creatYellowHuman(){return new YellowBoyHuman();}

6.女人工厂 (生产白种女孩、黄种女孩)

#pragma once#include "CreatHumanFactory.h"#include "YellowHuman.h"#include "WhiteGirlHuman.h"#include "YellowGirlHuman.h"class CreateGirlHumanFactory:public CreatHumanFactory{public:CreateGirlHumanFactory();~CreateGirlHumanFactory();Human* creatWhiteHuman();Human* creatYellowHuman();private:};CreateGirlHumanFactory::CreateGirlHumanFactory(){}CreateGirlHumanFactory::~CreateGirlHumanFactory(){}Human* CreateGirlHumanFactory::creatWhiteHuman(){return new WhiteGirlHuman();}Human* CreateGirlHumanFactory::creatYellowHuman(){return new YellowGirlHuman();}

7.主程序

// 抽象工厂模式.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。//#include "CreatHumanFactory.h"#include "CreateBoyHumanFactory.h"#include "CreateGirlHumanFactory.h"#include "Human.h"#include "WhiteHuman.h"#include "YellowHuman.h"#include int main(){ CreateBoyHumanFactory* pBoyFactory = new CreateBoyHumanFactory(); Human* pWhiteBoy = pBoyFactory->creatWhiteHuman(); pWhiteBoy->character(); pWhiteBoy->Sex();Human* pYellowBoy = pBoyFactory->creatYellowHuman(); pYellowBoy->character(); pYellowBoy->Sex();delete pBoyFactory;delete pWhiteBoy;delete pYellowBoy;std::cout << std::endl;CreateGirlHumanFactory* pGirlFactory = new CreateGirlHumanFactory();Human* pWhiteGirl = pGirlFactory->creatWhiteHuman();pWhiteGirl->character();pWhiteGirl->Sex();Human* pYellowGirl = pGirlFactory->creatYellowHuman();pYellowGirl->character();pYellowGirl->Sex();delete pGirlFactory;delete pWhiteGirl;delete pYellowGirl; system("pause"); return 0;}

8.结果

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。