Android[高級教程] 設計模式之八 橋接模式

現在我們說一下橋接模式,打個比方,橋接模式就好比是小時候玩的搭積木一樣,每一塊積木都有自己的造型,但將它們組合起來可以實現不同的樣式。我們還是拿《西遊記》中的主角來解釋這個模式,悟空是用金箍棒,八戒是用九齒釘耙,沙僧是用降妖寶杖的,每個使的兵器都是不同的,我們先來看一下例圖吧:

  

 

好瞭,我們首先定義一個武器接口:

[java] public interface Weapon{ 
     
    public String getWeapon(); 
     

public interface Weapon{
 
 public String getWeapon();
 
}接著再定義瞭一個人的接口,這裡面就有使用武器的方法,這裡需要說清楚的是:人使用武器,而不是武器使用人,所以我們把接口類放在Person裡面:

[java] public interface Person { 
     
    public void setWeapon(Weapon weapon); 
 

public interface Person {
 
 public void setWeapon(Weapon weapon);

}接口定義好以後就是每個接口的具體實現方法瞭:

我們先來定義三種武器:

金箍棒

[java] public class JinGuBang implements Weapon { 
 
    private String weapon; 
 
    public JinGuBang() { 
        this.weapon = "金箍棒"; 
    } 
 
    @Override 
    public String getWeapon() { 
         
        return weapon; 
    } 
 

public class JinGuBang implements Weapon {

 private String weapon;

 public JinGuBang() {
  this.weapon = "金箍棒";
 }

 @Override
 public String getWeapon() {
  
  return weapon;
 }

}九齒釘耙

[java] public class JiuChiDingPa implements Weapon { 
 
    private String weapon; 
 
    public JiuChiDingPa() { 
 
        this.weapon = "九齒釘耙"; 
 
    } 
 
    @Override 
    public String getWeapon() { 
 
        return weapon; 
    } 
 

public class JiuChiDingPa implements Weapon {

 private String weapon;

 public JiuChiDingPa() {

  this.weapon = "九齒釘耙";

 }

 @Override
 public String getWeapon() {

  return weapon;
 }

}最後就是降妖寶仗

[java] public class XiangYaoBaoZhang implements Weapon { 
 
    private String weapon; 
 
    public XiangYaoBaoZhang() { 
        this.weapon = "降妖寶仗"; 
    } 
 
    @Override 
    public String getWeapon() { 
 
        return weapon; 
    } 
 

public class XiangYaoBaoZhang implements Weapon {

 private String weapon;

 public XiangYaoBaoZhang() {
  this.weapon = "降妖寶仗";
 }

 @Override
 public String getWeapon() {

  return weapon;
 }

}三種武器定義好以後就是三個徒弟類的實現瞭:

[java] public class Wukong implements Person { 
 
    private String name; 
    private Weapon weapon; 
 
    public Wukong() { 
 
        this.name = "悟空"; 
 
    } 
 
    @Override 
    public void setWeapon(Weapon weapon) { 
 
        this.weapon = weapon; 
 
    } 
 
    @Override 
    public String toString() { 
 
        return name + "使用" + weapon.getWeapon(); 
    } 
 

public class Wukong implements Person {

 private String name;
 private Weapon weapon;

 public Wukong() {

  this.name = "悟空";

 }

 @Override
 public void setWeapon(Weapon weapon) {

  this.weapon = weapon;

 }

 @Override
 public String toString() {

  return name + "使用" + weapon.getWeapon();
 }

}這裡定義瞭悟空需要武器來打妖怪,但是哪種武器沒有具體說明,八戒和沙僧也是一樣的,這裡就不一一列出瞭,最後我們看一下Android中的實現:

[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); 
         
        //三個徒弟類www.aiwalls.com  
        Person wukong = new Wukong(); 
        Person bajie = new Bajie(); 
        Person shasheng = new Shaseng(); 
         
        //三種武器類  
        Weapon jinGuBang = new JinGuBang(); 
        Weapon jiuChiDingPa = new JiuChiDingPa(); 
        Weapon XiangYaoBaoZhang = new XiangYaoBaoZhang(); 
         
        //設置孫悟空的武器為"金箍棒"  
        wukong.setWeapon(jinGuBang); 
        //設置八戒的武器為"九齒釘耙"  
        bajie.setWeapon(jiuChiDingPa); 
        //設置沙僧的武器為"降妖寶杖"  
        shasheng.setWeapon(XiangYaoBaoZhang); 
         
        ArrayList<Person> persons = new ArrayList<Person>(); 
        persons.add(wukong); 
        persons.add(bajie); 
        persons.add(shasheng); 
 
        ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this, 
                android.R.layout.simple_list_item_1, persons); 
 
        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);
  
  //三個徒弟類
  Person wukong = new Wukong();
  Person bajie = new Bajie();
  Person shasheng = new Shaseng();
  
  //三種武器類
  Weapon jinGuBang = new JinGuBang();
  Weapon jiuChiDingPa = new JiuChiDingPa();
  Weapon XiangYaoBaoZhang = new XiangYaoBaoZhang();
  
  //設置孫悟空的武器為"金箍棒"
  wukong.setWeapon(jinGuBang);
  //設置八戒的武器為"九齒釘耙"
  bajie.setWeapon(jiuChiDingPa);
  //設置沙僧的武器為"降妖寶杖"
  shasheng.setWeapon(XiangYaoBaoZhang);
  
  ArrayList<Person> persons = new ArrayList<Person>();
  persons.add(wukong);
  persons.add(bajie);
  persons.add(shasheng);

  ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this,
    android.R.layout.simple_list_item_1, persons);

  listView.setAdapter(adapter);

 }
}裡面的註釋寫得比較清楚瞭,大傢應該能看明白,當然,你也可以給悟空其他武器用,OK,完成後的顯示效果如下:
   

 

同以前一樣,我們這裡也進行一下重構,因為具體類裡的變量跟方法都一樣,我們將Person方法改成抽像類,讓悟空,八戒,沙僧都繼承它,這樣,我們每個具體類裡隻要設置名字就可以瞭,其他的就不需要我們再寫瞭,看一下改好的代碼:Person類

[java] public abstract class Person { 
     
    private String name; 
    private Weapon weapon; 
     
    public void setName(String name){ 
         
        this.name = name; 
         
    } 
     
    public void setWeapon(Weapon weapon){ 
         
        this.weapon = weapon; 
         
    } 
     
    @Override 
    public String toString() { 
         
        return name + "使用" + weapon.getWeapon(); 
    } 
 

public abstract class Person {
 
 private String name;
 private Weapon weapon;
 
 public void setName(String name){
  
  this.name = name;
  
 }
 
 public void setWeapon(Weapon weapon){
  
  this.weapon = weapon;
  
 }
 
 @Override
 public String toString() {
  
  return name + "使用" + weapon.getWeapon();
 }

}裡面就具體實現一些具體類要實現的方法,然後就是悟空,八戒和沙僧的類瞭,你看瞭一定嚇一跳瞭,它們分別隻有一行代碼瞭:

[java] public class Wukong extends Person { 
 
    public Wukong() { 
 
        this.setName("悟空"); 
 
    } 
 

public class Wukong extends Person {

 public Wukong() {

  this.setName("悟空");

 }

}就是每個人設置一下名字就行瞭,其他的都是依賴繼承去實現瞭,另兩個跟悟空一樣,也是設置一個名字就行瞭。最終的Activity就是上面的Activity,Activity裡面不需要修改任何代碼,最終實現也跟上面的圖一樣,這裡,我們就介紹瞭橋接模式,並對我們的代碼進行瞭重構。再次謝謝大傢的關註。
 

摘自 kangkangz4的專欄 

發佈留言