重学设计模式——中介者模式

来源:互联网 发布:网络套路是什么意思 编辑:程序博客网 时间:2024/06/04 08:18

前些天原来的房子马上到期,又打算和女友住一起,所以换个住处成了一个迫在眉睫的问题(手动圈重点:我有女友)

但是问题就接踵而来,通过在微博,豆瓣等平台观望许久之后发现,很难找到合适的房源,并且作为一个程序员,没有太多精力投放在这种一对多的查询上面(圈重点:一对多)

于是乎,我找到了中介。

果然是术业有专攻,半天的功夫,他们就给我找到合适的房源,并且现在顺利入住了。

所以,中介,充当了很大的一个作用,这不仅仅在生活中,在开发中,中介者模式也是一个很常用的设计模式。

中介者模式定义:

官方定义:中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。中介者模式属于行为型模式。

根据迪米特原则,一个对象对其他对象的了解越少越好,就像很多人的死就是因为知道太多了。


中介者模式结构:

图中,Mediator 为一个抽象中介类

Colleague为抽象同事,即通过中介进行管理的成员类

中介者模式的运用

就拿我找房子的例子来说,为了避免我重复地寻找房子,我通过一个中介者来对我的需求进行筛选。

在这过程中参与的角色有Renter(租客,即我本人)

Lianjia(链家,即中介)

Landlord(房东)


首先,我们先写一个中介的抽象类AbstractMediator

public abstract class AbstractMediator {public abstract void notice(AbstractCustomer iCustomer,int money);}
这里定义了一个抽象方法notice,用于通知通过中介托管的用户(用户可以是租客,也可是房东)


接着,我们写一个客户的抽象类AbstractCustomer

public abstract class AbstractCustomer {protected int hope;protected AbstractMediator iMediator;public AbstractCustomer(AbstractMediator iMediator,int hope){this.iMediator = iMediator;this.hope = hope;}}
在客户类中,hope表示希望的租金,同时类持有对一个抽象中介类的引用


现在,我们定义一个租客RenterA

public class RenterA extends AbstractCustomer {public RenterA(AbstractMediator iMediator,int hope) {super(iMediator,hope);}public void sendInfo(){iMediator.notice(RenterA.this,this.hope);}public void getInfo(String landlordName){System.out.println("To RenterA:"+landlordName+"的房子符合要求");}}

sendInfo方法实现中介类中通知的功能,getInfo()方法为接收中介类的通知信息

接下来,我们定义三个房东类,LandlordA,LandlordB,LandlordC

public class LandlordA extends AbstractCustomer{public LandlordA(AbstractMediator iMediator,int hope) {super(iMediator,hope);}public void getInfo(String renterName){System.out.println("To LandlordA:"+renterName+"租金符合要求");}}public class LandlordB extends AbstractCustomer{public LandlordB(AbstractMediator iMediator,int hope) {super(iMediator,hope);}public void getInfo(String renterName){System.out.println("To Landlordb:"+renterName+"租金符合要求");}}public class LandlordC extends AbstractCustomer{public LandlordC(AbstractMediator iMediator,int hope) {super(iMediator,hope);}public void getInfo(String renterName){System.out.println("To LandlordC:"+renterName+"租金符合要求");}}
实现原理和RenterA相似,聪明的你一定看得懂,我就不赘述了


然后,我们实现一个中介,Lianjia(链家看到后记得给我打广告费)

public class Lianjia extends AbstractMediator {private RenterA renterA;private LandlordA landlordA;private LandlordB landlordB;private LandlordC landlordC;public void setRenterA(RenterA renterA){this.renterA = renterA;}public void setLandlordA(LandlordA landlordA){this.landlordA = landlordA;}public void setLandlordB(LandlordB landlordB){this.landlordB = landlordB;}public void setLandlordC(LandlordC landlordC){this.landlordC = landlordC;}@Overridepublic void notice(AbstractCustomer iCustomer, int money) {// TODO Auto-generated method stubif(landlordA.hope<=iCustomer.hope){renterA.getInfo("landlordA");landlordA.getInfo("renterA");}if(landlordB.hope<=iCustomer.hope){renterA.getInfo("landlordB");landlordA.getInfo("renterA");}if(landlordC.hope<=iCustomer.hope){renterA.getInfo("landlordC");landlordA.getInfo("renterA");}else {System.out.println("老子找不到合适的房源");}}}
在这段代码中,我们分别实例化了RenterA,和三个房东Landlord类,并将其注入到类中。同时,在notice()方法中写了找房子的逻辑,在我看来,房东的租金低于我的希望租金即为可以接受,当然作为中介,也要考虑房东的要价,我这里就先不考虑了。


最后,我们写一个用于测试的客户端Client

public class Client {public static void main(String[] args) {Lianjia lianjia = new Lianjia();RenterA renterA = new RenterA(lianjia,3000);LandlordA landlordA = new LandlordA(lianjia,2600);LandlordB landlordB = new LandlordB(lianjia,5000);LandlordC landlordC = new LandlordC(lianjia,2900);lianjia.setLandlordA(landlordA);lianjia.setLandlordB(landlordB);lianjia.setLandlordC(landlordC);lianjia.setRenterA(renterA);renterA.sendInfo();}}
打印结果为

To RenterA:landlordA的房子符合要求To LandlordA:renterA租金符合要求To RenterA:landlordC的房子符合要求To LandlordA:renterA租金符合要求

当然,如果我稍稍做下改变,把RenterA的价格参数改为1000

打印结果为:

老子找不到合适的房源

所以说,贪便宜,就算是中介也没办法解决相对应的需求,


总结

通过中介者模式,减少了类之间的相互调用,大部分的逻辑由中介者来进行承担,更大程度地做到了解耦。但是同时也造成了一定的损耗,就好像我们请中介也是要花钱的一样。