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

來源:互聯網
上載者:User

 

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

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

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


建立一個People類,具有買車的行為,所以實現介面buy_car:

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是否有資格進行買車:

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

<?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

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://download.csdn.net/detail/jindegegesun/4087696

 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.