Android设计模式(七)--原型模式

news/2024/7/6 0:21:41 标签: 移动开发, 设计模式, java

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:

浅拷贝:

java">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;
	}

}

深拷贝:
java">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 的对象。

java">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);
    }

}



http://www.niftyadmin.cn/n/1383215.html

相关文章

Java编程——服务器设计方案之应用限流

前言 在一个高并发系统中对流量的把控是非常重要的&#xff0c;当巨大的流量直接请求到我们的服务器上没多久就可能造成接口不可用&#xff0c;不处理的话甚至会造成整个应用不可用。 比如最近就有个这样的需求&#xff0c;我作为客户端要向kafka生产数据&#xff0c;而kafka的…

apache虚拟主机配置

常用的配置&#xff1a; <VirtualHost *:80> ServerAdmin romyredidai.com DocumentRoot "D:/var/www/user_admin/" ServerName user_admin.redidai.com ErrorLog "logs/romy-redidai.com-error.log" CustomLog "logs/romy-redidai.com-access.…

利用 DBHelper实现登录功能

一&#xff0e; 用DBHelper 与mysql 连接 实现最简单的登录验证。 &#xff08;1&#xff09;连接好mysql数据库&#xff0c;如果无法连接&#xff0c;先打开mysql服务。新建一个数据库&#xff0c;名字为text&#xff0c;再新建一个数据表&#xff0c;名字为user&#xff0c;…

Session与Cookie底层原理

2019独角兽企业重金招聘Python工程师标准>>> 学习目录 1.会话入门 2.cookie 3.session 4.自定义缓存 5.自定义Token &#xff16;.表单重复提交&#xff08;Http重复提交&#xff09; 一、会话管理入门 1.2、软件中的会话 打开浏览器 -> 浏览商品列表 -> 加…

Maven下载慢的解决方案

2019独角兽企业重金招聘Python工程师标准>>> 在使用maven构建的项目中&#xff0c;打开pom文件&#xff0c;将maven仓库指定为国内阿里云提供的仓库地址 如下配置&#xff1a; <!--设置maven 仓库为阿里云提供的maven仓库 国外的maven仓库慢--><repositori…

160 为什么不建议varchar(256),而是varchar(255)

数据库中的 varchar(255) MySQL | ver < 4.1&#xff1a; VARCHAR以字节为单位存储&#xff0c;所以假设全部为常用汉字&#xff08;UTF-8 3字节编码长度&#xff09;&#xff0c;则VARCHAR(255)共可存放约85个汉字&#xff1b; MySQL | ver > 4.1&#xff1a; VARCH…

【有奖征文】怎样做好linux安全运维

怎样做好linux安全运维什么叫安全&#xff0c;概括为三大类数据&#xff08;信息&#xff09;安全运行&#xff08;系统&#xff09;安全物理&#xff08;实体&#xff09;安全那么怎么才算做到企业安全了数据&#xff08;信息&#xff09;安全 指数据库的安全这里我用的数据库…

MySQL运维案例分析:Binlog中的时间戳

摘要&#xff1a; 本文从一个典型的案例入手来讲述Binlog中时间戳的原理和实践&#xff0c;通过本文你可以了解时间戳在Binlog中的作用及产生方法&#xff0c;以便在出现一些这方面怪异的问题时&#xff0c;做到心中有数&#xff0c;胸有成竹。本文选自《MySQL运维内参》 背景 …