diff --git a/README.md b/README.md index d125934..42f0984 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ 8.桥接模式- 已上传 -9.中介者模式 - 整理中 +9.中介者模式 - 已上传 10.责任链模式 - 整理中 diff --git a/src/main/com/mxy/design/mediator/AbstractMediator.java b/src/main/com/mxy/design/mediator/AbstractMediator.java new file mode 100644 index 0000000..746e0f3 --- /dev/null +++ b/src/main/com/mxy/design/mediator/AbstractMediator.java @@ -0,0 +1,6 @@ +package com.mxy.design.mediator; + +public abstract class AbstractMediator { + + public abstract void sendMessage(Class clazz); +} diff --git a/src/main/com/mxy/design/mediator/ITaobaosMarket.java b/src/main/com/mxy/design/mediator/ITaobaosMarket.java new file mode 100644 index 0000000..487c3e6 --- /dev/null +++ b/src/main/com/mxy/design/mediator/ITaobaosMarket.java @@ -0,0 +1,10 @@ +package com.mxy.design.mediator; + +/** + * 商城 + */ +public interface ITaobaosMarket { + + void contact(); + +} diff --git a/src/main/com/mxy/design/mediator/Mediator.java b/src/main/com/mxy/design/mediator/Mediator.java new file mode 100644 index 0000000..359a070 --- /dev/null +++ b/src/main/com/mxy/design/mediator/Mediator.java @@ -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(); + } + } +} diff --git a/src/main/com/mxy/design/mediator/TaobaoBuyer.java b/src/main/com/mxy/design/mediator/TaobaoBuyer.java new file mode 100644 index 0000000..f9bc6ad --- /dev/null +++ b/src/main/com/mxy/design/mediator/TaobaoBuyer.java @@ -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); + } +} diff --git a/src/main/com/mxy/design/mediator/TaobaoLogistics.java b/src/main/com/mxy/design/mediator/TaobaoLogistics.java new file mode 100644 index 0000000..3928db0 --- /dev/null +++ b/src/main/com/mxy/design/mediator/TaobaoLogistics.java @@ -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); + } +} diff --git a/src/main/com/mxy/design/mediator/TaobaosSller.java b/src/main/com/mxy/design/mediator/TaobaosSller.java new file mode 100644 index 0000000..4adab75 --- /dev/null +++ b/src/main/com/mxy/design/mediator/TaobaosSller.java @@ -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); + } +} diff --git a/src/test/com/mxy/design/mediator/MediatorTest.java b/src/test/com/mxy/design/mediator/MediatorTest.java new file mode 100644 index 0000000..1bd6906 --- /dev/null +++ b/src/test/com/mxy/design/mediator/MediatorTest.java @@ -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(); + + + + } + + +}