cpp: Command Pattern

 

/*****************************************************************//**
 * \file   Gold.h
 * \brief   Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command C++ 14
 * 塗聚文 Geovin Du Visual Studio 2022 edit. 
 * \author geovindu
 * \date   27 May 2023
 *********************************************************************/

#pragma once
#ifndef GOLD_H 
#define GOLD_H 



#include <iostream>
#include <sstream>
#include <vector>
using namespace std;



/**
* @brief 類庫空間名
* geovindu edit
*
*/
namespace DuCommonPattern
{


	/// <summary>
	/// 類
	/// </summary>
	class Gold
	{

	public:
		/// <summary>
		/// 製作金項鍊
		/// </summary>
		void GoldNecklace()
		{
			cout << "製作一條金項鍊" << endl;
		}

		/// <summary>
		/// 製作黃金耳環
		/// </summary>
		void GoldEarring()
		{
			cout << "製作一對黃金耳環" << endl;
		}
		//做其他各種黃金鉓品......略

	};
}
#endif


/*****************************************************************//**
 * \file   Command.h 
 * \brief  Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command C++ 14
 *  塗聚文 Geovin Du Visual Studio 2022 edit. 
 * \author geovindu
 * \date   27 May 2023
 *********************************************************************/
#pragma once
#ifndef COMMAND_H 
#define COMMAND_H 



#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "Gold.h"


using namespace std;



/**
* @brief 類庫空間名
* geovindu edit
*
*/
namespace DuCommonPattern
{

	/// <summary>
	/// 製作各種黃金飾品對應的抽象類
	/// </summary>
	class Command
	{
	public:
		/// <summary>
		/// 構造函數
		/// </summary>
		/// <param name="pgold"></param>
		Command(Gold* pgold)
		{
			mPGold = pgold;
		}
		/// <summary>
		/// 做父類時析構函數應該爲虛函數
		/// </summary>
		virtual ~Command()
		{
			if (mPGold != nullptr)
			{
				//delete mPGold;
				mPGold = nullptr;
			}
		}
		/// <summary>
		/// 執行
		/// </summary>
		virtual void Execute() = 0;
	protected:
		/// <summary>
		/// 子類需要訪問
		/// </summary>
		Gold* mPGold; //


	};

}
#endif


/*****************************************************************//**
 * \file   CommandNecklace.h 
 * \brief   Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command C++ 14
 * 塗聚文 Geovin Du Visual Studio 2022 edit. 
 * \author geovindu
 * \date   27 May 2023
 *********************************************************************/
#pragma once
#ifndef COMMANDNECKLACE_H 
#define COMMANDNECKLACE_H 



#include <iostream>
#include <sstream>
#include <vector>
#include "Gold.h"
#include "Command.h"

using namespace std;



/**
* @brief 類庫空間名
* geovindu edit
*
*/
namespace DuCommonPattern
{

	/// <summary>
	/// 製作黃金項鍊命令
	/// </summary>
	class CommandNecklace:public Command
	{
	public:
		/// <summary>
		/// 構造函數
		/// </summary>
		/// <param name="pgold"></param>
		CommandNecklace(Gold* pgold) :Command(pgold) {}
		/// <summary>
		/// 執行
		/// </summary>
		virtual void Execute()
		{
			mPGold->GoldNecklace();
		}

	};
}

#endif

/*****************************************************************//**
 * \file   CommandEarring.h
 * \brief  Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command C++ 14
 * 塗聚文 Geovin Du Visual Studio 2022 edit. 
 * \author geovindu
 * \date   27 May 2023
 *********************************************************************/
#pragma once
#ifndef COMMANDEARRING_H 
#define COMMANDEARRING_H 



#include <iostream>
#include <sstream>
#include <vector>
#include "Gold.h"
#include "Command.h"

using namespace std;



/**
* @brief 類庫空間名
* geovindu edit
*
*/
namespace DuCommonPattern
{

	/// <summary>
	/// 製作黃金耳環命令
	/// </summary>
	class CommandEarring:public Command
	{

	public:
		/// <summary>
		/// 構造函數
		/// </summary>
		/// <param name="pgold"></param>
		CommandEarring(Gold* pgold) :Command(pgold) {}
		/// <summary>
		/// 執行
		/// </summary>
		virtual void Execute()
		{
			mPGold->GoldEarring();
		}


	};

}
#endif

