设计模式在
软件设计中非常重要,目前发展中有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);
}
}
下面看看效果图: