Android设计模式(七)--原型模式
2024-08-31 00:29:35
1、定义:
用原型实例指定创建对象种类,并通过拷贝这些原型创建新的对象。
2、目的:
从一个对象创建另外一个可定制的对象,而不须要知道不论什么创建细节。
3、作用:
3.1、简化对象的创建。
3.2 、对于处理大对象。性能上比new 高出非常多。
4、分类:
4.1浅拷贝:拷贝对象中的主要的数据类型。对于数组、容器对象、引用对象等都不会拷贝。
4.2深拷贝:将全部类型进行拷贝。
5、注意:
5.1对象实现Cloneable接口,必须将Object clone()
方法改为public;
5.2对于基本数据类型,其封装类型。String不须要进行处理。
他们进行的均是深拷贝。
6、简单的demo:
浅拷贝:
package com.example.demo.Prototype;
/**
* 浅拷贝
* @author qubian
* @data 2015年6月4日
* @email naibbian@163.com
*
*/
public class Prototype implements Cloneable { private int num;
private String name; public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public Object clone() {
Prototype prototype = null;
try {
prototype = (Prototype) super.clone(); } catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return prototype;
} }
深拷贝:
package com.example.demo.Prototype; import java.util.ArrayList;
import java.util.Vector;
/**
* 深拷贝
* @author qubian
* @data 2015年6月4日
* @email naibbian@163.com
*
*/
public class DeepPrototype implements Cloneable{ private String name;
private ArrayList<String> arrayList; private DeepObject deepObject; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getArrayList() {
return arrayList;
}
public void setArrayList(ArrayList<String> arrayList) {
this.arrayList = arrayList;
}
public DeepObject getDeepObject() {
return deepObject;
}
public void setDeepObject(DeepObject deepObject) {
this.deepObject = deepObject;
}
/**
* clone 方法
*/
@SuppressWarnings("unchecked")
@Override
public Object clone() {
DeepPrototype prototype = null;
try {
prototype = (DeepPrototype) super.clone();
prototype.arrayList=(ArrayList<String>) this.arrayList.clone();
prototype.deepObject=(DeepObject) this.deepObject.clone(); } catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return prototype;
} class DeepObject implements Cloneable
{
String name;
protected Object clone() {
DeepObject deep=null;
try {
deep= (DeepObject) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return deep;
};
}
}
7、原型模式在Android中的运用:
最明显的样例就是Intent,可是好像还未知其用处。
可是细看,竟然还是new 的对象。
public class Intent implements Parcelable, Cloneable {
/**
* Copy constructor.
*/
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;
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);
}
} @Override
public Object clone() {
return new Intent(this);
} }
最新文章
- SDWebImage清理图片缓存方法
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q105-Q108)
- Reset CSS
- 使用windows的远程桌面连接连接Ubuntu
- nodejs 任务调度使用
- extern 修饰符
- Gradle Goodness: Using and Working with Gradle Version
- javascript创建对象(二)
- BZOJ 1014 火星人prefix
- Guice学习(一)
- mysql 分区表详解
- SQL Server带游标的SQL
- Unreal 4 error 记录
- GitHub 托管的10款免费开源 windows 工具
- Leetcode-645 Set Mismatch
- 为NEO-GUI 添加插件系统
- Find the Top 10 commands in your linux box!
- 4C - 七夕节
- JS实现前端将数据导出excel
- win7重装系统后设置Python2.7环境
热门文章
- nyoj-673-悟空的难题(数组标记)
- 51nod-1131: 覆盖数字的数量
- 显示gif动画(帧动画的播放)
- mutt发邮件
- bzoj1025 [SCOI2009]游戏 动态规划
- HDU-1878 欧拉回路 欧拉回路
- python虚拟环境virtualenv、virtualenv下运行IDLE、powershell 运行脚本由执行策略引起的问题
- 紫书 例题 10-17 UVa 1639(数学期望+分数处理+处理溢出)
- UVALive 2659 数独 DLX模板
- 在Unix上用 BIND建立名称服务器(naem server)