/*****************************************************************//**
 * \file   Craftsman.h
 * \brief  Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command C++ 14
 * 塗聚文 Geovin Du Visual Studio 2022 edit. 
 * 
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef CRAFTSMAN_H 
#define CRAFTSMAN_H 



#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "Gold.h"
#include "Command.h"

using namespace std;



/**
* @brief 類庫空間名
* geovindu edit
*
*/
namespace DuCommonPattern
{

	/// <summary>
	/// 工匠
	/// </summary>
	class Craftsman
	{
	public:
		
		
		/// <summary>
		/// 添加
		/// </summary>
		/// <param name="pcommand"></param>
		void AddCommand(Command* pcommand)
		{
			mCommlist.push_back(pcommand);
		}
		/// <summary>
		/// 刪除
		/// </summary>
		/// <param name="pcommand"></param>
		void DelCommand(Command* pcommand) 
		{
			mCommlist.remove(pcommand);
		}

		/// <summary>
		/// 收到多個製作BOM單,按順序製作
		/// </summary>
		void Notify() 
		{
		
			for (auto iter = mCommlist.begin(); iter != mCommlist.end(); ++iter)
			{
				(*iter)->Execute();
			}
		}
	private:
		
		
		/// <summary>
		/// 多個製作BOM單列表
		/// </summary>
		std::list<Command*> mCommlist; 


	};

}

#endif

/*****************************************************************//**
 * \file   TraineeCraftsman.h
 * \brief  Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command C++ 14
 * 塗聚文 Geovin Du Visual Studio 2022 edit. 
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef CRAFTSMAN_H 
#define CRAFTSMAN_H 



#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "Gold.h"
#include "Command.h"

using namespace std;



/**
* @brief 類庫空間名
* geovindu edit
*
*/
namespace DuCommonPattern
{

	/// <summary>
	/// 實習工匠
	/// </summary>
	class TraineeCraftsman
	{
	public:

		/// <summary>
		/// 構造函數
		/// </summary>
		/// <param name="pCommand"></param>
		TraineeCraftsman(Command* pCommand) :mPCommand(pCommand) {}

		/// <summary>
		/// 實習工匠交給工匠開始製作首飾
		/// </summary>
		void Notify() 
		{
			mPCommand->Execute();
		}

		/// <summary>
		/// 析構函數
		/// </summary>
		~TraineeCraftsman() //
		{
			if (mPCommand != nullptr)
			{
				delete mPCommand;
				mPCommand = nullptr;
			}
		}
	private:

		/// <summary>
		/// 實習工匠,有BOM清單列表
		/// </summary>
		Command* mPCommand; 



	};
}
#endif

/*****************************************************************//**
 * \file   CraftsmanOld.h
 * \brief  Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command C++ 14
 * 塗聚文 Geovin Du Visual Studio 2022 edit. 
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef CRAFTSMANOLD_H 
#define CRAFTSMANOLD_H 



#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "Gold.h"
#include "Command.h"

using namespace std;



/**
* @brief 類庫空間名
* geovindu edit
*
*/
namespace DuCommonPattern
{

	/// <summary>
	/// 
	/// </summary>
	class CraftsmanOld
	{
	public:
		/// <summary>
		/// 
		/// </summary>
		/// <param name="pcommand"></param>
		void SetCommand(Command* pcommand) 
		{
			mPCommand = pcommand;
		}
		/// <summary>
		/// 
		/// </summary>
		void Notify() 
		{
			mPCommand->Execute();
		}
	private:
		/// <summary>
		/// 
		/// </summary>
		Command* mPCommand; 

	};
}
#endif


/*****************************************************************//**
 * \file   GeovinDu.h
 * \brief   Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command C++ 14
 * 塗聚文 Geovin Du Visual Studio 2022 edit. 
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#pragma once
#ifndef GEOVINDU_H 
#define GEOVINDU_H 



#include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include "Gold.h"
#include "Command.h"
#include "CommandEarring.h"
#include "CommandMacro.h"
#include "CommandNecklace.h"
#include "Craftsman.h"
#include "TraineeCraftsman.h"
#include "CraftsmanOld.h"


using namespace std;



/**
* @brief 類庫空間名
* geovindu edit
*
*/
namespace DuCommonPattern
{

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

	private:

	public:
		/// <summary>
		/// 顯示實例
		/// </summary>
		void displaySimple();
	



	};

}

#endif

/*****************************************************************//**
 * \file   GeovinDu.cpp
 * \brief   Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command C++ 14
 * 塗聚文 Geovin Du Visual Studio 2022 edit. 
 * \author geovindu
 * \date   May 2023
 *********************************************************************/
