JAVA设计模式:中介者模式

中介者模式

Mediator模式是行为模式之一,在Mediator模式中,类之间的交互行为被统一放在Mediator的对象中,对象通过Mediator对象同其他对象交互,Mediator对象起着控制器的作用。

一、结构与角色

在这里插入图片描述

  • mediator:中介者类的抽象父类
  • concreteMediator:具体的中介者类,持有concreteColleague的引用
  • colleague:关联类的抽象父类,例如示例中的Person,持有mediator的引用
  • concreteColleague:具体的关联类,例如示例中的Man、Woman

二、传统例子解析

通过代码可以发现,Man与Woman在getPartner方法中存在交互行为,这样使得两个类的耦合度非常高,如果Woman里面有方法改变,Man里面也需要修改,客户端中也需要自己去寻找另一半,过程繁琐

2.1 Person
//相亲的人
public abstract class Person {
    private String name;
    private int condition;

    public Person(String name,int condition){
        this.name = name;
        this.condition = condition;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCondition() {
        return condition;
    }

    public void setCondition(int condition) {
        this.condition = condition;
    }

    //获得另外一半
    public abstract void getPartner(Person person);
}
2.2 Man
public class Man extends Person{
    public Man(String name, int condition) {
        super(name, condition);
    }

    @Override
    public void getPartner(Person person) {
        //存在交互行为
        if(person instanceof Man) {
            System.out.println("I need a girl");
        }
        else{
            if(this.getCondition() == person.getCondition())
                System.out.println(this.getName()+" match "+person.getName());
            else
                System.out.println(this.getName()+" doesn't match "+person.getName());
        }
    }
}
2.3 Woman
public class Woman extends Person {
    public Woman(String name, int condition) {
        super(name, condition);
    }

    @Override
    public void getPartner(Person person) {
        //存在交互行为
        if(person instanceof Woman) {
            System.out.println("I need a boy");
        }
        else{
            if(this.getCondition() == person.getCondition())
                System.out.println(this.getName()+" match "+person.getName());
            else
                System.out.println(this.getName()+" doesn't match "+person.getName());
        }
    }
}
2.4 MainClass
public class MainClass {
    public static void main(String[] args){
        Person Derrick = new Man("Derrick",5);
        Person Jessica = new Woman("Jessica",5);
        Person Alan = new Man("Alan",4);
        Derrick.getPartner(Jessica);
        Derrick.getPartner(Alan);
        Alan.getPartner(Jessica);
    }
}

三、中介者模式
3.1 Person
//相亲的人
public abstract class Person {
    private String name;
    private int condition;
    //持有中介者的引用
    private Mediator mediator;

    public Person(String name,int condition,Mediator mediator){
        this.name = name;
        this.condition = condition;
        this.mediator = mediator;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCondition() {
        return condition;
    }

    public void setCondition(int condition) {
        this.condition = condition;
    }

    public Mediator getMediator() {
        return mediator;
    }

    public void setMediator(Mediator mediator) {
        this.mediator = mediator;
    }

    //获得另外一半
    public abstract void getPartner(Person person);
}
3.2 Man
public class Man extends Person{
    public Man(String name, int condition,Mediator mediator) {
        super(name, condition,mediator);
    }

    @Override
    public void getPartner(Person person) {
        //在中介公司注册,即在中介者中注册
        this.getMediator().setMan(this);
        //持有中介者的引用,通过中介者寻找
        this.getMediator().getPartner(person);
    }
}
3.3 Woman
public class Woman extends Person {
    public Woman(String name, int condition,Mediator mediator) {
        super(name, condition,mediator);
    }

    @Override
    public void getPartner(Person person) {
        //在中介公司注册,即在中介者中注册
        this.getMediator().setWoman(this);
        //持有中介者的引用,通过中介者寻找
        this.getMediator().getPartner(person);
    }
}
3.4 Mediator
public class Mediator {
    private Man man;
    private Woman woman;

    public void setMan(Man man) {
        this.man = man;
    }

    public void setWoman(Woman woman) {
        this.woman = woman;
    }

    public void getPartner(Person person) {
        //匹配搭档,避免同性问题
        //如果匹配搭档是男生,则Person属于Man,并且女生匹配之前一定注册了
        if(person instanceof Man && this.woman != null) {
            this.setMan((Man)person);
        }
        //如果匹配搭档是女生,则Person属于Woman,并且男生匹配之前一定注册了
        if(person instanceof Woman && this.man != null){
            this.setWoman((Woman)person);
        }
        if(this.man==null || this.woman==null){
            System.out.println("should be a man and a woman");
        }else {
            //判断条件
            if (man.getCondition() == woman.getCondition())
                System.out.println(man.getName() + " match " + woman.getName());
            else
                System.out.println(man.getName() + " doesn't match " + woman.getName());
        }
        //清空中介中的男生跟女生,为下一次匹配
        this.man=null;
        this.woman=null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值