Android中常用的设计模式 --- 原型模式
来源:互联网 发布:mysql基本语句大全 编辑:程序博客网 时间:2024/04/30 15:43
本篇文章介绍的模式其实很简单,即原型模式,按照惯例,先看定义。
用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
首先我们定义一个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方法
@Overridepublic 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; }
测试一下
public class Main { public static void main(String [] args){ Person p=new Person(); p.setAge(18); p.setName("张三"); p.setHeight(178); p.setWeight(65); System.out.println(p); Person p1= (Person) p.clone(); System.out.println(p1); p1.setName("李四"); System.out.println(p); System.out.println(p1); }}
输出结果如下
Person{name=’张三’, age=18, height=178.0, weight=65.0}
Person{name=’张三’, age=18, height=178.0, weight=65.0}
Person{name=’张三’, age=18, height=178.0, weight=65.0}
Person{name=’李四’, age=18, height=178.0, weight=65.0}试想一下,两个不同的人,除了姓名不一样,其他三个属性都一样,用原型模式进行拷贝就会显得异常简单,这也是原型模式的应用场景之一。
一个对象需要提供给其他对象访问,而且各个调用者可能都需要修改其值时,可以考虑使用原型模式拷贝多个对象供调用者使用,即保护性拷贝。
但是假设Person类里还有一个属性叫兴趣爱好,是一个List集合,就像这样子
private ArrayList<String> hobbies=new ArrayList<String>(); public ArrayList<String> getHobbies() { return hobbies; } public void setHobbies(ArrayList<String> hobbies) { 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; }
会带来一个问题
使用测试代码进行测试
public class Main { public static void main(String [] args){ Person p=new Person(); p.setAge(18); p.setName("张三"); p.setHeight(178); p.setWeight(65); ArrayList <String> hobbies=new ArrayList<String>(); hobbies.add("篮球"); hobbies.add("编程"); hobbies.add("长跑"); p.setHobbies(hobbies); System.out.println(p); Person p1= (Person) p.clone(); System.out.println(p1); p1.setName("李四"); p1.getHobbies().add("游泳"); System.out.println(p); System.out.println(p1); }}
我们拷贝了一个对象,并添加了一个兴趣爱好进去,看下打印结果
Person{name=’张三’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’张三’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’张三’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑, 游泳]}
Person{name=’李四’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑, 游泳]}//有集合的时候主要点----在cone()方法中
(错) person.hobbies=this.hobbies;改为
(对)person.hobbies=(ArrayList<String>)this.hobbies.clone();
你会发现原来的对象的hobby也发生了变换。
其实导致这个问题的本质原因是我们只进行了浅拷贝,也就是只拷贝了引用,最终两个对象指向的引用是同一个,一个发生变化另一个也会发生变换,显然解决方法就是使用深拷贝。
@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<String>)this.hobbies.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return person; }
注意person.hobbies=(ArrayList)this.hobbies.clone();,不再是直接引用而是进行了一份拷贝。再运行一下,就会发现原来的对象不会再发生变化了。
Person{name=’张三’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’张三’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’张三’, age=18, height=178.0, weight=65.0, hobbies=[篮球, 编程, 长跑]}
Person{name=’李四’, 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<String>(hobbies); }
其实都差不多,只是写法不一样。
现在来挖挖Android中的原型模式。
先看Bundle类,该类实现了Cloneable接口
public Object clone() { return new Bundle(this);} public Bundle(Bundle b) { super(b); mHasFds = b.mHasFds; mFdsKnown = b.mFdsKnown;}
然后是Intent类,该类也实现了Cloneable接口
@Overridepublic 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<String>(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); }}
用法也显得十分简单,一旦我们要用的Intent与现有的一个Intent很多东西都是一样的,那我们就可以直接拷贝现有的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);
网络请求中一个最常见的开源库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方法返回了一个当前对象的浅拷贝对象。
至于其他框架中的原型模式,请读者自行发现。
- Android中常用的设计模式 --- 原型模式
- Android的开发之&java23中设计模式------>原型模式
- Android的设计模式-原型模式
- Android 原型设计模式
- Android设计模式 ~~~ 原型模式
- Android设计模式-原型模式
- Android设计模式-原型模式
- Android常用设计模式——原型模式
- Android 开发中常用的设计模式
- Android中常用的设计模式
- android中常用的设计模式
- Android中常用的设计模式<一>
- android中最常用的设计模式
- Android中常用的设计模式
- Android开发中常用的设计模式
- Android中常用的设计模式
- Android中原型模式
- Android中常用的设计模式 --- 单例模式
- 关于局部变量和for循环测试
- Yarn 资源调度器
- java序列化和反序列化放缓存的问题
- JS中URL中的特殊字符问题:escape,encodeURI,encodeURIComponent
- 插件化开发---静态、动态代理
- Android中常用的设计模式 --- 原型模式
- 欢迎使用CSDN-markdown编辑器
- java 窗体程序
- 在Parallel中使用DbSet.Add()发现的一系列多线程问题和解决过程
- Dev-c++使用方法?Dev-c++怎么用?这里都有啦
- 格子中输出
- 170118
- apache与tomcat
- JSON.parse()和JSON.stringify(),HTML5本地存储localStorage、sessionStorage基本用法