Android Application Development design mode: Prototype

Source: Internet
Author: User

Next, let's learn about the prototype.

Prototype: Use a prototype instance to specify the object type and copy the prototype to create a new object.

Interface for creating a racing car:

public interface car_interface {    public void start();    public void stop();}
Implementation class for creating a BMW Auto:
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;    }}

When creating a BMW accessories and tires, you must note that the clone method of the original protected type must be changed to public, so that it can be made public, called, and kept confidential.

Package com. jindegege. fitting; public class car_tyre {private string name = "original German tires"; Public String getname () {return name ;}}

Create an android client and provide XML and 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>
Below is the 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 ("My BMW parameter is:" + bmw1 ); textview02.settext ("My BMW tire parameters are:" + bmw1.getcar _ tyre_ref (); bmw2 = (bmw_impl) bmw1.clone (); textview03.settext ("others' BMW parameters are: "+ bmw2); textview04.settext (" others' BMW parameters: "+ bmw2);} catch (clonenotsupportedexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}

Below is:

Source code: http://download.csdn.net/detail/jindegegesun/4087689

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.