android應用開發設計模式之原型模式

來源:互聯網
上載者:User

 下面我們來學習下原型模式

原型模式:用原型執行個體制定建立對象的種類,並且通過拷貝這些原型建立新的對象。

建立賽車的介面:

public interface car_interface {    public void start();    public void stop();}
建立寶馬汽車的實作類別:
package com.jindegege.car;import com.jindegege.fitting.car_tyre;import com.jindegege.service.car_interface;public class bmw_impl implements car_interface, Cloneable {    private car_tyre car_tyre_ref;    private bmw_impl bmw;    public void start() {           }    public void stop() {           }    public car_tyre getCar_tyre_ref() {        return car_tyre_ref;    }    public void setCar_tyre_ref(car_tyre car_tyre_ref) {        this.car_tyre_ref = car_tyre_ref;    }    @Override    public Object clone() throws CloneNotSupportedException {        super.clone();        bmw = new bmw_impl();        bmw.setCar_tyre_ref(new car_tyre());        return bmw;    }}

建立寶馬的配件輪胎類在寶馬汽車實作類別中需要注意的是將原來protected類型的clone方法要變成public,這樣才可以對外公開,可以被調用,將秘密公開化。

package com.jindegege.fitting;public class car_tyre {    private String name = "德國製造原版輪胎";    public String getName() {        return name;    }}

建立android用戶端,給出xml以及activity:

<?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"         />    <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
package com.jindegege.activity;import com.jindegege.car.bmw_impl;import com.jindegege.fitting.car_tyre;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;import android.widget.Toast;public class PrototypeActivity extends Activity {   private bmw_impl bmw1;   private bmw_impl bmw2;   private TextView textview01;   private TextView textview02;   private TextView textview03;   private TextView textview04;       @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        try {textview01= (TextView)findViewById(R.id.textview01);textview02=(TextView)findViewById(R.id.textview02);textview03= (TextView)findViewById(R.id.textview03);textview04=(TextView)findViewById(R.id.textview04);bmw1 = new bmw_impl();bmw1.setCar_tyre_ref(new car_tyre());textview01.setText("我的寶馬參數是:" + bmw1);textview02.setText("我的寶馬的輪胎參數是:" + bmw1.getCar_tyre_ref());bmw2 = (bmw_impl) bmw1.clone();textview03.setText("他人的寶馬的參數是:" + bmw2);textview04.setText("他人的寶馬的參數是:" + bmw2);} catch (CloneNotSupportedException e) {// TODO Auto-generated catch blocke.printStackTrace();}            } }

下面是:

原始碼:http://download.csdn.net/detail/jindegegesun/4087689
相關文章

聯繫我們

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