android應用開發設計模式之面板模式

來源:互聯網
上載者:User

下面我們來學習下面板模式
[html]
<p>面板模式:提供了一個統一的介面,用來訪問子系統中的一群介面。</p><p> </p><p>建立一個Car類</p><pre class="java" name="code">package com.jindegege.car; 
 
public class Car { 
 
    public String start() { 
        return "車子已啟動"; 
    } 
 
    public String check_stop() { 
        return "檢查刹車"; 
    } 
 
    public String check_box() { 
        return "檢查油箱"; 
    } 
 
    public String check_console() { 
        return "檢查儀錶盤"; 
    } 
 
} 
 
2、建立一個外觀類(該類提供了一個統一的介面用來訪問Car類的介面方法)
[java]
package com.jindegege.car.facade; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
import com.jindegege.car.Car; 
 
 
 
 
public class Facade { 
 
    public Map<String, String> car_do(Car car) {//統一的介面 方法 
          Map<String,String> data= new HashMap<String,String>(); 
          /**
           * 用來訪問子系統中的一群介面方法
           * 
           */ 
         String data1=car.check_box(); 
         String data2=car.check_console(); 
         String data3=car.check_stop(); 
         String data4=car.start(); 
         data.put("data1", data1); 
         data.put("data2", data2); 
         data.put("data3", data3); 
         data.put("data4", data4); 
         return data; 
    } 
} 
3、建立android用戶端,給出xml以及activity:
[html]
<?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" 
        /> 
    <TextView 
        android:id="@+id/textview03" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
      /> 
    <TextView 
        android:id="@+id/textview04" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        /> 
 
</LinearLayout> 

主activity類:
[java]
package com.jindegege.facade; 
 
 
import java.util.Map; 
 
import com.jindegege.car.Car; 
import com.jindegege.car.facade.Facade; 
 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class FacadeActivity extends Activity { 
 private TextView textview01; 
 private TextView textview02; 
 private TextView textview03; 
 private TextView textview04; 
 private Facade car_impl; 
 private Map<String, String> data; 
    @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); 
        textview03=(TextView)findViewById(R.id.textview03); 
        textview04=(TextView)findViewById(R.id.textview04); 
        car_impl=new Facade(); 
        data=car_impl.car_do(new Car()); 
        if(textview01!=null&&data.get("data1")!=null){ 
            textview01.setText(data.get("data1").toString()); 
        } 
        if(textview02!=null&&data.get("data2")!=null){ 
            textview02.setText(data.get("data2").toString()); 
        } 
        if(textview03!=null&&data.get("data3")!=null){ 
            textview03.setText(data.get("data3").toString()); 
        } 
        if(textview04!=null&&data.get("data4")!=null){ 
            textview04.setText(data.get("data4").toString()); 
        } 
         
    } 
} 

下面看看實現的簡單效果:


是不是感覺很簡單啊,將子操作用一個外觀介面封裝起來,然後調用這個介面就可以調用那些非常複雜的微操作了。


 摘自 jindegegesun的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.