当前位置:操作系统 > 安卓/Android >>

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" />
&n

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,