设计模式之原型模式

原型模式

  定义:
  用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
  Prototype原型模式是一种创建型设计模式,Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。
  解决什么问题:
  它主要面对的问题是:“某些结构复杂的对象”的创建工作;由于需求的变化,这些对象经常面临着剧烈的变化,但是他们却拥有比较稳定一致的接口。
  如何使用?
  因为Java中的提供clone()方法来实现对象的克隆,所以Prototype模式实现一下子变得很简单.
 
Java 实现代码
package com.picc.cl;

public class Teacher implements Cloneable {
	public Teacher(){}
	public Teacher(String name,int total){
		this.name=name;
		this.total=total;
	}
	private String name;
	private int total;
	public static void main(String[] args) {
		Teacher teacher = new Teacher();
		teacher.setName("张三");
		teacher.setTotal(2000);
		System.out.println(teacher.toString());
		try {
			Teacher teacher1 =teacher.clone();
			teacher1.setName("李四");
			teacher1.setTotal(9000);
			System.out.println(teacher1.toString());
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	@Override
	protected Teacher clone() throws CloneNotSupportedException {

		return (Teacher)super.clone();
	}

	@Override
	public String toString() {

		return  "姓名:"+this.name+"\t薪水:"+this.total;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + total;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Teacher other = (Teacher) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (total != other.total)
			return false;
		return true;
	}

}

输出结果
姓名:张三 薪水:2000
姓名:李四 薪水:9000
代码改进
package com.picc.cl;

public class Teacher implements Cloneable {
	public Teacher(){}
	public static Teacher copyTeacher;//作为一个类对象保存Teahcer 实例
	static{
		copyTeacher = new Teacher();
	}
	public Teacher(String name,int total){
		this.name=name;
		this.total=total;
	}
	private String name;
	private int total;
	public static void main(String[] args) {
		try {
			
			Teacher teacher1 =Teacher.copyTeacher.clone();
			teacher1.setName("李四");
			teacher1.setTotal(9000);
			System.out.println(teacher1.toString());
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	@Override
	protected Teacher clone() throws CloneNotSupportedException {

		return (Teacher)super.clone();
	}

	@Override
	public String toString() {

		return  "姓名:"+this.name+"\t薪水:"+this.total;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + total;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Teacher other = (Teacher) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (total != other.total)
			return false;
		return true;
	}

}
思考: 在需要Teacher 实例的时候,不需要通过new 创建一个新的实例。只需要获取一个Teacher 的副本,可以减小内存的开销。
相关文章
相关标签/搜索