android应用开发设计模式之原型模式
下面我们来学习下原型模式
原型模式:用原型实例制定创建对象的种类,并且通过拷贝这些原型创建新的对象。
新建赛车的接口:
[java]
public inte易做图ce car_inte易做图ce {
public void start();
public void stop();
}
public inte易做图ce car_inte易做图ce {
public void start();
public void stop();
}
[java]
新建宝马汽车的实现类:
新建宝马汽车的实现类:[java]
<pre class="java" name="code">package com.jindegege.car;
import com.jindegege.fitting.car_tyre;
import com.jindegege.service.car_inte易做图ce;
public class bmw_impl implements car_inte易做图ce, 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;
}
}
<pre class="java" name="code">package com.jindegege.car;
import com.jindegege.fitting.car_tyre;
import com.jindegege.service.car_inte易做图ce;
public class bmw_impl implements car_inte易做图ce, 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,这样才可以对外公开,可以被调用,将秘密公开化。
[java]
package com.jindegege.fitting;
public class car_tyre {
private String name = "德国制造原版轮胎";
public String getName() {
return name;
}
}
package com.jindegege.fitting;
public class car_tyre {
private String name = "德国制造原版轮胎";
public String getName() {
return name;
}
}
新建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"
/>
<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>
<?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 ,