设计模式(策略模式、单例模式模板、享元模式)

1.享元模式

类图


#include "stdafx.h"
#include <iostream>
#include <map>
using namespace std;
/********************************************享元模式*******************************************/
#if 1
class Character  
{
public:
	virtual ~Character(){};

	virtual void SetSize(int, int) = 0;
	virtual void Display() = 0;
protected:
	Character(){};
	char m_chSymbol;
	int m_nWeight;
	int m_nHeight;
};

class CharacterA : public Character
{
public:
	CharacterA();
	virtual ~CharacterA();

	void SetSize(int, int);
	void Display();
};

CharacterA::CharacterA()
{
	this->m_chSymbol = 'A';
	this->m_nWeight = 100;
	this->m_nHeight = 200;
}

CharacterA::~CharacterA()
{

}
void CharacterA::SetSize(int nWeight, int nHeight)
{
	this->m_nWeight = nWeight;
	this->m_nHeight = nHeight;
}
void CharacterA::Display()
{
	cout << "CharacterA:" << m_chSymbol << "(" << m_nWeight << "," << m_nHeight << ")" << endl;
}

class CharacterB : public Character
{
public:
	CharacterB();
	virtual ~CharacterB();

	void SetSize(int, int);
	void Display();
};

CharacterB::CharacterB()
{
	this->m_chSymbol = 'B';
	this->m_nWeight = 100;
	this->m_nHeight = 200;
}

CharacterB::~CharacterB()
{

}

void CharacterB::SetSize(int nWeight, int nHeight)
{
	this->m_nWeight = nWeight;
	this->m_nHeight = nHeight;
}

void CharacterB::Display()
{
	cout << "CharacterB:" << m_chSymbol << "(" << m_nWeight << "," << m_nHeight << ")" << endl;
}

class CharacterFactory  
{
public:
	CharacterFactory();
	virtual ~CharacterFactory();

	Character* GetCharacter(char);
private:
	std::map<char, Character*> m_mChar;
};

CharacterFactory::CharacterFactory()
{
	m_mChar.insert(make_pair<char, Character*>('A', new CharacterA));
	m_mChar.insert(make_pair<char, Character*>('B', new CharacterB));
}

CharacterFactory::~CharacterFactory()
{
	map<char,Character *>::iterator iter = m_mChar.begin();
	for(;iter != m_mChar.end();++iter)
	{
		delete iter->second;
		iter->second = NULL;
	}
	m_mChar.clear();
}

Character* CharacterFactory::GetCharacter(char chIn)
{
	map<char, Character*>::iterator it = m_mChar.find(chIn);
	if(it != m_mChar.end())
	{
		return (Character*)it->second;
	}

	return NULL;
}

int _tmain(int argc, _TCHAR* argv[])
{
	CharacterFactory* pFactory = new CharacterFactory;


	Character* ch1 = pFactory->GetCharacter('A');
	ch1->Display();


	Character* ch2 = pFactory->GetCharacter('B');
	ch2->SetSize(500, 800);
	ch2->Display();
	system("pause");


	return 0;
}
#endif 

运行结果


优点:

1、享元模式的优点在于它能够极大的减少系统中对象的个数。

2、享元模式由于使用了外部状态,外部状态相对独立,不会影响到内部状态,所以享元模式使得享元对象能够在不同的环境被共享。

缺点:

1、由于享元模式需要区分外部状态和内部状态,使得应用程序在某种程度上来说更加复杂化了。

2、为了使对象可以共享,享元模式需要将享元对象的状态外部化,而读取外部状态使得运行时间变长。


2.策略模式

#if 1
class WeaponBehavior
{
public:
	void virtual useWeapon() = 0;
};

class AK47:public WeaponBehavior
{
public:
	void useWeapon()
	{
		cout << "Use AK47 to shoot!" << endl;
	}
};

class Knife:public WeaponBehavior
{
public:
	void useWeapon()
	{
		cout << "Use Knife to kill!" << endl;
	}
};

class Character
{
public:
	Character()
	{
		weapon = 0;
	}
	void setWeapon(WeaponBehavior *w)
	{
		this->weapon = w;
	}
	void virtual fight() = 0;
protected:
	WeaponBehavior *weapon;
};

class King:public Character
{
public:
	void fight()
	{
		cout << "The king:" ;
		if ( this->weapon == NULL)
		{
			cout << "You don't have a weapon! Please Set Weapon!" << endl;
		}
		else
		{ 
			weapon->useWeapon();
		}
	}
};
int main()
{    
	WeaponBehavior *ak47 = new AK47();
	WeaponBehavior *knife = new Knife();     

	Character *kin = new King();      

	kin->fight();   
	cout << endl; 

	kin->setWeapon(ak47);
	kin->fight();
	cout << endl;

	kin->setWeapon(knife);
	kin->fight();
	system("pause");
	return 0;
}


#endif 

运行结果


优点:

  1、 简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。
  2、 避免程序中使用多重条件转移语句,使系统更灵活,并易于扩展。
       3、 遵守大部分GRASP原则和常用设计原则,高内聚、低偶合。

缺点:
  1、 因为每个具体策略类都会产生一个新类,所以会增加系统需要维护的类的数量。

       2、 在基本的策略模式中,选择所用具体实现的职责由客户端对象承担,并转给策略模式的Context对象

3.单例模式

参照:https://www.cnblogs.com/qiaoconglovelife/p/5851163.html     点击打开链接

这个链接已经讲的很清楚了,下来给你们看下单例模式的模板

#include<mutex>
#include<memory>
#include<iostream>
template<class T>
class Singleton{
public:
	template<class ...Args>
	static inline std::shared_ptr<T> instance(Args&&... args){
		if (!instance_.get()){
			std::unique_lock<std::mutex> lock(instanceMutex_);
			if (!instance_.get()){
				instance_.reset(new T(std::forward<Args>(args)...));
			}
		}
		return instance_;
	}
private:
	Singleton() = default;
	virtual ~Singleton() = default;
	Singleton(const Singleton&) = default;
	Singleton& operator=(const Singleton&) = delete;
private:
	static std::shared_ptr<T> instance_;
	static std::mutex instanceMutex_;
};

template<class T>
std::shared_ptr<T> Singleton<T>::instance_;

template<class T>
std::mutex Singleton<T>::instanceMutex_;

#define SINGLETON_DECL(type) \
	friend class Singleton<type>; \
	friend class std::shared_ptr<type>; \


class Test1{
public:
	Test1(int age, std::string name) :age_(age), name_(name){
		std::cout << "Test1" << std::endl; 
	}
private:
	SINGLETON_DECL(Test1);
	int age_;
	std::string name_;
};
class Test2{
public:
	Test2(int age, std::string name,std::string addr) :age_(age), name_(name),addr_(addr){
		std::cout << "Test2" << std::endl;
	}
private:
	SINGLETON_DECL(Test2);
	int age_;
	std::string name_;
	std::string addr_;
};
class Test3{
public:
	Test3() {
		std::cout << "Test3" << std::endl;
	}
private:
	SINGLETON_DECL(Test3);
	int age_;
	std::string name_;
};
int main(){
	 
	auto test = Singleton<Test3>::instance();
	std::cin.get();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值