android应用开发设计模式之代理模式

设计模式在软件设计中非常重要,目前发展中有23种模式,在android(java)中我们也有必要对其有一定的了解.在后面的学习中,我也学习总结一下,希望大家批评指正.首先我们看看代理模式.我们以游戏中的例子进行分析.

代理模式:对一些对象提供代理,以限制哪些对象去访问其它对象。

public interface buy_car {
    public String buy_car();
}

新建一个People类,具有买车的行为,所以实现接口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() {
	return username +"买了一台新车";
   }
}


people类不能拥有车,必须经过proxy代理类的认证,符合条件之后才可以拥有车辆,新建一个代理,这个代理类来考察当前的people是否有资格进行买车:

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

public class ProxyActivity extends Activity {

   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

source link:http://blog.csdn.net/jindegegesun/article/details/7291166

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值