android應用開發設計模式之代理模式

設計模式在軟件設計中非常重要,目前發展中有23種模式,在android(java)中我們也有必要對其有一定的瞭解.在後面的學習中,我也學習總結一下,希望大傢批評指正.首先我們看看代理模式.我們以遊戲中的例子進行分析.

      代理模式:對一些對象提供代理,以限制哪些對象去訪問其它對象。

[java]
package com.jindegege.service;   
   
public interface buy_car {   
   public String buy_car();   

package com.jindegege.service; 
 
public interface buy_car { 
   public String buy_car(); 
}

新建一個People類,具有買車的行為,所以實現接口buy_car:

[java]
package com.jindegege.buy_car; 
 
import com.jindegege.service.buy_car; 
 
public class People implements buy_car { 
 
    private int cash; 
    private String username; 
 
    public int getCash() { 
        return cash; 
    } 
 
    public void setCash(int cash) { 
        this.cash = cash; 
    } 
 
    public String getUsername() { 
        return username; 
    } 
 
    public void setUsername(String username) { 
        this.username = username; 
    } 
 
 @Override 
 public String buy_car() { 
  // TODO Auto-generated method stub  
   
   
   return   username + "買瞭一臺新車"; 
 } 
    

package com.jindegege.buy_car;

import com.jindegege.service.buy_car;

public class People implements buy_car {

    private int cash;
    private String username;

    public int getCash() {
        return cash;
    }

    public void setCash(int cash) {
        this.cash = cash;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

 @Override
 public String buy_car() {
  // TODO Auto-generated method stub
 
 
   return   username + "買瞭一臺新車";
 }
  
}

people類不能擁有車,必須經過proxy代理類的認證,符合條件之後才可以擁有車輛,新建一個代理,這個代理類來考察當前的people是否有資格進行買車:

[java]
package com.jindegege.buy_car; 
 
import com.jindegege.service.buy_car; 
 
public class buy_car_impl implements buy_car { 
    private People people; 
 
    public People getPeople() { 
        return people; 
    } 
 
    public void setPeople(People people) { 
        this.people = people; 
    } 
 
    public String buy_car() { 
        if (people.getCash() > 2000) { 
           return people.getUsername() + "花" + people.getCash()+ "塊買瞭新車,交易結束!"; 
        } else { 
            return people.getUsername() + "金錢不夠,請繼續比賽!"; 
        } 
         
    } 
package com.jindegege.buy_car;

import com.jindegege.service.buy_car;

public class buy_car_impl implements buy_car {
 private People people;

    public People getPeople() {
        return people;
    }

    public void setPeople(People people) {
        this.people = people;
    }

    public String buy_car() {
        if (people.getCash() > 2000) {
           return people.getUsername() + "花" + people.getCash()+ "塊買瞭新車,交易結束!";
        } else {
            return people.getUsername() + "金錢不夠,請繼續比賽!";
        }
       
    }

 

下面我們創建一個andriod客戶端(控制層),用來控制前面的業務層,先給出一個簡單的xml

[java]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <TextView 
        android:id="@+id/textview01" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" /> 
    <TextView 
        android:id="@+id/textview02" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <TextView
        android:id="@+id/textview02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

 

然後給出這個activity

[java]
package com.jindegege.activity; 
 
import com.jindegege.buy_car.People; 
import com.jindegege.buy_car.buy_car_impl; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
 
 
public class ProxyActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private People people1; 
    private People people2; 
    private People people3; 
    private buy_car_impl impl; 
    private static final String TAG1="people1"; 
    private static final String TAG2="people2"; 
    private TextView textview01; 
    private TextView textview02; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        textview01 =(TextView)findViewById(R.id.textview01); 
        textview02 =(TextView)findViewById(R.id.textview02); 
        people1 = new People(); 
        people1.setCash(5000); 
        people1.setUsername("jindegege"); 
 
        people2 = new People(); 
        people2.setCash(200); 
        people2.setUsername("biyumeimei"); 
 
        impl = new buy_car_impl(); 
        impl.setPeople(people1); 
        impl.buy_car(); 
        String data1=impl.buy_car(); 
        //Log.i(TAG1,data1);  
        textview01.setText(data1); 
 
        impl.setPeople(people2); 
        String data2= impl.buy_car(); 
        //Log.i(TAG2,data2);  
        textview02.setText(data2); 
 
 
    } 

package com.jindegege.activity;

import com.jindegege.buy_car.People;
import com.jindegege.buy_car.buy_car_impl;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ProxyActivity extends Activity {
    /** Called when the activity is first created. */
 private People people1;
 private People people2;
 private People people3;
 private buy_car_impl impl;
 private static final String TAG1="people1";
 private static final String TAG2="people2";
 private TextView textview01;
 private TextView textview02;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textview01 =(TextView)findViewById(R.id.textview01);
        textview02 =(TextView)findViewById(R.id.textview02);
        people1 = new People();
        people1.setCash(5000);
        people1.setUsername("jindegege");

        people2 = new People();
        people2.setCash(200);
        people2.setUsername("biyumeimei");

        impl = new buy_car_impl();
        impl.setPeople(people1);
        impl.buy_car();
        String data1=impl.buy_car();
        //Log.i(TAG1,data1);
        textview01.setText(data1);

        impl.setPeople(people2);
        String data2= impl.buy_car();
        //Log.i(TAG2,data2);
        textview02.setText(data2);

    }
}

下面看看效果圖:

 

 源代碼下載地址:http://up.aiwalls.com/2012/0227/20120227112827777.rar

摘自 jindegegesun的專欄

發佈留言