public class Friend { public void play(){ System.out.println("friends play"); } }
public class Stranger { public void play(){ System.out.println("stranger play"); } }
public class SomeOne { public void play(Friend friends){ System.out.println("someone play"); friends.play(); } public void play(Stranger stranger){//SomeOne直接和Stranger通信/交互 System.out.println("someone play"); stranger.play(); } }
public class MainClass { public static void main(String[] args) { SomeOne zhangsan = new SomeOne(); zhangsan.play(new Friend()); zhangsan.play(new Stranger()); } }
package com.ibeifeng.ex2; public class Friend { public void play(){ System.out.println("friends play"); } public Stranger getStranger(){ return new Stranger(); } }
package com.ibeifeng.ex2; public class MainClass { public static void main(String[] args) { SomeOne zhangsan = new SomeOne(); zhangsan.play(new Friend()); //zhangsan.play(new Stranger()); } }
package com.ibeifeng.ex2; public class SomeOne { public void play(Friend friends){ System.out.println("someone play"); friends.play(); Stranger stranger = friends.getStranger();//还是不符合迪米特法则,因为SomeOne //里面还是使用到了Stranger.虽然你这种获得方式是通过别人转发的,但是你还是使用了Stranger. //这种方式SomeOne和Stranger还是直接打了交道. //某人不要和陌生人说话,不应该有任何的陌生人的接收或者是引用. stranger.play(); } /* public void play(Stranger stranger){//SomeOne直接和Stranger通信/交互 System.out.println("someone play"); stranger.play(); }*/ }
package com.ibeifeng.ex2; public class Stranger { public void play(){ System.out.println("stranger play"); } }
package com.ibeifeng.ex3; public class Friend { public void play(){ System.out.println("friends play"); /* Stranger stranger = new Stranger(); stranger.play();*/ } /* public Stranger getStranger(){ return new Stranger(); }*/ public void playWithStranger(){ Stranger stranger = new Stranger(); stranger.play(); } }
package com.ibeifeng.ex3; public class MainClass { public static void main(String[] args) { SomeOne zhangsan = new SomeOne(); //zhangsan.play(new Friend()); zhangsan.setFriend(new Friend()); zhangsan.getFriend().playWithStranger(); //zhangsan.play(new Stranger()); } }
package com.ibeifeng.ex3; public class SomeOne { private Friend friend;//这样friend作为第三者,SomeOne没有 //和Stranger直接联系. public Friend getFriend() { return friend; } public void setFriend(Friend friend) { this.friend = friend; } public void play(Friend friends){ System.out.println("someone play"); friends.play(); //Stranger stranger = friends.getStranger();//还是不符合迪米特法则,因为SomeOne //里面还是使用到了Stranger.虽然你这种获得方式是通过别人转发的,但是你还是使用了Stranger. //这种方式SomeOne和Stranger还是直接打了交道. //某人不要和陌生人说话,不应该有任何的陌生人的接收或者是引用. //stranger.play(); } /* public void play(Stranger stranger){//SomeOne直接和Stranger通信/交互 System.out.println("someone play"); stranger.play(); }*/ }
package com.ibeifeng.ex3; public class Stranger { public void play(){ System.out.println("stranger play"); } }
为什么不要某人和陌生人直接说话呢就是避免某人和陌生人的耦合度比较高.要使这两个耦合变低才这样设计的.
依赖倒转原则是高层和底层模块依赖抽象模块.某人和抽象陌生人可以直接交互.有了里氏替换原则和依赖倒转原则,和抽象陌生人交互其实就可以与陌生人进行交互了.
SomeOne紧密联系的是陌生人抽象.是一个抽象类.某人和陌生人不是直接进行通信的,而是和陌生人的抽象直接进行通信.你随时可以替换掉陌生人.陌生人相当于公共模块,陌生人被替换成陌生人B不会对SomeOne造成影响,因为它们之间的耦合度很低,不是直接交互的.
package com.ibeifeng.ex4; public class Friend { public void play(){ System.out.println("friends play"); /* Stranger stranger = new Stranger(); stranger.play();*/ } /* public Stranger getStranger(){ return new Stranger(); }*/ /* public void playWithStranger(){ Stranger stranger = new Stranger(); stranger.play(); }*/ }
package com.ibeifeng.ex4; public class MainClass { public static void main(String[] args) { SomeOne zhangsan = new SomeOne(); //zhangsan.play(new Friend()); zhangsan.setFriend(new Friend()); zhangsan.setStranger(new StrangerA());//setStranger()的时候实际上set的它的实现类的实例 //zhangsan.getFriend().playWithStranger(); //zhangsan.play(new Stranger()); zhangsan.play(); } }
package com.ibeifeng.ex4; public class SomeOne { private Friend friend;//这样friend作为第三者,SomeOne没有 //和Stranger直接联系. private Stranger stranger; public Friend getFriend() { return friend; } public void setFriend(Friend friend) { this.friend = friend; } //public void play(Friend friends){ public void play(){ System.out.println("someone play"); //friends.play(); //Stranger stranger = friends.getStranger();//还是不符合迪米特法则,因为SomeOne //里面还是使用到了Stranger.虽然你这种获得方式是通过别人转发的,但是你还是使用了Stranger. //这种方式SomeOne和Stranger还是直接打了交道. //某人不要和陌生人说话,不应该有任何的陌生人的接收或者是引用. //stranger.play(); friend.play(); stranger.play(); } /* public void play(Stranger stranger){//SomeOne直接和Stranger通信/交互 System.out.println("someone play"); stranger.play(); }*/ public Stranger getStranger() { return stranger; } public void setStranger(Stranger stranger) { this.stranger = stranger; } }
package com.ibeifeng.ex4; /*public class Stranger { public void play(){ System.out.println("stranger play"); } } */ public abstract class Stranger { public abstract void play(); }
package com.ibeifeng.ex4; public class StrangerA extends Stranger { @Override public void play() { // TODO Auto-generated method stub System.out.println("strangerA play"); } }
病人去找其他医生没用,因为其他医生不认识你.所以病人和厉医生耦合度很高.
医生==上面的抽象陌生人.
与依赖倒转原则结合的迪米特法则.把抽象陌生人看作第三者,通过抽象陌生人来调用陌生人.迪米特法则也是为了降低类之间的耦合度.降低类之间的耦合,增强程序的扩展性.
外观模式Client不再与子系统直接交互而是与外观Facade直接交互.
Client:某人、病人
子系统或者是类库:陌生人、厉医生、张医生
Facade:facade、朋友、抽象陌生人、医生
它们之间没有任何联系.