Java編程思想之中間層檢測類

 首先請看如下的代碼。

  view plainprint?

  class Help { private int n;Help() { this.n = 0;} public void setMe(int n) { Helper.setValue(this, n);} public void setN(int n) { this.n = n;}

  class Helper { static public void setValue(Help h, int n) { h.setN(n);}

  public class TestQuestion{ public static void main(String[] args) { Help h = new Help();h.setMe(13);}

  Help的setMe方法調用了Helper的setValue方法,Helper的setValue方法又調用了Help的setN方法。這樣似乎中間的Helper沒有用。但是它可以加一些檢測操作,使得這種檢測操作分離出來。具體如下:view plainprint?

  class Help { private int n;Help() { this.n = 0;} public void setMe(int n) { Helper.setValue(this, n);} public void setN(int n) { this.n = n;}

  class Helper { static public void setValue(Help h, int n) { if (n < 1000) { h.setN(n);} else { System.out.println("The value is beyond 1000.");}

  public class TestQuestion{ public static void main(String[] args) { Help h = new Help(www.37meishi.com);h.setMe(13);}

  這樣可以把檢測分離出來,符合軟件工程團隊開發的的思想。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章