1、什麼是迪米特法則設計模式
迪米特法則(Law of Demeter )又叫作最少知識 原則,也就是說,一個對象應當對其餘對象儘可 能少的瞭解。 迪米特法則最初是用來做爲面向對象的系統設 計風格的一種法則,於1987年秋天由lan holland 在美國東北大學爲一個叫作迪米特的項目設計提 出的。測試
2、狹義的迪米特法則this
若是兩個類沒必要彼此直接通訊,那麼這兩個類 就不該當發生直接的相互做用。若是其中一個類 須要調用另外一類的某一個方法的話,能夠經過第 三者轉發這個調用。spa
3、和陌生人說話設計
4、不要和陌生人說話3d
5、與依賴倒轉原則結合code
6、走後門看病對象
7、辦理手續住院blog
8、迪米特法則和設計模式get
1.外觀模式
2.中介者模式
ex1:
陌生人
1 //陌生人 2 public class Stranger { 3 public void play(){ 4 System.out.println("stranger play"); 5 } 6 }
朋友
1 //朋友 2 public class Friend { 3 public void play(){ 4 System.out.println("friends paly"); 5 } 6 }
某人
1 //某人 2 public class SomeOne { 3 public void play(Friend friend){ 4 System.out.println("someone play"); 5 friend.play(); 6 } 7 8 public void play(Stranger stranger) { 9 System.out.println("someone play"); 10 stranger.play(); 11 } 12 }
測試
1 public class MainClass { 2 public static void main(String[] args) { 3 SomeOne zhangsan = new SomeOne(); 4 zhangsan.play(new Friend()); 5 zhangsan.play(new Stranger()); 6 } 7 }
===============================================================
ex2:
1 //陌生人 2 public class Stranger { 3 public void play(){ 4 System.out.println("stranger play"); 5 } 6 }
1 //朋友 2 public class Friend { 3 public void play(){ 4 System.out.println("friends paly"); 5 } 6 7 public Stranger getStranger() { 8 return new Stranger(); 9 } 10 }
1 //某我的 2 public class SomeOne { 3 public void play(Friend friend){ 4 System.out.println("someone play"); 5 friend.play(); 6 Stranger stranger = friend.getStranger(); 7 stranger.play(); 8 } 9 }
1 public class MainClass { 2 public static void main(String[] args) { 3 SomeOne zhangsan = new SomeOne(); 4 zhangsan.play(new Friend()); 5 } 6 }
===============================================================
ex3:
1 //陌生人 2 public class Stranger { 3 public void play(){ 4 System.out.println("stranger play"); 5 } 6 }
1 //朋友 2 public class Friend { 3 public void play(){ 4 System.out.println("friends paly"); 5 } 6 7 public void playWithStranger() { 8 Stranger stranger = new Stranger(); 9 stranger.play(); 10 } 11 }
1 //某我的 2 public class SomeOne { 3 private Friend friend; 4 5 public Friend getFriend() { 6 return friend; 7 } 8 9 public void setFriend(Friend friend) { 10 this.friend = friend; 11 } 12 13 public void play(Friend friend){ 14 System.out.println("someone play"); 15 friend.play(); 16 } 17 }
=======================================================================
ex4:
1 public abstract class Stranger { 2 public abstract void play(); 3 }
1 public class StrangerA extends Stranger { 2 3 public void play() { 4 System.out.println("strangerA play"); 5 } 6 }
1 public class Friend { 2 public void play(){ 3 System.out.println("friends paly"); 4 } 5 }
1 public class SomeOne { 2 private Friend friend; 3 private Stranger stranger; 4 5 public Stranger getStranger() { 6 return stranger; 7 } 8 9 public void setStranger(Stranger stranger) { 10 this.stranger = stranger; 11 } 12 13 public Friend getFriend() { 14 return friend; 15 } 16 17 public void setFriend(Friend friend) { 18 this.friend = friend; 19 } 20 21 public void play() { 22 System.out.println("someone play"); 23 friend.play(); 24 stranger.play(); 25 } 26 }
1 public class MainClass { 2 public static void main(String[] args) { 3 SomeOne zhangsan = new SomeOne(); 4 zhangsan.setFriend(new Friend()); 5 zhangsan.setStranger(new StrangerA()); 6 zhangsan.play(); 7 } 8 }