存档在 2013年3月24日

C++实现中介者设计模式在项目中应用

2013年3月24日

项目中存在机器人类,路径规划类,串口类,分别为:Robot ,RobotPathPlan,RobotSerial
robot.h

#ifndef ROBOT_H_
#define ROBOT_H_
class Mediator;
class Robot
{
public:
	Robot(Mediator *);
	void sendMsg(int n);
	void setPoint(int p);
	int getSonarByIndex(int i);
	void processData();
private:
	Mediator *mediator;
};
#endif

robot.cpp

#include "robot.h"
#include "mediator.h"
#include <iostream>
using namespace std;

Robot::Robot(Mediator* media) : mediator(media){}
void Robot::sendMsg(int n)
{
	mediator->sendMsg(n);
}
void Robot::setPoint(int p)
{
	mediator->setPoint(p);
}
int Robot::getSonarByIndex(int i)
{
	cout<<"获取第"<<i<<"个超声波"<<endl;
	return 1;
}
void Robot::processData()
{
	cout<<"机器人类开始处理数据"<<endl;
}

pathplan.h

#ifndef PATHPLAN_H_
#define PATHPLAN_H_
class Mediator;
class PathPlan
{
public:
	PathPlan(Mediator* media);
	int getSonarByIndex(int i);
	void setPoint(int p);
private:
	Mediator *mediator;
};

#endif

pathplan.cpp

#include "pathplan.h"
#include "mediator.h"
#include <iostream>
using namespace std;

PathPlan::PathPlan(Mediator* media) : mediator(media){}
int PathPlan::getSonarByIndex(int i) 
{
	return mediator->getSonarByIndex(i);
}

void PathPlan::setPoint(int p)
{
	cout<<"设置机器人目标点为"<<p<<endl;
}

robotserial.h

#ifndef ROBOTSERIAL_H_
#define ROBOTSERIAL_H_
class Mediator;

class RobotSerial
{
public:
	RobotSerial(Mediator* media);
	void sendMsg(int n);
	void processData();

private:
	Mediator *mediator;
};
#endif 

robotserial.cpp

#include "robotserial.h"
#include "mediator.h"
#include <iostream>
using namespace std;

RobotSerial::RobotSerial(Mediator* media) : mediator(media){}

void RobotSerial::sendMsg(int n)
{
	cout<<"发送命令"<<n<<endl;
}
void RobotSerial::processData()
{
	mediator->processData();
}

mediator.h

#ifndef MEDIATOR_H_
#define MEDIATOR_H_
class Robot;
class RobotSerial;
class PathPlan;

class Mediator
{
public:
	void registRobot(Robot *tRobot);
	void registRobotSerial(RobotSerial* tRobotSerial);
	void registPathPlan(PathPlan *tPathplan);

	void setPoint(int p);
	int getSonarByIndex(int i);
	void sendMsg(int n);
	void processData();
private:
	Robot *robot;
	RobotSerial *robotSerial;
	PathPlan *pathPlan;
};
#endif 

mediator.cpp

#include "mediator.h"
#include "robot.h"
#include "robotserial.h"
#include "pathplan.h"

void Mediator::registRobot(Robot *tRobot){robot = tRobot;}
void Mediator::registRobotSerial(RobotSerial* tRobotSerial){robotSerial = tRobotSerial;}
void Mediator::registPathPlan(PathPlan *tPathPlan) { pathPlan = tPathPlan;}

void Mediator::setPoint(int p)
{
	pathPlan->setPoint(p);
}
int Mediator::getSonarByIndex(int i)
{
	return robot->getSonarByIndex(i);
}
void Mediator::sendMsg(int n)
{
	robotSerial->sendMsg(n);
}
void Mediator::processData()
{
	robot->processData();
}

main.cpp

#include "robot.h"
#include "robotserial.h"
#include "pathplan.h"
#include "mediator.h"

int main()
{
	Mediator media;
	Robot robot(&media);
	RobotSerial robotSerial(&media);
	PathPlan pathPlan(&media);
	media.registRobot(&robot);
	media.registRobotSerial(&robotSerial);
	media.registPathPlan(&pathPlan);
	pathPlan.getSonarByIndex(1);
	robot.setPoint(1);

	robot.sendMsg(2);
	robotSerial.processData();
}