国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > Android开发中无处不在的设计模式――原型模式

Android开发中无处不在的设计模式――原型模式

来源:程序员人生   发布时间:2016-03-22 08:58:12 阅读次数:2254次

不知不觉这个系列已写了3篇了,其实很早之前就想写设计模式了,只不过怕自己误人子弟没有提笔去写。后来在实际开发中,发现设计模式可让1个开发人员融会贯通所学的知识,为了进1步巩固自己,就写下了这1些列文章。前面介绍了3个模式。

  • Android开发中无处不在的设计模式――单例模式
  • Android开发中无处不在的设计模式――Builder模式
  • Android开发中无处不在的设计模式――视察者模式

本篇文章介绍的模式其实很简单,即原型模式,依照惯例,先看定义。

用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。

这是甚么鬼哦,本宝宝看不懂!没必要过度在乎这些定义,自己心里明白就ok了。没代码你说个jb。

首先我们定义1个Person类

public class Person{ private String name; private int age; private double height; private double weight; public Person(){ } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public String toString() { return "Person{" + "name=" + name + + ", age=" + age + ", height=" + height + ", weight=" + weight + }; } }

要实现原型模式,只需要依照下面的几个步骤去实现便可。

  • 实现Cloneable接口
public class Person implements Cloneable{ }
  • 重写Object的clone方法
@Override public Object clone(){ return null; }
  • 实现clone方法中的拷贝逻辑
@Override public Object clone(){ Person person=null; try { person=(Person)super.clone(); person.name=this.name; person.weight=this.weight; person.height=this.height; person.age=this.age; } catch (CloneNotSupportedException e) { e.printStackTrace(); } return person; }

测试1下

public class Main { public static void main(String [] args){ Person p=new Person(); p.setAge(18); p.setName("张3"); p.setHeight(178); p.setWeight(65); System.out.println(p); Person p1= (Person) p.clone(); System.out.println(p1); p1.setName("李4"); System.out.println(p); System.out.println(p1); } }

输出结果以下

Person{name=’张3’, age=18, height=178.0, weight=65.0}
Person{name=’张3’, age=18, height=178.0, weight=65.0}
Person{name=’张3’, age=18, height=178.0, weight=65.0}
Person{name=’李4’, age=18, height=178.0, weight=65.0}

试想1下,两个不同的人,除姓名不1样,其他3个属性都1样,用原型模式进行拷贝就会显得异常简单,这也是原型模式的利用场景之1。

1个对象需要提供给其他对象访问,而且各个调用者可能都需要修改其值时,可以斟酌使用原型模式拷贝多个对象供调用者使用,即保护性拷贝。

但是假定Person类里还有1个属性叫兴趣爱好,是1个List集合,就像这模样

private ArrayListhobbies=new ArrayList(); public ArrayListgetHobbies() { return hobbies; } public void setHobbies(ArrayListhobbies) { this.hobbies = hobbies; }

在进行拷贝的时候要格外注意,如果你直接按之前的代码那样拷贝

@Override public Object clone(){ Person person=null; try { person=(Person)super.clone(); person.name=this.name; person.weight=this.weight; person.height=this.height; person.age=this.age; person.hobbies=this.hobbies; } catch (CloneNotSupportedException e) { e.printStackTrace(); } return person; }

会带来1个问题

使用测试代码进行测试

public class Main { public static void main(String [] args){ Person p=new Person(); p.setAge(18); p.setName("张3"); p.setHeight(178); p.setWeight(65); ArrayListhobbies=new ArrayList(); hobbies.add("篮球"); hobbies.add("编程"); hobbies.add("长跑"); p.setHobbies(hobbies); System.out.println(p); Person p1= (Person) p.clone(); System.out.println(p1); p1.setName("李4"); p1.getHobbies().add("游泳"); System.out.println(p); System.out.println(p1); } }

我们拷贝了1个对象,并添加了1个兴趣爱好进去,看下打印结果

Person{name=’张3’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’张3’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’张3’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑, 游泳]}
Person{name=’李4’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑, 游泳]}

你会发现原来的对象的hobby也产生了变换。

其实致使这个问题的本质缘由是我们只进行了浅拷贝,也就是只拷贝了援用,终究两个对象指向的援用是同1个,1个产生变化另外一个也会产生变换,明显解决方法就是使用深拷贝。

