cpp: Bridge Pattern

 

/*****************************************************************//**
 * \file   Gold.h
 * \brief  桥接模式  Bridge Pattern C++ 14
 * 2023年6月3日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLD_H 
#define GOLD_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 DuJewelryBridgePattern
{

	/// <summary>
	/// 黄金
	/// </summary>
	class Gold
	{

	public:
		/// <summary>
		/// 镶㠌
		/// </summary>
		/// <param name="pInlayName"></param>
		void metallurgy(const char* pInlayName)
		{
			int iLen = 100;
			//char du[100]=pInlayName;
			
			//char** ptr;
			//ptr = &pInlayName;

			char* pData = parseDesgin(pInlayName, iLen);
			if (iLen > 0)
			{
				//char* p;
				//*p = ' ';
				// *p= *pData;
				// << *p
				cout<<"["<< pInlayName << "]*" << "显示pData所指向的缓冲区中的黄金数据。" << endl;
				//......
				delete pData; //模拟代码中因为pData的内存是new出来的,这里需要释放该内存
			}
		}
		/// <summary>
		/// 
		/// </summary>
		virtual ~Gold() {} //做父类时析构函数应该为虚函数
	private:
		//根据文件名分析文件内容,每个子类因为黄金冶炼不同,会有不同的读取和处理代码 Inlay

		/// <summary>
		/// 设计
		/// </summary>
		/// <param name="pInlayName"></param>
		/// <param name="iLen"></param>
		/// <returns></returns>
		virtual char* parseDesgin(const char* pInlayName, int& iLen) = 0;

	};
}

#endif

/*****************************************************************//**
 * \file   GoldGrace.h
 * \brief  桥接模式  Bridge Pattern C++ 14
 * 2023年6月3日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDGRACE_H 
#define GOLDGRACE_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 "Gold.h"


using namespace std;




namespace DuJewelryBridgePattern
{

	/// <summary>
	/// 优雅
	/// </summary>
	class GoldGrace:public Gold
	{

	private:
		/// <summary>
		/// 设计
		/// </summary>
		/// <param name="pInlayName"></param>
		/// <param name="iLen"></param>
		/// <returns></returns>
		virtual char* parseDesgin(const char* pInlayName, int& iLen)
		{
			
			cout << "Grace 优雅:韧性和延展性高,不受传统黄金材质限制,可打造出优雅时尚的造型;";
			iLen = 100;
			char* presult = new char[iLen];
			return presult;
		}


	};

}


#endif


/*****************************************************************//**
 * \file   GoldGlorious.h
 * \brief  桥接模式  Bridge Pattern C++ 14
 * 2023年6月3日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDGLORIOUS_H
#define GOLDGLORIOUS_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 "Gold.h"

using namespace std;




namespace DuJewelryBridgePattern
{

	/// <summary>
	/// 璀璨
	/// </summary>
	class GoldGlorious :public Gold
	{

		virtual char* parseDesgin(const char* pInlayName, int& iLen)
		{

			cout << "Glorious 璀璨:纯金材质,拥有天然的24K夺目金色,不易褪色,璀璨夺目:";
			iLen = 100;
			char* presult = new char[iLen];
			return presult;
		}

	};
}

#endif


/*****************************************************************//**
 * \file   GoldGentle.h
 * \brief  桥接模式  Bridge Pattern C++ 14
 * 2023年6月3日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDGENGLE_H
#define GOLDGENGLE_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 "Gold.h"

using namespace std;




namespace DuJewelryBridgePattern
{

	/// <summary>
	/// 高尚
	/// </summary>
	class GoldGentle :public Gold
	{

		virtual char* parseDesgin(const char* pInlayName, int& iLen)
		{

			cout << "Gentle 高尚:达到足金以上的含金量,更具价值;";
			iLen = 100;
			char* presult = new char[iLen];
			return presult;
		}

	};
}
#endif


/*****************************************************************//**
 * \file   GoldGenius.h
 * \brief  桥接模式  Bridge Pattern C++ 14
 * 2023年6月3日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDGENIUS_H
#define GOLDGENIUS_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 "Gold.h"

using namespace std;




namespace DuJewelryBridgePattern
{
	/// <summary>
	/// 天赋
	/// </summary>
	class GoldGenius :public Gold
	{
		virtual char* parseDesgin(const char* pInlayName, int& iLen)
		{

			cout << "Genius 天赋:做工精细,技术前卫,艺术性强,具有先天工艺优势;";
			iLen = 100;
			char* presult = new char[iLen];
			return presult;
		}


	};

}

#endif


/*****************************************************************//**
 * \file   GoldGusty.h
 * \brief  桥接模式  Bridge Pattern C++ 14
 * 2023年6月3日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#pragma once
#ifndef GOLDGUSTY_H
#define GOLDGUSTY_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 "Gold.h"

using namespace std;




namespace DuJewelryBridgePattern
{

	/// <summary>
	/// 坚定
	/// </summary>
	class GoldGusty :public Gold
	{

		virtual char* parseDesgin(const char* pInlayName, int& iLen)
		{

			cout << "Gusty 坚定:硬度高,不易变形,且更加轻便;";
			iLen = 100;
			char* presult = new char[iLen];
			return presult;
		}

	};

}
#endif

/*****************************************************************//**
 * \file   GeovinDu.h
 * \brief  桥接模式  Bridge Pattern C++ 14
 * 2023年6月3日 涂聚文 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 "Gold.h"
#include "GoldGenius.h"
#include "GoldGentle.h"
#include "GoldGlorious.h"
#include "GoldGrace.h"
#include "GoldGusty.h"



using namespace std;




namespace DuJewelryBridgePattern
{

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

	private:

	public:

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


	};

}

#endif

/*****************************************************************//**
 * \file   GeovinDu.cpp
 * \brief  桥接模式  Bridge Pattern C++ 14
 * 2023年6月3日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
#include "GeovinDu.h"
using namespace std;




namespace DuJewelryBridgePattern
{


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

		DuJewelryBridgePattern::Gold* pJewelry= new DuJewelryBridgePattern::GoldGrace();
		pJewelry->metallurgy("镶嵌蓝宝石");

		DuJewelryBridgePattern::Gold* pJewelry2 = new DuJewelryBridgePattern::GoldGlorious();
		pJewelry2->metallurgy("镶嵌绿宝石");

		DuJewelryBridgePattern::Gold* pJewelry3 = new DuJewelryBridgePattern::GoldGentle();
		pJewelry3->metallurgy("镶嵌红宝石");

		DuJewelryBridgePattern::Gold* pJewelry4 = new DuJewelryBridgePattern::GoldGenius();
		pJewelry4->metallurgy("镶翡翠玉石");

		DuJewelryBridgePattern::Gold* pJewelry5 = new DuJewelryBridgePattern::GoldGusty();
		pJewelry5->metallurgy("镶嵌玛瑙");

		//释放资源
		delete pJewelry;
		delete pJewelry2;
		delete pJewelry3;
		delete pJewelry4;
		delete pJewelry5;
	






	}

}

  

调用:

/*****************************************************************//**
 * \file   ConsoleDuBridgePattern.cpp
 * \brief  桥接模式  Bridge Pattern C++ 14
 * 2023年6月3日 涂聚文 Geovin Du Visual Studio 2022 edit.
 * \author geovindu
 * \date   June 2023
 *********************************************************************/
// ConsoleDuBridgePattern.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;
using namespace DuJewelryBridgePattern;


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


    GeovinDu geovin;
    geovin.displaySimple();

    cout << "********" << endl;


    system("pause");

    return 0;

}

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

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

  

输出:

Hello World!涂聚文 Geovin Du
Grace 优雅:韧性和延展性高,不受传统黄金材质限制,可打造出优雅时尚的造型;[镶嵌蓝宝石]*显示pData所指向的缓冲区中的黄金数据。
Glorious 璀璨:纯金材质,拥有天然的24K夺目金色,不易褪色,璀璨夺目:[镶嵌绿宝石]*显示pData所指向的缓冲区中的黄金数据。
Gentle 高尚:达到足金以上的含金量,更具价值;[镶嵌红宝石]*显示pData所指向的缓冲区中的黄金数据。
Genius 天赋:做工精细,技术前卫,艺术性强,具有先天工艺优势;[镶翡翠玉石]*显示pData所指向的缓冲区中的黄金数据。
Gusty 坚定:硬度高,不易变形,且更加轻便;[镶嵌玛瑙]*显示pData所指向的缓冲区中的黄金数据。
********
请按任意键继续. . .

  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章