Skip to content

Commit

Permalink
feat:上传中介者模式例子
Browse files Browse the repository at this point in the history
  • Loading branch information
maxy26 committed Mar 9, 2019
1 parent 40c6d97 commit 3250c2a
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

8.桥接模式- 已上传

9.中介者模式 - 整理中
9.中介者模式 - 已上传

10.责任链模式 - 整理中

Expand Down
6 changes: 6 additions & 0 deletions src/main/com/mxy/design/mediator/AbstractMediator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mxy.design.mediator;

public abstract class AbstractMediator {

public abstract void sendMessage(Class clazz);
}
10 changes: 10 additions & 0 deletions src/main/com/mxy/design/mediator/ITaobaosMarket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mxy.design.mediator;

/**
* 商城
*/
public interface ITaobaosMarket {

void contact();

}
81 changes: 81 additions & 0 deletions src/main/com/mxy/design/mediator/Mediator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.mxy.design.mediator;

public class Mediator extends AbstractMediator {

private TaobaoBuyer buyer;
private TaobaoLogistics logistics;
private TaobaosSller sller;

public TaobaoBuyer getBuyer() {
return buyer;
}

public void setBuyer(TaobaoBuyer buyer) {
this.buyer = buyer;
}

public TaobaoLogistics getLogistics() {
return logistics;
}

public void setLogistics(TaobaoLogistics logistics) {
this.logistics = logistics;
}

public TaobaosSller getSller() {
return sller;
}

public void setSller(TaobaosSller sller) {
this.sller = sller;
}

private Double payMoney;
private Double receiveMoney;
private String goodsName;

public Double getPayMoney() {
return payMoney;
}

public void setPayMoney(Double payMoney) {
this.payMoney = payMoney;
}

public Double getReceiveMoney() {
return receiveMoney;
}

public void setReceiveMoney(Double receiveMoney) {
this.receiveMoney = receiveMoney;
}

public String getGoodsName() {
return goodsName;
}

public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
@Override
public void sendMessage(Class clazz) {
try {
Object currentObj = Class.forName(clazz.getName()).newInstance();
if (currentObj instanceof TaobaoLogistics) {
((TaobaoLogistics) currentObj).receiveGoodsAndMoney(goodsName, receiveMoney);
} else if (currentObj instanceof TaobaosSller) {
sller.receiveMoney(receiveMoney);
sller.sendGoodsToLogistics(goodsName);
sller.payMoneyToLogistics(payMoney);
} else if (currentObj instanceof TaobaoBuyer) {
((TaobaoBuyer) currentObj).payMoneyAndGoods(payMoney,goodsName);
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
25 changes: 25 additions & 0 deletions src/main/com/mxy/design/mediator/TaobaoBuyer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mxy.design.mediator;

public class TaobaoBuyer implements ITaobaosMarket {


public TaobaoBuyer() {
}

private Mediator mediator;

public TaobaoBuyer(Mediator mediator) {
this.mediator = mediator;
}

public void payMoneyAndGoods(Double number, String goodsName) {
System.out.println("买家支付"+number+"与商品"+goodsName+"给卖家");
}

@Override
public void contact() {
mediator.setPayMoney(100D);
mediator.setGoodsName("篮球");
mediator.sendMessage(TaobaoBuyer.class);
}
}
25 changes: 25 additions & 0 deletions src/main/com/mxy/design/mediator/TaobaoLogistics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mxy.design.mediator;

public class TaobaoLogistics implements ITaobaosMarket {


public TaobaoLogistics() {
}

private Mediator mediator;

public TaobaoLogistics(Mediator mediator) {
this.mediator = mediator;
}

public void receiveGoodsAndMoney(String goodsName, Double number) {
System.out.println("快递小哥收到"+number+"与商品"+goodsName+"开始送货");
}

@Override
public void contact() {
mediator.setReceiveMoney(10D);
mediator.setGoodsName("篮球");
mediator.sendMessage(TaobaoLogistics.class);
}
}
34 changes: 34 additions & 0 deletions src/main/com/mxy/design/mediator/TaobaosSller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mxy.design.mediator;

public class TaobaosSller implements ITaobaosMarket{


public TaobaosSller() {
}

private Mediator mediator;

public TaobaosSller(Mediator mediator) {
this.mediator = mediator;
}

public void receiveMoney(Double number) {
System.out.println("卖家收到买家支付的"+number+"元钱");
}

public void payMoneyToLogistics(Double number) {
System.out.println("卖家支付"+number+"给快递小哥");
}

public void sendGoodsToLogistics(String goodsName) {
System.out.println("卖家把商品"+goodsName+"给快递小哥");
}

@Override
public void contact() {
mediator.setReceiveMoney(100D);
mediator.setPayMoney(10D);
mediator.setGoodsName("篮球");
mediator.sendMessage(TaobaosSller.class);
}
}
49 changes: 49 additions & 0 deletions src/test/com/mxy/design/mediator/MediatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.mxy.design.mediator;

import org.junit.Test;

/**
* 中介者模式:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互
*
* 优点:
* 同事对象之间的交互都被封装到中介者对象里面集中管理,集中了控制交互。当交互发生改变时,修改中介者对象。扩展中介者对象时,其他对象不需要做修改。
* 缺点:
* 过度集中化,这是中介者模式潜在的缺点。如果对象多了,交互复杂。全部集中到中介者对象中,会导致中介者对象十分臃肿,难以管理和维护
*
* 角色说明:
* 抽象中介者角色(AbstractMediator):定义出同事对象到中介者对象的接口,其中主要方法是一个(或多个)事件方法。
*
* 具体中介者角色(ConcreteMediator):实现抽象中介者中所声明的事件方法。具体中介者直销所有的具体同事类,并负责具体的协调各个同事对象的交互关系。
*
* 抽象同事类角色(AbstractColleague):定义出红接着到同事对象的接口。同事对象只知道中介者,而不知道的同事对象。(核心)
*
* 具体同事类角色(ConcreteColleague):所有的具体同事类均从抽象同事类继承而来。实现自己的业务,在需要与其他同事通信的时候,就与持有的中介者通信,中介者会负责与其他的同时交互。
*/

public class MediatorTest {

@Test
public void logicTest() {
Mediator mediator = new Mediator();
TaobaosSller sller = new TaobaosSller(mediator);
TaobaoLogistics logistics = new TaobaoLogistics(mediator);
TaobaoBuyer buyer = new TaobaoBuyer(mediator);

mediator.setBuyer(buyer);
mediator.setLogistics(logistics);
mediator.setSller(sller);

//同事之间不知道对方,至于中介沟通
buyer.contact();
System.out.println();
sller.contact();
System.out.println();
logistics.contact();



}


}

0 comments on commit 3250c2a

Please sign in to comment.