@Override public Object clone(){ Person person=null; try { person=(Person)super.clone(); person.name=this.name; person.weight=this.weight; person.height=this.height; person.age=this.age; person.hobbies=(ArrayList)this.hobbies.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return person; }

注意person.hobbies=(ArrayList)this.hobbies.clone();,不再是直接援用而是进行了1份拷贝。再运行1下,就会发现原来的对象不会再产生变化了。

Person{name=’张3’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’张3’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’张3’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’李4’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑, 游泳]}

其实有时候我们会更多的看到原型模式的另外一种写法。

  • 在clone函数里调用构造函数,构造函数的入参是该类对象。
@Override public Object clone(){ return new Person(this); }
  • 在构造函数中完成拷贝逻辑
public Person(Person person){ this.name=person.name; this.weight=person.weight; this.height=person.height; this.age=person.age; this.hobbies= new ArrayList(hobbies); }

其实都差不多,只是写法不1样。

现在来挖挖android中的原型模式。

先看Bundle类,该类实现了Cloneable接口

public Object clone() { return new Bundle(this); } public Bundle(Bundle b) { super(b); mHasFds = b.mHasFds; mFdsKnown = b.mFdsKnown; }

然后是Intent类,该类也实现了Cloneable接口

@Override public Object clone() { return new Intent(this); } public Intent(Intent o) { this.mAction = o.mAction; this.mData = o.mData; this.mType = o.mType; this.mPackage = o.mPackage; this.mComponent = o.mComponent; this.mFlags = o.mFlags; this.mContentUserHint = o.mContentUserHint; if (o.mCategories != null) { this.mCategories = new ArraySet(o.mCategories); } if (o.mExtras != null) { this.mExtras = new Bundle(o.mExtras); } if (o.mSourceBounds != null) { this.mSourceBounds = new Rect(o.mSourceBounds); } if (o.mSelector != null) { this.mSelector = new Intent(o.mSelector); } if (o.mClipData != null) { this.mClipData = new ClipData(o.mClipData); } }

用法也显得10分简单,1旦我们要用的Intent与现有的1个Intent很多东西都是1样的,那我们就能够直接拷贝现有的Intent,再修改不同的地方,即可以直接使用。

Uri uri = Uri.parse("smsto:10086"); Intent shareIntent = new Intent(Intent.ACTION_SENDTO, uri); shareIntent.putExtra("sms_body", "hello"); Intent intent = (Intent)shareIntent.clone() ; startActivity(intent);

网络要求中1个最多见的开源库OkHttp中,也利用了原型模式。它就在OkHttpClient这个类中,它实现了Cloneable接口

/** Returns a shallow copy of this OkHttpClient. */ @Override public OkHttpClient clone() { return new OkHttpClient(this); } private OkHttpClient(OkHttpClient okHttpClient) { this.routeDatabase = okHttpClient.routeDatabase; this.dispatcher = okHttpClient.dispatcher; this.proxy = okHttpClient.proxy; this.protocols = okHttpClient.protocols; this.connectionSpecs = okHttpClient.connectionSpecs; this.interceptors.addAll(okHttpClient.interceptors); this.networkInterceptors.addAll(okHttpClient.networkInterceptors); this.proxySelector = okHttpClient.proxySelector; this.cookieHandler = okHttpClient.cookieHandler; this.cache = okHttpClient.cache; this.internalCache = cache != null ? cache.internalCache : okHttpClient.internalCache; this.socketFactory = okHttpClient.socketFactory; this.sslSocketFactory = okHttpClient.sslSocketFactory; this.hostnameVerifier = okHttpClient.hostnameVerifier; this.certificatePinner = okHttpClient.certificatePinner; this.authenticator = okHttpClient.authenticator; this.connectionPool = okHttpClient.connectionPool; this.network = okHttpClient.network; this.followSslRedirects = okHttpClient.followSslRedirects; this.followRedirects = okHttpClient.followRedirects; this.retryOnConnectionFailure = okHttpClient.retryOnConnectionFailure; this.connectTimeout = okHttpClient.connectTimeout; this.readTimeout = okHttpClient.readTimeout; this.writeTimeout = okHttpClient.writeTimeout; }

正如开头的注释Returns a shallow copy of this OkHttpClient,该clone方法返回了1个当前对象的深拷贝对象。



生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生