安卓设计模式之原型模式
来源:互联网 发布:常见数据库系统 编辑:程序博客网 时间:2024/06/14 00:29
用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
首先我们定义一个Person类
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
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; }
要实现原型模式,只需要按照下面的几个步骤去实现即可。
- 实现Cloneable接口
123
public class Person implements Cloneable{}
- 重写Object的clone方法
1234
@Overridepublic Object clone(){return null;}
- 实现clone方法中的拷贝逻辑
1234567891011121314
@Overridepublic 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;}
测试一下
1234567891011121314151617
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集合,就像这样子
123456789
private ArrayList<String> hobbies=new ArrayList<String>();public ArrayList<String> getHobbies() {return hobbies;}public void setHobbies(ArrayList<String> hobbies) {this.hobbies = hobbies;}
在进行拷贝的时候要格外注意,如果你直接按之前的代码那样拷贝
123456789101112131415
@Overridepublic 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;}
会带来一个问题
使用测试代码进行测试
1234567891011121314151617181920212223
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=[篮球, 编程, 长跑, 游泳]}
你会发现原来的对象的hobby也发生了变换。
其实导致这个问题的本质原因是我们只进行了浅拷贝,也就是只拷贝了引用,最终两个对象指向的引用是同一个,一个发生变化另一个也会发生变换,显然解决方法就是使用深拷贝。
12345678910111213141516
@Overridepublic 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函数里调用构造函数,构造函数的入参是该类对象。
1234
@Overridepublic Object clone(){return new Person(this);}
- 在构造函数中完成拷贝逻辑
1234567
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接口
123456789
public Object clone() {return new Bundle(this);} public Bundle(Bundle b) {super(b);mHasFds = b.mHasFds;mFdsKnown = b.mFdsKnown;}
然后是Intent类,该类也实现了Cloneable接口
12345678910111213141516171819202122232425262728
@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,再修改不同的地方,便可以直接使用。
123456
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接口
12345678910111213141516171819202122232425262728293031
/** Returns a shallow copy of this OkHttpClient. */
正如开头的注释Returns a shallow copy of this OkHttpClient,该clone方法返回了一个当前对象的浅拷贝对象。
- 安卓设计模式之原型模式
- 设计模式之原型
- 设计模式之原型
- 设计模式 之 原型
- 设计模式之原型
- 设计模式之原型
- 设计模式之原型模式
- 设计模式之原型模式
- 设计模式之-原型模式
- 设计模式之 原型模式
- 设计模式之原型模式
- 设计模式之原型模式
- 设计模式之原型模式
- 设计模式之原型模式
- 设计模式之原型模式
- 设计模式之--原型模式
- 设计模式之原型模式
- 设计模式之原型模式
- java反射类的相关知识
- 网站发布阶段的跌跌撞撞
- 拉姆达表达式相关知识
- mybatis与hibernate本质的区别以及应用的场景
- 安卓常用的设计模式之观察者模式
- 安卓设计模式之原型模式
- 正则表达式各种字符含义(链接)
- 在windows系统的cmd窗口中命令查询指定端口占用的进程并关闭
- podSpec带资源包实践
- mybatis学习之原始开发DAO与mapper代理开发DAO的区别
- 使用starUML一步一步画顺序图
- UML顺序图和通信图
- AxonFramework,事件发布和处理
- poj 1942 Paths on a Grid 简单排列组合 + unsigned long long 使用方法