设计模式---中介者模式

 

代码展示

以前的 

package com.ibeifeng.ex1;

public class Man extends Person {

	public Man(String name, int condition) {
		super(name, condition);
	}

	public void getPartner(Person person) {
		if(person instanceof Man) {
			System.out.println("汗,我不是同性恋!");
		} else {
			if(this.getCondition() == person.getCondition()) {
				System.out.println(this.getName() + "和" + person.getName() + "绝配");
			} else {
				System.out.println(this.getName() + "和" + person.getName() + "不相配");
			}
		}
	}

}

 

package com.ibeifeng.ex1;

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);
	
}
package com.ibeifeng.ex1;

public class Woman extends Person {

	public Woman(String name, int condition) {
		super(name, condition);
	}

	public void getPartner(Person person) {
		if(person instanceof Woman) {
			System.out.println("汗,我不是同性恋!");
		} else {
			if(this.getCondition() == person.getCondition()) {
				System.out.println(this.getName() + "和" + person.getName() + "绝配");
			} else {
				System.out.println(this.getName() + "和" + person.getName() + "不相配");
			}
		}
	}
	
}

 

package com.ibeifeng.ex1;

public class MainClass {
	public static void main(String[] args) {
		Person zhangsan = new Man("张三",5);
		Person lisi = new Man("李四",6);
		
		Person xiaofang = new Woman("小芳", 6);
		
		zhangsan.getPartner(xiaofang);
		lisi.getPartner(xiaofang);
		zhangsan.getPartner(lisi);
		
	}
}	

现在的

package com.ibeifeng.ex2;

public abstract class Person {
	private String name;
	private int condition;
	private Mediator mediator;

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

	public void setMediator(Mediator mediator) {
		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 abstract void getPartner(Person person);
	
}
package com.ibeifeng.ex2;

public class Man extends Person {

	public Man(String name, int condition,Mediator mediator) {
		super(name, condition, mediator);
	}

	public void getPartner(Person person) {
		this.getMediator().setMan(this);
		this.getMediator().getPartner(person);
	}
}
package com.ibeifeng.ex2;

public class Woman extends Person {

	public Woman(String name, int condition,Mediator mediator) {
		super(name, condition, mediator);
	}

	public void getPartner(Person person) {
		this.getMediator().setWoman(this);
		this.getMediator().getPartner(person);
	}
	
}
package com.ibeifeng.ex2;

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) {
		//将搭档设置上
		if(person instanceof Man) {
			this.setMan((Man)person);
		} else {
			this.setWoman((Woman)person);
		}
		//判断条件
		if(man == null || woman == null) {
			System.out.println("汗,我不是同性恋!");
		} else {
			
			if(man.getCondition() == woman.getCondition()) {
				System.out.println(man.getName() + "和" + woman.getName() + "绝配");
			} else {
				System.out.println(man.getName() + "和" + woman.getName() + "不相配");
			}
		}
	}
	
}
package com.ibeifeng.ex2;

public class MainClass {
	public static void main(String[] args) {
		Mediator mediator = new Mediator();
		Person zhangsan = new Man("张三",7,mediator);
		Person lisi = new Man("李四",7,mediator);
		Person xiaofang = new Woman("小芳",7,mediator);
		
		zhangsan.getPartner(lisi);
		
		xiaofang.getPartner(lisi);
	}
}

 

 

外观模式、代理模式和中介者模式的区别

 

外观模式(Facade Pattern

定义一个外观类,外观类隐藏系统的复杂性,为客户端提供简化的方法和对现有系统类方法的委托调用。

例如:二手房交易的中介,属于外观模式。买房者通过中介可以简单地买到二手房,中介自己把联系房东看房砍价、过户、交税这些复杂的事情都搞定了。

 

代理模式(Proxy Pattern

用一个代理类代表另一个类的功能,但是不改变被代理类的功能。目的是控制对被代理类的访问。

 

中介者模式(Mediator Pattern

用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。将各对象之间的网状结构分离为星型结构。

例如:MVC 框架,其中C(控制器)就是 M(模型)和 V(视图)的中介者。微信群是各群员之间的中介者。