两种语言实现设计模式(C++和Java)(十八:中介者模式)
作者:互联网
中介者(Mediator)模式的定义:定义一个中介对象来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。中介者模式又叫调停模式,它是迪米特法则的典型应用。
中介者模式是一种对象行为型模式,其主要优点如下。
- 降低了对象之间的耦合性,使得对象易于独立地被复用。
- 将对象间的一对多关联转变为一对一的关联,提高系统的灵活性,使得系统易于维护和扩展。
其主要缺点是:当同事类太多时,中介者的职责将很大,它会变得复杂而庞大,以至于系统难以维护。
UML:
以中介者实现同事间相互传递消息为例。
C++实现
1 #include <string> 2 #include <iostream> 3 4 class Colleague; 5 6 class Mediator 7 { 8 public: 9 virtual void send(std::string msg, Colleague * p) = 0; 10 }; 11 12 class Colleague 13 { 14 protected: 15 Mediator * m_mediator; 16 public: 17 Colleague(Mediator * p) 18 { 19 m_mediator = p; 20 } 21 virtual void send(std::string msg) = 0; 22 virtual void notify(std::string msg) = 0; 23 }; 24 25 class ConcreteColleague_0 : public Colleague 26 { 27 public: 28 ConcreteColleague_0(Mediator * p) : Colleague(p) {} 29 void send(std::string msg) 30 { 31 m_mediator->send(msg, this); 32 } 33 void notify(std::string msg) 34 { 35 std::cout << "Colleague_0 收到了消息:" << msg << std::endl; 36 } 37 }; 38 39 class ConcreteColleague_1 : public Colleague 40 { 41 public: 42 ConcreteColleague_1(Mediator * p) : Colleague(p) {} 43 void send(std::string msg) 44 { 45 m_mediator->send(msg, this); 46 } 47 void notify(std::string msg) 48 { 49 std::cout << "Colleague_1 收到了消息:" << msg << std::endl; 50 } 51 }; 52 53 class ConcreteMediator : public Mediator 54 { 55 private: 56 // 这里也可以是一个列表 57 Colleague * m_p1; 58 Colleague * m_p2; 59 public: 60 void addColleague(Colleague * p1, Colleague * p2) 61 { 62 m_p1 = p1; 63 m_p2 = p2; 64 } 65 void send(std::string msg, Colleague * p) 66 { 67 // 这里接受来自一个同事发过来的消息,具体应该给谁要根据需求来 68 // 这里知识一个很简单的应用。比如该类总可以是有两个队列。 69 // 一个队列是客户,一个队列是客服 70 if (p == m_p1) 71 m_p2->notify(msg); 72 else 73 m_p1->notify(msg); 74 } 75 }; 76 77 int main() 78 { 79 using namespace std; 80 // 中介者模式 81 ConcreteMediator * p = new ConcreteMediator(); 82 Colleague * pCol1 = new ConcreteColleague_0(p); 83 Colleague * pCol2 = new ConcreteColleague_1(p); 84 p->addColleague(pCol1, pCol2); 85 pCol1->send("你下班了吗?"); 86 pCol2->send("没呢,你呢?"); 87 delete pCol1; 88 delete pCol2; 89 delete p; 90 return 0; 91 }
Java实现:
1 public interface Mediator { 2 void send(String message, Colleague colleague); 3 } 4 5 public abstract class Colleague { 6 7 protected Mediator mediator; 8 9 public Colleague(Mediator mediator){ 10 this.mediator = mediator; 11 } 12 13 public abstract void send(String message); 14 15 abstract public void notify(String message); 16 17 } 18 19 public class Colleague1 extends Colleague { 20 21 public Colleague1(Mediator mediator){ 22 super(mediator); 23 } 24 25 public void send(String message){ 26 mediator.send(message, this); 27 } 28 29 public void notify(String message){ 30 System.out.println("Colleague1 received " + message); 31 } 32 } 33 34 public class Colleague2 extends Colleague { 35 36 public Colleague2(Mediator mediator){ 37 super(mediator); 38 } 39 40 public void send(String message){ 41 mediator.send(message, this); 42 } 43 44 public void notify(String message){ 45 System.out.println("Colleague2 received " + message); 46 } 47 } 48 49 public class ConcreteMediator implements Mediator { 50 51 private Colleague colleague1; 52 53 private Colleague colleague2; 54 55 public void addColleague(Colleague colleague1, Colleague colleague2){ 56 this.colleague1 = colleague1; 57 this.colleague2 = colleague2; 58 } 59 60 @Override 61 public void send(String message, Colleague colleague) { 62 if (colleague == colleague1){ 63 colleague2.notify(message); 64 }else if(colleague == colleague2){ 65 colleague1.notify(message); 66 } 67 } 68 } 69 70 public class Main { 71 72 public static void main(String[] args) { 73 ConcreteMediator concreteMediator = new ConcreteMediator(); 74 Colleague1 colleague1 = new Colleague1(concreteMediator); 75 Colleague2 colleague2 = new Colleague2(concreteMediator); 76 concreteMediator.addColleague(colleague1, colleague2); 77 colleague1.send("下班了吗"); 78 colleague2.send("没呢"); 79 } 80 }
标签:Colleague,Java,mediator,void,C++,send,message,设计模式,public 来源: https://www.cnblogs.com/Asp1rant/p/11354957.html