Mediator

// CombatUnit.h

#ifndef _COMBATUNIT_H

#define _COMBATUNIT_H

#include <string>

using namespace std;

class CombatUnit {

public:

CombatUnit(int unitID, string unitName, int posX, int posY);

private:

int UnitID;

string UnitType;

protected:

int PosX;

int PosY;

public:

virtual void fire() = 0;

virtual go(int x, int y) = 0;

inline const int get_PosX() const;

inline const int get_PosY() const;

};

inline const int CombatUnit::get_PosX() const {

return PosX;

}

inline const int CombatUnit::get_PosY() const {

return PosY;

}

#endif

說明

    • 設計協調中心類別,指揮其他類別行為。
    • MediatorCombatInformationCenter 協調 Tank 與 Platoon 類別群行為。

// MediatorCombatInformationCenter.h

#ifndef _MEDIATORCOMBATINFORMATIONCENTER_H

#define _MEDIATORCOMBATINFORMATIONCENTER_H

class CombatUnit;

class Platoon;

class MediatorCombatInformationCenter {

public:

void go(const CombatUnit * combatUnit, int x, int y);

void fire(const CombatUnit * combatUnit);

private:

Platoon * platoon;

};

#endif

// Client.cpp

#include "Client.h"

int Client::main(int argc, const char ** argv)

{

Tank tank1(10, 10, 10);

Tank tank2(20, 20, 20);

Platoon platoon1(15, 15, 15);

Platoon platoon2(25, 25, 25);

this->MediatorCIC.go(tank1, platoon1.getPosX(), platoon1.getPosY());

this->MediatorCIC.fire(tank1);

this->MediatorCIC.go(tank2, platoon2.getPosX(), platoon2.getPosY());

this->MediatorCIC.fire(tank2);

}