设计模式之简单工厂模式、工厂方法模式、抽象工厂模式(C++)

1. 简单工厂

简单工厂模式里通常包含一个工厂类,用于创建实例。包含一个抽象类以及多个具体类。
向工厂类中传入参数,工厂类就可以实例化出具体的对象。

下面创建了一个生产Room的工厂类RoomFactory

  • 工厂类RoomFactory使用CreateRoom生产Room,工厂类RoomFactory作用于抽象类AbstractRoom
  • 抽象类AbstractRoom含有 ShowName的行为,
  • 具体类 SmallRoom MiddleRoom BigRoom继承ShowName的行为。

UML 图

设计模式之简单工厂模式、工厂方法模式、抽象工厂模式(C++)_第1张图片

代码实现

test1函数:首先实例化工厂类RoomFactory,之后向public函数CreateRoom传入 `“big”"small"分别得到
bighouse和smallhouse。

test2函数:首先实例化工厂类RoomFactory,之后向public函数CreateRoom传入 `“big”"middle"分别得到
BigRoom和MiddleRoom的house。

#define _CRT_SECURE_NO_WARNING
#include 
using namespace std;

class AbstractRoom {
public:
	virtual void ShowName() = 0;
};

class BigRoom : public AbstractRoom
{
public:
	virtual void ShowName()
	{
		cout << "This is bigroom" << endl;
	}
};

class SmallRoom :public AbstractRoom
{
public:
	virtual void ShowName()
	{
		cout << "This is smallroom" << endl;
	}
};

class MiddleRoom :public AbstractRoom
{
public:
	virtual void ShowName()
	{
		cout << "This is middleroom" << endl;
	}
};


class RoomFactory
{
public:
	static AbstractRoom* CreateRoom(string size)
	{
		if (size == "small")
		{
			return new SmallRoom;
		}
		else if (size == "middle")
		{
			return new MiddleRoom;
		}
		else if(size == "big")
		{
			return new BigRoom;
		}
		else
		{
			return NULL;
		}
	}
};


void test1()
{
	RoomFactory* factory = new RoomFactory;
	AbstractRoom* bighouse = factory->CreateRoom("big");
	bighouse->ShowName();
	AbstractRoom* smallhouse = factory->CreateRoom("small");
	smallhouse->ShowName();
}

void test2()
{
	RoomFactory* factory = new RoomFactory;
	AbstractRoom* house = factory->CreateRoom("big");
	house->ShowName();
	delete house;

	house = factory->CreateRoom("middle");
	house->ShowName();
	delete house;

	delete factory;
}

int main()
{
	test1();
	test2();
	return 0;
}

2. 工厂方法

简单工厂模式不符合开闭原则,增加新的产品类时,同时需要修改工厂类。
工厂方法模式对此进行了改进,为每一个产品类提供一个具体工厂类,由具体工厂类决定创建什么产品。
如 BigRoomFactory 创建 BigRoom, MiddleRoomFactory 创建 MiddleRoom, SmallRoomFactory 创建 SmallRoom。
具体工厂类(BigRoomFactory)继承抽象工厂类 (AbstractRoomFactory),具体工厂类(BigRoomFactory)负责生产具体的产品类(BigRoom)。

UML图

设计模式之简单工厂模式、工厂方法模式、抽象工厂模式(C++)_第2张图片

代码实现

#define _CRT_SECURE_NO_WARNING
#include
using namespace std;

class AbstractRoom {
public:
	virtual void ShowName() = 0;
};

class BigRoom :public AbstractRoom{
public:
	virtual void ShowName()
	{
		cout << "This is bigroom" << endl;
	}
};

class SmallRoom :public AbstractRoom {
public:
	virtual void ShowName()
	{
		cout << "This is smallroom" << endl;
	}
};

class MiddleRoom :public AbstractRoom {
public:
	virtual void ShowName()
	{
		cout << "This is middleroom" << endl;
	}
};

class AbstractRoomFactory {
public:
	virtual AbstractRoom* CreatRoom() = 0;
};

class BigRoomFactory :public AbstractRoomFactory {
public:
	virtual AbstractRoom* CreatRoom() {
		return new BigRoom;
	}
};



class SmallRoomFactory :public AbstractRoomFactory {
public:
	virtual AbstractRoom* CreatRoom()
	{
		return new SmallRoom;
	}
};


