//方法
//Reciever.h
#pragma一度クラスレシーバ{ パブリック: レシーバ(); 〜レシーバ(); 空のアクション(); 保護: プライベート: };
//Reciever.cpp
#include " Reciever.h " の#include <iostreamの> レシーバ::レシーバ(){} レシーバ::〜レシーバ(){} のボイドレシーバ::アクション() { のstd :: coutの << " レシーバのアクション... " << はstd ::てendl; }
//Command.h
#pragma一度クラスレシーバ。クラスコマンド{ パブリック: 仮想〜コマンド(); 仮想無効実行()= 0 ; 保護された: コマンド(); プライベート: }; クラス ConcreateCommand:パブリックコマンド { パブリック: ConcreateCommand(レシーバ * REC); 〜ConcreateCommand(); 無効実行(); 保護: プライベート: レシーバ * _rec; }。
//Command.cpp
#include " Command.h " の#include " Reciever.h " の#include <iostreamの> コマンド::コマンド(){} コマンド::〜コマンド(){} 無効コマンド::(){}を実行 ConcreateCommandを:: ConcreateCommand(レシーバ * REC) { _rec = REC。 } ConcreateCommand ::〜ConcreateCommand() { 削除 これを - > _rec。 } ボイドConcreateCommand ::)(実行 { _rec - > アクション(); std :: coutの<< " ConcreateCommand ... " << はstd ::てendl; }
//Invoker.h
クラスコマンド; クラスの発動 { パブリック: 実行者(コマンド * CMD)。 〜の発動(); 空を呼び出し(); 保護: プライベート: コマンド *が_CMD。 }。
//Invoker.cpp
#include " Command.h " の#include " Invoker.h " の#include <入出力ストリーム> インボーカ::インボーカ(コマンド * CMD) { _CMD = CMD。 } インボーカ::〜インボーカ() { 削除_CMD。 } ボイドインボーカ::呼び出し() { _CMD - > 実行(); }
//main.cpp
#include " Command.h " の#include " Invoker.h " の#include " Reciever.h " の#include <iostreamの> する#include < ストリング > int型のmain(int型の引数、文字 * ARGV) { レシーバ * REC = 新しいレシーバ() ; コマンド * CMD = 新しいConcreateCommand(REC); 実行者 * INV = 新しい呼び出し元(CMD); INV - > 呼び出し(); getchar(); リターン0 ; }