//main.cpp
#include "invoker.h"
#include "command.h"
#include "receiver.h"
#include "command.h"
#include "receiver.h"
void main()
{
Receiver *rev = new Receiver();
Command *cmd = new ConcreteCommand(rev);
Invoker *inv = new Invoker(cmd);
inv->Invoke();
}
{
Receiver *rev = new Receiver();
Command *cmd = new ConcreteCommand(rev);
Invoker *inv = new Invoker(cmd);
inv->Invoke();
}
//receiver.h
#ifndef _RECEIVER_H_
#define _RECEIVER_H_
#define _RECEIVER_H_
class Receiver
{
public:
Receiver()
{
cout<<"create Receiver"<
}
~Receiver() {}
virtual void Action()
{
cout<<"action"<
}
};
{
public:
Receiver()
{
cout<<"create Receiver"<
}
~Receiver() {}
virtual void Action()
{
cout<<"action"<
}
};
#endif
//component.h
#ifndef _COMPONENT_H_
#define _COMPONENT_H_
#define _COMPONENT_H_
#include
using namespace std;
using namespace std;
class Component
{
public:
Component()
{
cout<<"create Component"<
}
{
public:
Component()
{
cout<<"create Component"<
}
virtual void Execute()
{
cout<<"component execute"<
}
virtual ~Component(){}
};
{
cout<<"component execute"<
}
virtual ~Component(){}
};
#endif
//composite.h
#ifndef _COMPOSITE_H_
#define _COMPOSITE_H_
#define _COMPOSITE_H_
#include
#include
#include "component.h"
#include
#include "component.h"
using namespace std;
class Composite
{
public:
void Operation();
void Add(Component* com);
void Remove();
void GetChild();
virtual ~Composite();
protected:
Composite()
{
public:
void Operation();
void Add(Component* com);
void Remove();
void GetChild();
virtual ~Composite();
protected:
Composite()
{
cout<<"create composite"<< DIV>
}
private:
list* _com;
};
private:
list* _com;
};
#endif
//composite.cpp
#include "composite.h"
Composite::~Composite()
{
{
}
void Composite::Operation()
{
list::iterator it = _com->begin();
{
list::iterator it = _com->begin();
for(; it != _com->end() ; it++)
(*it)->Execute();
}
(*it)->Execute();
}
void Composite::Add(Component *com)
{
_com->push_back(com);
}
{
_com->push_back(com);
}
void Composite::Remove()
{
_com->pop_back();
}
{
_com->pop_back();
}
void Composite::GetChild()
{
}
{
}
//command.h
#ifndef _COMMAND_H_
#define _COMMAND_H_
#define _COMMAND_H_
#include
using namespace std;
#include "component.h"
#include "receiver.h"
using namespace std;
#include "component.h"
#include "receiver.h"
/***************Command:声明执行操作的接口********************/
class Command : public Component
{
public:
virtual ~Command()
{}
virtual void Execute();
protected:
Command()
{ cout<<"create command"<
};
{
public:
virtual ~Command()
{}
virtual void Execute();
protected:
Command()
{ cout<<"create command"<
};
/***************ConcreteCommand********************/
/*将一个接收者对象绑定于一个动作 */
/*调用接收者相应的操作,以实现Execute */
/***************ConcreteCommand********************/
class ConcreteCommand : public Command
{
public:
ConcreteCommand(Receiver* rev);
~ConcreteCommand()
{}
protected:
private:
Receiver* _rev;
};
/*将一个接收者对象绑定于一个动作 */
/*调用接收者相应的操作,以实现Execute */
/***************ConcreteCommand********************/
class ConcreteCommand : public Command
{
public:
ConcreteCommand(Receiver* rev);
~ConcreteCommand()
{}
protected:
private:
Receiver* _rev;
};
#endif
//component.cpp
#include "Command.h"
void Command::Execute()
{
}
{
}
ConcreteCommand::ConcreteCommand(Receiver *rev)
{
this->_rev = rev;
}
{
this->_rev = rev;
}
//invoker.h
#ifndef _INVOKER_H_
#define _INVOKER_H_
#define _INVOKER_H_
#include "Command.h"
class Invoker
{
public:
Invoker(Command* cmd)
{
_cmd = cmd;
}
{
public:
Invoker(Command* cmd)
{
_cmd = cmd;
}
void Invoke()
{
cout<<"invoke"<
}
{
cout<<"invoke"<
}
~Invoker()
{}
private:
Command* _cmd;
};
{}
private:
Command* _cmd;
};
#endif
输出结果为
create Receiver
create Component
create command
invoke
Press any key to continue
create Component
create command
invoke
Press any key to continue