class MiddleRoomFactory :public AbstractRoomFactory {
public:
	virtual AbstractRoom* CreatRoom()
	{
		return new MiddleRoom;
	}
};

void test3()
{
	AbstractRoomFactory* factory = NULL;
	AbstractRoom* room = NULL;
	factory = new BigRoomFactory;
	room = factory->CreatRoom();
	room->ShowName();
	delete room;
	delete factory;
	
	factory = new SmallRoomFactory;
	room = factory->CreatRoom();
	room->ShowName();
	delete room;
	delete factory;

	factory = new MiddleRoomFactory;
	room = factory->CreatRoom();
	room->ShowName();
	delete room;
	delete factory;
}

int main()
{
	test3();
	return 0;
}

3. 抽象工厂

抽象工厂模式提供一个接口负责创建一系列相互依赖或相关的对象。比如:
ChinaFactory 类生产 ChinaBigRoom 和 ChinaSmallRoom;
AmericaFactory 类生产 AmericaBigRoom 和 AmericaSmallRoom;
JapanFactory 类生产 JapanBigRoom 和 JapanSmallRoom;

UML图

设计模式之简单工厂模式、工厂方法模式、抽象工厂模式(C++)_第3张图片

代码实现

#define _CRT_SECURE_NO_WARNING
#include
using namespace std;


class AbstractBigRoom
{
public:
	virtual void ShowName() = 0;
};

class ChinaBigRoom : public AbstractBigRoom
{
public:
	virtual void ShowName()
	{
		cout << "This is bigroom in China." << endl;
	}
};

class AmericaBigRoom : public AbstractBigRoom {
public:
	virtual void ShowName()
	{
		cout << "This is bigroom in America." << endl;
	}
};

class JapanBigRoom : public AbstractBigRoom {
public:
	virtual void ShowName()
	{
		cout << "This is bigroom in Japan." << endl;
	}
};


class AbstractSmallRoom
{
public:
	virtual void ShowName() = 0;
};

class ChinaSmallRoom : public AbstractSmallRoom
{
public:
	virtual void ShowName()
	{
		cout << "This is smallroom in China." << endl;
	}
};

class AmericaSmallRoom : public AbstractSmallRoom
{
public:
	virtual void ShowName()
	{
		cout << "This is smallroom in America." << endl;
	}
};

class JapanSmallRoom : public AbstractSmallRoom
{
public:
	virtual void ShowName()
	{
		cout << "This is smallroom in Japan." << endl;
	}
};


class AbstractFactory
{
public:
	virtual AbstractBigRoom* CreatBigRoom() = 0;
	virtual AbstractSmallRoom* CreatSmallRoom() = 0;
};

class ChinaFactory : public AbstractFactory
{
public:
	virtual AbstractBigRoom* CreatBigRoom()
	{
		return new ChinaBigRoom;
	}
	virtual AbstractSmallRoom* CreatSmallRoom()
	{
		return new ChinaSmallRoom;
	}
};

class AmericaFactory : public AbstractFactory
{
public:
	virtual AbstractBigRoom* CreatBigRoom()
	{
		return new AmericaBigRoom;
	}
	virtual AbstractSmallRoom* CreatSmallRoom()
	{
		return new AmericaSmallRoom;
	}
};

class JapanFactory : public AbstractFactory
{
public:
	virtual AbstractBigRoom* CreatBigRoom()
	{
		return new JapanBigRoom;
	}
	virtual AbstractSmallRoom* CreatSmallRoom()
	{
		return new JapanSmallRoom;
	}
};

void test4()
{
	AbstractFactory* factory = NULL;
	AbstractBigRoom* bigroom = NULL;
	AbstractSmallRoom* smallroom = NULL;

	factory = new ChinaFactory;
	bigroom = factory->CreatBigRoom();
	smallroom = factory->CreatSmallRoom();
	
	bigroom->ShowName();
	smallroom->ShowName();
	
	delete factory;
	delete bigroom;
	delete smallroom;

	factory = new AmericaFactory;
	bigroom = factory->CreatBigRoom();
	smallroom = factory->CreatSmallRoom();

	bigroom->ShowName();
	smallroom->ShowName();

	delete factory;
	delete bigroom;
	delete smallroom;
}

int main()
{
	test4();
	return 0;
}

你可能感兴趣的:(设计模式,设计模式,c++,uml)