cpp:Adapter Pattern

 

/*****************************************************************//**
 * \file   GoldLogToFile.h
 * \brief  适配器模式Adapter Pattern 亦称: 封装器模式、Wrapper、Adapter  C++ 14
 * 2023年6月2日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDLOGTOFILE_H 
#define GOLDLOGTOFILE_H 

#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>

using namespace std;




namespace DuJewelryAdapterPattern
{

	/// <summary>
	/// 
	/// </summary>
	class GoldLogToFile
	{

	public:

		/// <summary>
		/// 加载
		/// </summary>
		void initFile()
		{
			cout << "日志文件初始化工作中" << endl;
			//做日志文件初始化工作,比如打开文件等等
			//......
		}
		/// <summary>
		/// 写
		/// </summary>
		/// <param name="pcontent"></param>
		void writeToFile(const char* pcontent)
		{

			cout << "将日志内容写入文件" << endl;
			//将日志内容写入文件
			//......
		}
		/// <summary>
		/// 读
		/// </summary>
		void readFromFile()
		{

			cout << "日志中读取一些信息" << endl;
			//从日志中读取一些信息
			//......
		}

		/// <summary>
		/// 关闭
		/// </summary>
		void closeFile()
		{
			cout << "关闭日志文件" << endl;
			//关闭日志文件
			//......
		}
		//......可能还有很多其他成员函数,略

	};

}

#endif

/*****************************************************************//**
 * \file   GoldLogToDatabase.h
 * \brief  适配器模式Adapter Pattern 亦称: 封装器模式、Wrapper、Adapter  C++ 14
 * 2023年6月2日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDLOGTODATABASE_H 
#define GOLDLOGTODATABASE_H 

#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>

using namespace std;




namespace DuJewelryAdapterPattern
{

	/// <summary>
	/// 
	/// </summary>
	class GoldLogToDatabase
	{

	public:
		/// <summary>
		/// 加载
		/// </summary>
		virtual void initDb() = 0; //不一定非是纯虚函数
		/// <summary>
		/// 写
		/// </summary>
		/// <param name="pcontent"></param>
		virtual void writeToDb(const char* pcontent) = 0;
		/// <summary>
		/// 读
		/// </summary>
		virtual void readFromDb() = 0;
		/// <summary>
		/// 关闭
		/// </summary>
		virtual void closeDb() = 0;
		/// <summary>
		/// 做父类时析构函数应该为虚函数
		/// </summary>
		virtual ~GoldLogToDatabase() {} 


	};
}

#endif


/*****************************************************************//**
 * \file   GoldLogAdapter.h
 * \brief  适配器模式Adapter Pattern 亦称: 封装器模式、Wrapper、Adapter  C++ 14
 * 2023年6月2日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDLOGADAPTER_H 
#define GOLDLOGADAPTER_H 

#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>

#include "GoldLogToDatabase.h"
#include "GoldLogToFile.h"

using namespace std;




namespace DuJewelryAdapterPattern
{

	/// <summary>
	/// 类适配器
	/// </summary>
	class GoldLogAdapter:public GoldLogToDatabase, private GoldLogToFile
	{

	public:

		/// <summary>
		/// 加载
		/// </summary>
		virtual void initDb()
		{
			cout << "在LogAdapter::initDb()中适配LogToFile::initFile()" << endl;
			//这其中也可以加任何的其他代码......
			initFile();
		}
		/// <summary>
		/// 写
		/// </summary>
		/// <param name="pcontent"></param>
		virtual void writeToDb(const char* pcontent)
		{
			cout << "在LogAdapter::writeToDb()中适配LogToFile::writeToFile()" << endl;
			writeToFile(pcontent);
		}
		/// <summary>
		/// 读
		/// </summary>
		virtual void readFromDb()
		{
			cout << "在LogAdapter::readFromFile()中适配LogToFile::readFromDb()" << endl;
			readFromFile();
		}
		/// <summary>
		/// 关闭
		/// </summary>
		virtual void closeDb()
		{
			cout << "在LogAdapter::closeDb()中适配LogToFile::closeDb()" << endl;
			closeFile();
		}

	};
}

#endif


/*****************************************************************//**
 * \file   DuLogToDatabase.h
 * \brief  适配器模式Adapter Pattern 亦称: 封装器模式、Wrapper、Adapter  C++ 14
 * 2023年6月2日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef DULOGTODATABASE_H 
#define DULOGTODATABASE_H 

#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>

using namespace std;




namespace DuAdapterPattern
{

	/// <summary>
	/// 
	/// </summary>
	class DuLogToDatabase
	{
	public:

		/// <summary>
		/// 加载
		/// </summary>
		void initDb()
		{

			cout << "连接数据库" << endl;
			//连接数据库,做一些基本的数据库连接设置等
			//......
		}
		/// <summary>
		/// 写
		/// </summary>
		/// <param name="pcontent"></param>
		void writeToDb(const char* pcontent)
		{

			cout << "将日志内容写入数据库" << endl;
			//将日志内容写入数据库
			//......
		}
		/// <summary>
		/// 读
		/// </summary>
		void readFromDb()
		{

			cout << "从数据库中读取一些日志信息" << endl;
			//从数据库中读取一些日志信息
			//......
		}
		/// <summary>
		/// 关闭
		/// </summary>
		void closeDb()
		{
			cout << "关闭到数据库的连接" << endl;
			//关闭到数据库的连接
			//......
		}


	};
}

#endif

/*****************************************************************//**
 * \file   DuLogAdapter.h
 * \brief  适配器模式Adapter Pattern 亦称: 封装器模式、Wrapper、Adapter  C++ 14
 * 2023年6月2日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef DULOGADAPTER_H 
#define DULOGADAPTER_H 

#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>
#include <vector>

#include "DuLogToDatabase.h"
#include "GoldLogToFile.h"

using namespace std;
using namespace DuJewelryAdapterPattern;



namespace DuAdapterPattern
{

	/// <summary>
	/// 
	/// </summary>
	class DuLogAdapter:public DuLogToDatabase
	{

	public:
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="pfile"></param>
		DuLogAdapter(GoldLogToFile* pfile) //形参是老接口所属类的指针
		{
			mPFile = pfile;
		}
		/// <summary>
		/// 
		/// </summary>
		virtual void initDb()
		{
			cout << "在DuLogAdapter::initDb()中适配GoldLogToFile::initFile()" << endl;
			//这其中也可以加任何的其他代码......
			mPFile->initFile();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="pcontent"></param>
		virtual void writeToDb(const char* pcontent)
		{
			cout << "在DuLogAdapter::writeToDb()中适配GoldLogToFile::writeToFile()" << endl;
			mPFile->writeToFile(pcontent);
		}
		/// <summary>
		/// 
		/// </summary>
		virtual void readFromDb()
		{
			cout << "在DuLogAdapter::readFromDb()中适配GoldLogToFile::readFromDb()" << endl;
			mPFile->readFromFile();
		}
		/// <summary>
		/// 
		/// </summary>
		virtual void closeDb()
		{
			cout << "在DuLogAdapter::closeDb()中适配GoldLogToFile::closedb()" << endl;
			mPFile->closeFile();
		}
	private:
		GoldLogToFile* mPFile;

	};

}

#endif


/*****************************************************************//**
 * \file   GeovinDu.h
 * \brief  适配器模式Adapter Pattern 亦称: 封装器模式、Wrapper、Adapter  C++ 14
 * 2023年6月2日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GEOVINDU_H 
#define GEOVINDU_H 

#include<cstring>
#include<stdbool.h>
#include<stdlib.h>
#include<iostream>
#include<malloc.h>
#include<cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <array>
#include <functional>
#include <list>
#include <string>
#include <string.h>

#include "GoldLogToDatabase.h"
#include "GoldLogToFile.h"
#include "GoldLogAdapter.h"

#include "DuLogToDatabase.h"
#include "DuLogAdapter.h"

using namespace std;
using namespace DuAdapterPattern;



namespace DuJewelryAdapterPattern
{

	/// <summary>
	/// 
	/// </summary>
	class GeovinDu
	{

	private:

	public:

		/// <summary>
		/// 
		/// </summary>
		void displaySimple();
		/// <summary>
		/// 
		/// </summary>
		void displayDuSimple();

	};
}
#endif

/*****************************************************************//**
 * \file   GeovinDu.cpp
 * \brief  适配器模式Adapter Pattern 亦称: 封装器模式、Wrapper、Adapter  C++ 14
 * 2023年6月2日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#include "GeovinDu.h"
using namespace std;




namespace DuJewelryAdapterPattern
{
	/// <summary>
	/// 
	/// </summary>
	void GeovinDu::displaySimple()
	{

		DuJewelryAdapterPattern::GoldLogToFile* plog = new DuJewelryAdapterPattern::GoldLogToFile();
		plog->initFile();
		plog->writeToFile("向日志文件中写入一条日志"); //写一条日志到日志文件中
		plog->readFromFile();
		plog->closeFile();
		delete plog;


			


	}
	/// <summary>
	/// 
	/// </summary>
	void GeovinDu::displayDuSimple()
	{

		    DuAdapterPattern::DuLogToDatabase* plogdb = new DuAdapterPattern::DuLogToDatabase();
			plogdb->initDb();
			plogdb->writeToDb("向数据库中写入一条日志"); //写一条日志到数据库中
			plogdb->closeDb();
			plogdb->readFromDb();
			
			
			DuJewelryAdapterPattern::GoldLogToFile* plog2 = new DuJewelryAdapterPattern::GoldLogToFile();
			DuAdapterPattern::DuLogToDatabase* plogdb2 = new DuAdapterPattern::DuLogAdapter(plog2); //
			plogdb2->initDb();
			plogdb2->writeToDb("向数据库中写入一条日志,实际是向日志文件中写入一条日志");
			plogdb2->readFromDb();
			plogdb2->closeDb();

			delete plogdb;
			//delete plogdb2;
			delete plog2;



	}

}

  

调用:

/*****************************************************************//**
 * \file   ConsoleDuAdapterPattern.cpp
 * \brief  适配器模式Adapter Pattern 亦称: 封装器模式、Wrapper、Adapter  C++ 14
 * 2023年6月2日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
// ConsoleDuAdapterPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _UNICODE


#include <iostream>
#include "GeovinDu.h"


#ifdef _DEBUG   //只在Debug(调试)模式下
#ifndef DEBUG_NEW
#define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__) //重新定义new运算符
#define new DEBUG_NEW
#endif
#endif

//#include <boost/type_index.hpp>
using namespace std;
//#pragma warning(disable : 4996) 
using namespace DuJewelryAdapterPattern;



int main()
{
    std::cout << "Hello World!涂聚文 Geovin Du\n";

    GeovinDu geovin;
    geovin.displaySimple();
    cout << "*****" << endl;
    geovin.displayDuSimple();


    system("pause");

    return 0;



}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件


#define UNICODE

  

输出:

Hello World!涂聚文 Geovin Du
日志文件初始化工作中
将日志内容写入文件
日志中读取一些信息
关闭日志文件
*****
连接数据库
将日志内容写入数据库
关闭到数据库的连接
从数据库中读取一些日志信息
连接数据库
将日志内容写入数据库
从数据库中读取一些日志信息
关闭到数据库的连接
请按任意键继续. . .

  

 

posted @ 2023-06-02 22:29  ®Geovin Du Dream Park™  阅读(7)  评论(0编辑  收藏  举报