#include "GeovinDu.h"


using namespace std;



/**
* @brief 類庫空間名
* geovindu edit
*
*/
namespace DuCommonPattern
{

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

			DuCommonPattern::Gold* pgold = new DuCommonPattern::Gold();
			pgold->GoldNecklace();
			pgold->GoldEarring();

			cout << "****" << endl;
	
	
			DuCommonPattern::Gold gold1;
			DuCommonPattern::Command* pcmd1 = new DuCommonPattern::CommandNecklace(&gold1);
			pcmd1->Execute(); 

			DuCommonPattern::Command* pcmd2 = new DuCommonPattern::CommandEarring(&gold1);
			pcmd2->Execute(); 

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

			
			DuCommonPattern::Gold gold2;
			DuCommonPattern::CraftsmanOld* pcraftsman = new DuCommonPattern::CraftsmanOld();

			DuCommonPattern::Command* pcmd4 = new DuCommonPattern::CommandNecklace(&gold2);
			pcraftsman->SetCommand(pcmd4);
			pcraftsman->Notify();

			DuCommonPattern::Command* pcmd5 = new DuCommonPattern::CommandEarring(&gold2);
			pcraftsman->SetCommand(pcmd5);
			pcraftsman->Notify();


			cout << "****" << endl;
			
			DuCommonPattern::Gold gold3;
			
			DuCommonPattern::Command* pcmd3 = new DuCommonPattern::CommandNecklace(&gold3);		
			DuCommonPattern::Command* pcmd6 = new DuCommonPattern::CommandEarring(&gold3);

			DuCommonPattern::Craftsman* pcraftsman2 = new DuCommonPattern::Craftsman();
			
			pcraftsman2->AddCommand(pcmd3);
			pcraftsman2->AddCommand(pcmd6);

			
			pcraftsman2->Notify();





			//釋放資源
			delete pgold;
			//釋放資源
			delete pcmd1;
			delete pcmd2;

			//釋放資源
			delete pcmd4;
			delete pcmd5;
			delete pcraftsman;



			//釋放資源
			delete pcmd3;
			delete pcmd6;
			delete pcraftsman2;


	}

}

  

調用:

/*****************************************************************//**
 * \file   ConsoleDuCommandPattern.cpp
 * \brief  Command Pattern 命令模式 亦稱:動作、事務、Action、Transaction、Command  C++ 14
 * 塗聚文 Geovin Du Visual Studio 2022 edit. 
 * 
 * \author geovindu
 * \date   26 May 2023
 *********************************************************************/
// ConsoleDuCommandPattern.cpp : 此文件包含 "main" 函數。程序執行將在此處開始並結束。
//2023年5月26日 塗聚文 Geovin Du Visual Studio 2022 edit. 
#define _UNICODE
#include <iostream>
#include <list>
#include "GeovinDu.h"

#ifdef _DEBUG   //只在Debug(調試)模式下
#ifndef DEBUG_NEW
#define DEBUG_NEW new(_NORMAL_BLOCK,__FILE__,__LINE__) //重新定義new運算符
#define new DEBUG_NEW
#endif
#endif
using namespace std;
using namespace DuCommonPattern;

int main()
{

    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);//程序退出時檢測內存泄漏並顯示到“輸出”窗口

    std::cout << "Hello World!!塗聚文 Geovin Du\n";
    GeovinDu geovin;
    geovin.displaySimple();

    system("pause");
    return 0;
}

// 運行程序: Ctrl + F5 或調試 >“開始執行(不調試)”菜單
// 調試程序: F5 或調試 >“開始調試”菜單

// 入門使用技巧: 
//   1. 使用解決方案資源管理器窗口添加/管理文件
//   2. 使用團隊資源管理器窗口連接到源代碼管理
//   3. 使用輸出窗口查看生成輸出和其他消息
//   4. 使用錯誤列表窗口查看錯誤
//   5. 轉到“項目”>“添加新項”以創建新的代碼文件,或轉到“項目”>“添加現有項”以將現有代碼文件添加到項目
//   6. 將來,若要再次打開此項目,請轉到“文件”>“打開”>“項目”並選擇 .sln 文件
#define UNICODE

  

輸出:

Hello World!!塗聚文 Geovin Du\n製作一條金項鍊
製作一對黃金耳環
****
製作一條金項鍊
製作一對黃金耳環
****
製作一條金項鍊
製作一對黃金耳環
****
製作一條金項鍊
製作一對黃金耳環
請按任意鍵繼續. . .

  

 

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