[セットトップ]androidアプリケーション開発設計モードの代理モード
デザインモードはソフトウェア設計において非常に重要であり、現在の発展の中で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