设计模式--代码实现(一)

       写博客,是为了总结一下近期学的东西。但是设计模式过于抽象,所以多说无用,放码上来(对着代码理解更容易)。

1、单例模式

包括文件:Singleton.h, Singleton.cpp, main.cpp

Singleton.h 如下:

/*
Design Mode:	Singleton
Author:			Roger
Date:			2020/02/07
*/
#ifndef _SINGLETON_H
#define _SINGLETON_H

#include <iostream>
#include <cstdio>
#include <cstring>
#include <mutex>
#include <thread>

class LazySingleton 
{
public:
	static LazySingleton* getInstance()
	{
		if(Instance == NULL)
		{
			m_mtx.lock();

			//两层的判断为了防止多次创建对象
			if (Instance == NULL)
			{
				Instance = new LazySingleton();

			}
			m_mtx.unlock();
		}
		
		return Instance;
	}

	//内部类用于内存回收
	class GC
	{
	public:
		~ GC()
		{
			if(LazySingleton::Instance != NULL)
			{
				//new与delete需要一一匹配
				delete LazySingleton::Instance;
				LazySingleton::Instance = NULL;
				//std::cout<<"delete Instance"<<std::endl;	//测试
			}
		}
		
	};

	//假设以下是一个连接数据库的方法
	bool connectDB(std::string sql);
private:

	//构造函数私有化,防声明构造。
	LazySingleton()
	{
	}

	//防重构克隆
	LazySingleton(LazySingleton const &);

	//重载等号,防拷贝(返回引用是为了返回自身)
	LazySingleton& operator=(LazySingleton const&);

	static LazySingleton * Instance;
	static std::mutex m_mtx;

	//内部类GC的静态对象会在程序结束时自动析构,届时会释放Instance对象
	static GC gc;	
};

#endif

 Singleton.cpp

/*
Design Mode:	Singleton
Author:			Roger
Date:			2020/02/07
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <mutex>
#include <thread>

#include "Singleton.h"

LazySingleton * LazySingleton::Instance = NULL;
std::mutex LazySingleton::m_mtx;
LazySingleton::GC gc;
bool LazySingleton::connectDB(std::string sql)
{
	bool result = false;
	sql.length() > 0 ? (result = true) : result = false;
	return result;
}

main.cpp:

#include <iostream>
#include <cstdio>
#include <memory>
#include "Singleton.h"

int main() {
	std::string sql;
	std::cout<<"Instance address: "<<LazySingleton::getInstance()<<std::endl;
	std::cin>>sql;
	//使用懒汉模式
	bool result = LazySingleton::getInstance()->connectDB(sql);
	std::cout << result << std::endl;

}

 

单例模式是指该类只能有一个对象,无法构造也无法拷贝。这样做的目的是减少资源浪费,如连接数据库的类,在很多地方会被使用,倘若每个使用到的地方都创建一个对象,会浪费空间,也会增加出错的概率。

 

2、工厂模式

包括文件:GameFrame.h, Football.h, Football.cpp, Basketball.h, Basketball.cpp, main.cpp

其中GameFrame.h中的class gameFrame是游戏的接口,Football.h和Basketball.h的类继承于class  gameFrame,并实现其中的纯虚方法。

GameFrame.h如下:

/*
Design Mode:	Factory pattern
Author:			Roger
Date:			2020/02/08
*/

#ifndef _GAME_FRAME_H
#define _GAME_FRAME_H

#include <iostream>
#include <cstdio>
#include <cstring>

//Interface
class gameFrame
{
public:
	virtual void playGame(std::string param) = 0;
	virtual void gameType(std::string param) = 0;

protected:
	int _game_id;
	std::string _game_rule;
};

#endif

Football.h:

/*
Design Mode:	Factory pattern
Author:			Roger
Date:			2020/02/08
*/

#ifndef _FOOTBALL_H
#define _FOOTBALL_H

#include <iostream>
#include <cstdio>
#include <cstring>
#include "GameFrame.h"

//继承游戏框架父类
class Football : public gameFrame	
{
public:
	Football()
	{
		_game_id = 1;
		_game_rule = "play ball by foots";
	}
	~Football()
	{

	}
	void playGame(std::string param);
	void gameType(std::string param);

private:
	void getGameRule();
	void screwShot();
	void marseilleTurn();
	
};

#endif

Basketball.h:

/*
Design Mode:	Factory pattern
Author:			Roger
Date:			2020/02/08
*/

#ifndef _BASKETBALL_H
#define _BASKETBALL_H

#include <iostream>
#include <cstdio>
#include <cstring>
#include "GameFrame.h"

//继承游戏框架父类
class Basketball : public gameFrame	
{
public:
	Basketball()
	{
		_game_id = 2;
		_game_rule = "play ball by hands";
	}
	~Basketball()
	{

	}
	void playGame(std::string param);
	void gameType(std::string param);

private:
	void getGameRule();
	void crossOver();
	void threePointShot();
	
};

#endif

main.cpp:

#include <iostream>
#include <cstdio>
#include <memory>
#include "Singleton.h"
#include "Basketball.h"
#include "Football.h"
#include "GameFrame.h"


/*
Design Mode:	Factory pattern
Author:			Roger
Date:			2020/02/08
*/
class  Factory
{
public:
	 Factory()
	 {

	 }
	~ Factory()
	{

	}
	std::shared_ptr<gameFrame> createGame(int type)
	{
		switch(type)
		{
			case 1: 
				gameptr = new Football();
				break;
			case 2:
				gameptr = new Basketball();
				break;
			default:
				break;
		}
		std::shared_ptr<gameFrame> game(gameptr);
		return game;
	}

private:
	gameFrame* gameptr;
	
};

int main() {

	//使用工厂模式
	Factory* footballGame = new Factory();
	std::shared_ptr<gameFrame>football = footballGame->createGame(1);
	football->playGame("I am Roger");

}

实现工厂类和工厂模式的主要目的是延迟对象实例化,增加使用的灵活性。

 

3、策略模式

包括文件:GameFrame.h, Football.h, Football.cpp, Basketball.h, Basketball.cpp, main.cpp

对工厂模式稍作改动,如:

#include <iostream>
#include <cstdio>
#include <memory>
#include "Singleton.h"
#include "Basketball.h"
#include "Football.h"
#include "GameFrame.h"

/*
Design Mode:	Strategy Pattern
Author:			Roger
Date:			2020/02/08
*/

class  sportsGame : public gameFrame
{
public:
	 sportsGame(int type)
	 {
	 	switch(type)
	 	{
	 		case 1: 
				gameptr = new Football();
				break;
			case 2:
				gameptr = new Basketball();
				break;
			default:
				break;
	 	}
	 	std::shared_ptr<gameFrame> gameSelect(gameptr);
	 	game = gameSelect;
	 }

    //在这里,因为使用了智能指针,所以不需要再手动释放对象
	~ sportsGame()
	{
		
	}

	void playGame(std::string param)
	{
		game->playGame(param);
	}

	void gameType(std::string param)
	{
		game->gameType(param);
	}

private:
	gameFrame* gameptr;
	std::shared_ptr<gameFrame> game;	
};

int main() {

	//策略模式
	std::shared_ptr<sportsGame>football(new sportsGame(1));
	football->playGame("I am Mess");

	std::shared_ptr<sportsGame>basketball(new sportsGame(2));
	basketball->playGame("I am Kobe");

}

策略模式变化在于,策略模式不需要将对象实例成不同的实例,而是通过同一个类,去使用不同的方法。所以这是一个行为模式。

今天先总结到这里,如上面游理解偏差的,请大佬们支持,谢谢。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值