软件设计——抽象工厂模式之人与肤色 c++

1、类图

2、源代码

test4.cpp

#include<iostream>
#include<string>
using namespace std;

//抽象产品类 男人
class Man
{
public:
	virtual void makeM()  = 0;
};
//具体产品类 白色男人
class WhiteMan : public Man
{
public:
	void makeM()
	{
		cout << "我是白种男人"<< endl;
	}
};
//具体产品类 黄色男人
class YellowMan : public Man
{
public:
	void makeM()
	{
		cout << "我是黄种男人" << endl;
	}
};
//具体产品类  黑色男人
class BlackMan : public Man
{
public:
	void makeM()
	{
		cout << "我是黑种男人" << endl;
	}
};

//抽象产品类 女人
class Woman
{
public:
	virtual void makeW() = 0;
};
//具体产品类 白色女人
class WhiteWoman : public Woman
{
public:
	void makeW()
	{
		cout << "我是白种女人" << endl;
	}
};
//具体产品类 黄色女人
class YellowWoman : public Woman
{
public:
	void makeW()
	{
		cout << "我是黄种女人" << endl;
	}
};
//具体产品类 黑色女人
class BlackWoman : public Woman
{
public:
	void makeW()
	{
		cout << "我是黑种女人" << endl;
	}
};
//抽象工厂类  肤色
class Color
{
public:
	virtual Man *produceMan() = 0;
	virtual Woman *produceWoman() = 0;
};
//具体工厂类 黄色
class Yellow : public Color
{
public:
	Man *produceMan()
	{
		return new YellowMan();
	}
	Woman *produceWoman()
	{
		return new YellowWoman();
	}
};
//具体工厂类 白色
class White : public Color
{
public:
	Man* produceMan()
	{
		return new  WhiteMan();
	}
	Woman *produceWoman()
	{
		return new  WhiteWoman();
	}
};
//具体工厂类 黑色
class Black : public Color
{
public:
	Man * produceMan()
	{
		return  new BlackMan();
	}
	Woman * produceWoman()
	{
		return new BlackWoman();
	}
};
//主函数
int main(int argc, char*argv[])
{
	Color * color_y = new Yellow();
	Man * man_y = color_y->produceMan();
	Woman * woman_y = color_y->produceWoman();

	Color * color_b = new Black();
	Man * man_b = color_b->produceMan();
	Woman * woman_b = color_b->produceWoman();

	Color * color_w = new White();
	Man * man_w = color_w->produceMan();
	Woman * woman_w = color_w->produceWoman();

	string t ;
	string y = "Yellow";
	string b = "Black";
	string w = "White";
	string e = "exit";
	while (strcmp(t.c_str(), e.c_str()) != 0)  
	{
		cout << "请输入标识符 ( 首字母大写 ):";
		cin >> t;
		if (strcmp(t.c_str(), y.c_str()) == 0)
		{
			man_y->makeM();
			woman_y->makeW();
		}
		else if (strcmp(t.c_str(), w.c_str()) == 0) {
			man_w->makeM();
			woman_w->makeW();
		}
		else if (strcmp(t.c_str(), b.c_str()) == 0) {
			man_b->makeM();
			woman_b->makeW();
		}
		else if (strcmp(t.c_str(), e.c_str()) == 0) {   //输入exit退出系统
			cout << "退出" << endl;
		}
		else {
			cout << "输入的标识符有误,请重新输入!" << endl;
		}
		cout << endl;
	}


		if (man_y != NULL)
		{
			delete man_y;
			man_y = NULL;
		}

		if (woman_y != NULL)
		{
			delete woman_y;
			woman_y = NULL;
		}

		if (man_b != NULL)
		{
			delete man_b;
			man_b = NULL;
		}

		if (woman_b != NULL)
		{
			delete woman_b;
			woman_b = NULL;
		}

		if (man_w != NULL)
		{
			delete man_w;
			man_w = NULL;
		}

		if (woman_w != NULL)
		{
			delete woman_w;
			woman_w = NULL;
		}
}

3、遇到的问题

3.1 指针使用不正确,应如如下代码

//抽象工厂类  肤色
class Color
{
public:
	virtual Man *produceMan() = 0;
	virtual Woman *produceWoman() = 0;
};

3.2 字符串初始化报错

string t = NULL;

错误:

0x79C608F0 (ucrtbased.dll)处(位于 test4.exe 中)引发的异常: 0xC0000005: 读取位置 0x00000000 时发生访问冲突。

具体原因不太清楚,可能和VS版本有关,据说VS2008支持这样写。

4、运行截图

原文地址:https://www.cnblogs.com/ltw222/p/15343402.html