设计模式(一)

C++实现

Posted by Gavin on October 31, 2019

少年心事

邈如春梦

概念

设计模式

设计模式是某类软件设计问题的经典解决方案,它可以提高代码可重用性,增强系统的可维护性。

分类:

  • 创建型模式
    类实例化时使用的模式,规定了创建对象的规则和方法,例如单例模式工厂模式
  • 结构型模式
    从程序结构上解决模块之间的耦合性问题,例如代理模式
  • 行为型模式
    描述了算法和对象之间的职责分配,例如迭代器模式

单例模式

一个类只有一个实例,并且提供一个全局访问点,该实例被所有程序模块共享,应用于任务管理器网站计数器等等

懒汉模式

std::mutex hasInstance;

class LasySingleten
{
public:
	static LasySingleten* getInstance()
	{
		if (instance == nullptr)
		{
			hasInstance.lock();
			instance = new LasySingleten();
			hasInstance.unlock();
		}
		return instance;
	}
	~LasySingleten()
	{
		delete instance;
	};
private:
	LasySingleten() {};
	static LasySingleten* instance;
};
LasySingleten* LasySingleten::instance = nullptr;

饿汉模式

class HungrySingleten
{
public:
	static HungrySingleten* getInstance()
	{
		static HungrySingleten instance;
		return &instance;
	}
	~HungrySingleten() {};
private:
	HungrySingleten() {};
};

工厂模式

简单工厂

由工厂对类进行实例化,每增加一个新类,就需要修改工厂类

class AbstractProduct
{
public:
	virtual void operate() {};
	virtual ~AbstractProduct() {};	
};

class ProductA : public AbstractProduct
{
public:
	void operate()
	{
		std::cout << "ProductA" << std::endl;
	}
};

class ProductB : public AbstractProduct
{
public:
	void operate()
	{
		std::cout << "ProductB" << std::endl;
	}
};

class Factory
{
public:
	AbstractProduct* createProduct(char type)
	{
		AbstractProduct* product = nullptr;
		switch (type)
		{
			case 'A' : product = new ProductA(); break;
			case 'B' : product = new ProductB(); break;
		}
		return product;
	}
};

工厂方法

把简单工厂中的工厂类抽象出一个接口,此接口只实现创建抽象产品的工厂方法,要生产具体类的工厂就去实现这个接口。

#include <iostream>

class AbstractProduct
{
public:
	virtual void operate() {};
	virtual ~AbstractProduct() {};	
};

class ProductA : public AbstractProduct
{
public:
	void operate()
	{
		std::cout << "ProductA" << std::endl;
	}
};

class ProductB : public AbstractProduct
{
public:
	void operate()
	{
		std::cout << "ProductB" << std::endl;
	}
};

class AbstractFactory
{
public:
	virtual AbstractProduct* produce();
};

class FactoryA : public AbstractProduct
{
public:
	AbstractProduct* produce()
	{
		return new ProductA();
	}
};

class FactoryB : public AbstractProduct
{
public:
	AbstractProduct* produce()
	{
		return new ProductB();
	}
};

int main(int argc, char const *argv[])
{
	FactoryA factory;
	AbstractProduct *pa = factory.produce();
	pa->operate();

	FactoryB factoryb;
	AbstractProduct *pb = factoryb.produce();
	pb->operate();
	return 0;
}

抽象工厂

增加了工厂生产产品的种类,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类

#include <iostream>

class AbstractProductA
{
public:
	virtual void operate() {};
};

class ProductA1 : public AbstractProductA
{
public:
	void operate()
	{
		std::cout << "ProductA1" << std::endl;
	}
};

class ProductA2 : public AbstractProductA
{
public:
	void operate()
	{
		std::cout << "ProductA2" << std::endl;
	}
};

class AbstractProductB
{
public:
	virtual void operate() {};
};

class ProductB1 : public AbstractProductB
{
public:
	void operate()
	{
		std::cout << "ProductB1" << std::endl;
	}
};

class ProductB2 : public AbstractProductB
{
public:
	void operate()
	{
		std::cout << "ProduceB2" << std::endl;
	}
};

class AbstractFactory
{
public:
	virtual AbstractProductA* produceA() = 0;
	virtual AbstractProductB* produceB() = 0;
};

class FactoryA : public AbstractFactory
{
public:
	AbstractProductA* produceA()
	{
		return new ProductA1();
	}
	AbstractProductB* produceB()
	{
		return new ProductB1();
	}
};

class FactoryB : public AbstractFactory
{
public:
	AbstractProductA* produceA()
	{
		return new ProductA2();
	}
	AbstractProductB* produceB()
	{
		return new ProductB2();
	}
};

int main(int argc, char const *argv[])
{
	FactoryA factorya;
	AbstractProductA *pa1 = factorya.produceA();
	pa1->operate();
	AbstractProductB *pb1 = factorya.produceB();
	pb1->operate();

	FactoryB factoryb;
	AbstractProductA *pa2 = factoryb.produceA();
	pa2->operate();
	AbstractProductB *pb2 = factoryb.produceB();
	pb2->operate();
	return 0;
}

代理模式

代理模式提供一种其他对象代理操作本对象的方法

简单代理

#include <iostream>

class Subject
{
public:
	virtual void request() {};
	virtual ~Subject() {};
};

class RealSubject : public Subject
{
public:
	void request()
	{
		std::cout << "Request" << std::endl;
	}
};

class Proxy : public Subject
{
	Subject *subject;
public:
	Proxy(Subject *_subject)
	{
		subject = _subject;
	}
	void request()
	{
		std::cout << "Proxy" << std::endl;
		subject->request();
	}
	~Proxy()
	{
		delete subject;
	}	
};

int main(int argc, char const *argv[])
{
	Subject *subject = new RealSubject();
	Subject *proxy = new Proxy(subject);
	proxy->request();
	return 0;
}