Android[高級教程] 設計模式之六 中介者模式(二)

我們還是接著上一章節的中介者模式,這次我們來重構一下,唐僧隻告訴小妖讓他的徒弟們來救他,至於小妖通知誰,他不管,反正就是三個徒弟,小妖也想省事,就看見誰就通知誰唄。於是,我們的代碼修改成:

[java] public class Monster { 
 
    private Person person; 
    private String message; 
 
    public Monster() { 
         
    } 
 
    public void setPerson(String name) { 
         
        if (name.equals("悟空")) { 
             
            person = new Wukong(); 
             
        } else if (name.equals("八戒")) { 
             
            person = new Bajie(); 
             
        } else if (name.equals("沙僧")){ 
             
            person = new Shaseng(); 
        } 
         
        person.setMessage(message); 
    } 
 
    public String getMessage() { 
        return person.getMessage(); 
    } 
 
    public void setMessage(String message) { 
        this.message = message; 
    } 
 

public class Monster {

 private Person person;
 private String message;

 public Monster() {
  
 }

 public void setPerson(String name) {
  
  if (name.equals("悟空")) {
   
   person = new Wukong();
   
  } else if (name.equals("八戒")) {
   
   person = new Bajie();
   
  } else if (name.equals("沙僧")){
   
   person = new Shaseng();
  }
  
  person.setMessage(message);
 }

 public String getMessage() {
  return person.getMessage();
 }

 public void setMessage(String message) {
  this.message = message;
 }

}首先是小妖的代碼,他看見誰就通知誰,至於消息就是唐僧需要轉達的消息。接下來看一下唐僧的代碼:

[java] public class Tangseng { 
 
    private Monster monster; 
 
    public void sendMessage(Monster monster, String message) { 
 
        this.monster = monster; 
        monster.setMessage(message); 
 
    } 
 
    public String getMessage() { 
 
        return monster.getMessage(); 
 
    } 
 

public class Tangseng {

 private Monster monster;

 public void sendMessage(Monster monster, String message) {

  this.monster = monster;
  monster.setMessage(message);

 }

 public String getMessage() {

  return monster.getMessage();

 }

}裡面隻有小妖這個對象,他隻跟小妖接觸,隻對小妖說話。然後就是悟空的類瞭:

[java] public class Wukong implements Person { 
 
    private String name; 
    private String message; 
 
    public Wukong() { 
        this.name = "孫悟空"; 
    } 
 
    @Override 
    public void setMessage(String message) { 
 
        this.message = message; 
 
    } 
 
    @Override 
    public String getMessage() { 
 
        return name + "收到" + message; 
 
    } 
 

public class Wukong implements Person {

 private String name;
 private String message;

 public Wukong() {
  this.name = "孫悟空";
 }

 @Override
 public void setMessage(String message) {

  this.message = message;

 }

 @Override
 public String getMessage() {

  return name + "收到" + message;

 }

}
這裡三個實現類基本完成瞭,最後就是調用瞭,最終實現如下:

[java] public class XiyoujiActivity extends Activity { 
 
    private ListView listView; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        listView = (ListView) findViewById(R.id.listView1); 
        ArrayList<String> lists = new ArrayList<String>(); 
 
        Tangseng tangseng = new Tangseng(); 
        Monster monster = new Monster(); 
        tangseng.sendMessage(monster,"'徒弟救我'的消息"); 
         
        monster.setPerson("悟空"); 
        lists.add(tangseng.getMessage()); 
         
        monster.setPerson("八戒"); 
        lists.add(tangseng.getMessage()); 
         
         
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                android.R.layout.simple_list_item_1, lists); 
 
        listView.setAdapter(adapter); 
 
    } 

public class XiyoujiActivity extends Activity {

 private ListView listView;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  listView = (ListView) findViewById(R.id.listView1);
  ArrayList<String> lists = new ArrayList<String>();

  Tangseng tangseng = new Tangseng();
  Monster monster = new Monster();
  tangseng.sendMessage(monster,"'徒弟救我'的消息");
  
  monster.setPerson("悟空");
  lists.add(tangseng.getMessage());
  
  monster.setPerson("八戒");
  lists.add(tangseng.getMessage());
  
  
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, lists);

  listView.setAdapter(adapter);

 }
}最後運行結果如圖:

  

 

這裡我們就對中介模式進行瞭重構,這裡我還得說一下,其實設計模式最終需要實現的是低藕合,每個類之間盡量獨立。OK,謝謝大傢的關註。

 摘自 kangkangz4的專欄 

 

發佈留言