抽象工厂可以理解为简单工厂模式和工厂方法模式的结合,抽象工厂模式拥有多个工厂,每个工厂又可以生产多种产品;可大致理解为简单工厂模式是一个工厂,自己一对多;工厂方法模式是多个工厂,每个工厂一对一;抽象工厂模式是多个工厂,每个工厂一对多;
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
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
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
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
8.结果