声明:文章内容根据大牛博客的内容,自己理解后,给自己做的学习笔记,文末会附上大牛博客地址链接。有需要沟通交流的可加我QQ群:425120333
每个人活在这个世界上,就不可不免的要和其他人有接触,想要真正的一个人,是不现实的。就像代码里的类一样,纯粹的不和其他类有交互的类,
压根就没必要存在。随着类与类之间的交互,各个类之间的耦合必然越来越强,复杂度也就越来越高,类数量少时还好,数量一多,直接让你懵逼。
大家想必都知道的石头剪刀布的游戏,其中的规则也比较简单,先用原始的类代码来实现这个游戏的规则。
public class SimpleGame {
public static void main(String[] args) {
Stone stone = new Stone();
stone.win();
stone.lose();
stone.same();
System.out.println("-----------------------分隔符------------------------");
Scissor scissor = new Scissor();
scissor.win();
scissor.lose();
scissor.same();
System.out.println("-----------------------分隔符------------------------");
Cloth cloth = new Cloth();
cloth.win();
cloth.lose();
cloth.same();
}
}
/**
* @introduce:石头类
*
*/
class Stone {
private final String name = "石头";
public String getName() {
return name;
}
public void win() {
Scissor scissor = new Scissor();
System.out.println(this.getName() + " Vs " + scissor.getName() + " result : win");
}
public void lose() {
Cloth cloth = new Cloth();
System.out.println(this.getName() + " Vs " + cloth.getName() + " result : lose");
}
public void same() {
System.out.println(this.getName() + " Vs " + this.getName() + " result : same");
}
}
/**
* @introduce:剪刀类
*
*/
class Scissor {
private final String name = "剪刀";
public String getName() {
return name;
}
public void win() {
Cloth cloth = new Cloth();
System.out.println(this.getName() + " Vs " + cloth.getName() + " result : win");
}
public void lose() {
Stone stone = new Stone();
System.out.println(this.getName() + " Vs " + stone.getName() + " result : lose");
}
public void same() {
System.out.println(this.getName() + " Vs " + this.getName() + " result : same");
}
}
/**
* @introduce:布类
*
*/
class Cloth {
private final String name = "布";
public String getName() {
return name;
}
public void win() {
Stone stone = new Stone();
System.out.println(this.getName() + " Vs " + stone.getName() + " result : win");
}
public void lose() {
Scissor scissor = new Scissor();
System.out.println(this.getName() + " Vs " + scissor.getName() + " result : lose");
}
public void same() {
System.out.println(this.getName() + " Vs " + this.getName() + " result : same");
}
}
控制台输出:
石头 Vs 剪刀 result : win
石头 Vs 布 result : lose
石头 Vs 石头 result : same
———————–分隔符————————
剪刀 Vs 布 result : win
剪刀 Vs 石头 result : lose
剪刀 Vs 剪刀 result : same
———————–分隔符————————
布 Vs 石头 result : win
布 Vs 剪刀 result : lose
布 Vs 布 result : same
观察控制台可以发现已经达到了实现简单游戏的规则,不过这里的耦合太强了,这是三个简单类,但是每个类都和剩下的类耦合,
如果类数据上升,这个耦合度会更强,关系更复杂。这时就可以引进一个中介者,通过中介者把这个关联关系拆开,类之间的相互依赖都改为依赖中介者,
从而降低耦合度。
public class SimpleGame {
public static void main(String[] args) {
Mediator mediator = new Mediator();
Stone stone = new Stone(mediator);
stone.win();
stone.lose();
stone.same();
System.out.println("-----------------------分隔符------------------------");
Scissor scissor = new Scissor(mediator);
scissor.win();
scissor.lose();
scissor.same();
System.out.println("-----------------------分隔符------------------------");
Cloth cloth = new Cloth(mediator);
cloth.win();
cloth.lose();
cloth.same();
}
}
/**
* @introduce:中介者类
*
*/
class Mediator {
private Stone stone = new Stone(this);
private Scissor scissor = new Scissor(this);
private Cloth cloth = new Cloth(this);
public Stone getStone() {
return stone;
}
public Scissor getScissor() {
return scissor;
}
public Cloth getCloth() {
return cloth;
}
}
/**
* @introduce:石头类
*
*/
class Stone {
private final String name = "石头";
private Mediator mediator;
public String getName() {
return name;
}
public Stone(Mediator mediator) {
this.mediator = mediator;
}
public void win() {
System.out.println(this.getName() + " Vs " + mediator.getScissor().getName() + " result : win");
}
public void lose() {
System.out.println(this.getName() + " Vs " + mediator.getCloth().getName() + " result : lose");
}
public void same() {
System.out.println(this.getName() + " Vs " + this.getName() + " result : same");
}
}
/**
* @introduce:剪刀类
*
*/
class Scissor {
private final String name = "剪刀";
private Mediator mediator;
public Scissor(Mediator mediator) {
this.mediator = mediator;
}
public String getName() {
return name;
}
public void win() {
System.out.println(this.getName() + " Vs " + mediator.getCloth().getName() + " result : win");
}
public void lose() {
System.out.println(this.getName() + " Vs " + mediator.getStone().getName() + " result : lose");
}
public void same() {
System.out.println(this.getName() + " Vs " + this.getName() + " result : same");
}
}
/**
* @introduce:布类
*
*/
class Cloth {
private final String name = "布";
private Mediator mediator;
public Cloth(Mediator mediator) {
this.mediator = mediator;
}
public String getName() {
return name;
}
public void win() {
System.out.println(this.getName() + " Vs " + mediator.getStone().getName() + " result : win");
}
public void lose() {
System.out.println(this.getName() + " Vs " + mediator.getScissor().getName() + " result : lose");
}
public void same() {
System.out.println(this.getName() + " Vs " + this.getName() + " result : same");
}
}
控制台输出时一样的,这里就不展示了,通过添加了一个中介者类使得这三个类(剪刀、石头、布)都不在与其他两个类直接交互,都是通过中介者来完成,
即使也会添加了更多的类,这都是通过中介者来交互。类之间的耦合不会变高,不过相应的中介者类会变得越来越复杂。如果只是简单的几个类
其实没必要用中介者模式,中介者模式是在各个类之间出现了网状的关联关系时才去使用的。平时几个简单类之间没必要用到(上面只是举个例子)。
参考大牛博客
http://www.cnblogs.com/zuoxiaolong/p/pattern14.html