原始模型(Prototype【创建模式第七篇】
原始模型(Prototype):
属于对象的创建模式。通过给出一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象。
java.lang.Object.clone()方法,所有javaBean都必须实现Cloneable接口,才具有clone功能;
Java语言提供的Cloneable接口只起一个作用,就是在运行时期通知java虚拟机可以安全地在这个类上使用clone()方法。通过调用
这个clone()方法可以得到一个对象的复制。由于Object类本身并实现Cloneable接口,因此如果所考虑的类没有实现Cloneable接
口时,调用clone()方法会抛出CloneNotSupportedException异常。
//PandaToClone类public class PandaToClone implements Cloneable{private int height, weight, age;public PandaToClone(int height, int weight){this.age = 0;this.weight = weight;this.height = height;}public void setAge(int age){this.age = age;}public int getAge(){return age;}//其他重复//克隆方法public Object clone(){//创建一个本类对象并返回给调用者PandaToClone temp = new PandaToClone(height,weight);temp.setAge(age);return (Object)temp;}}//客户端public class Client{private static PandaToClone thisPanda, thatPanda;public static void main(String args[]){thisPanda = new PandaToClone(15,25);thisPanda.setAge(3);thatPanda = (PandaToClone)thisPanda.clone();System.out.println("Age of this panda :" + thisPanda.getAge());System.out.println("Height : " + thisPanda.getHeight());System.out.println("Weigth : " + this.Panda.getWeight());System.out.println("Age of that panda :" + thatPanda.getAge());System.out.println("Height : " + thatPanda.getHeight());System.out.println("Weigth : " + that.Panda.getWeight());}}
//客户端public class Client{private Prototype prototype;public void operation(Prototype example){Prototype p = (Prototype)example.clone();}}//抽象原型角色public interface Prototype implements Cloneable{Prototype clone();}//具体原型角色public class ConcretePrototype implements Prototype{//克隆方法public Object clone(){try{return super.clone();}catch(CloneNotSupportedException e){return null;}}}2、登记形式的原始模型模式1):有如下角色:。 客户端(Client)角色:客户端类向管理员提出创建对象的请求。。 抽象原型(Prototype)角色:这是一个抽象角色,通常由一个接口或抽象类实现。 此角色给出所有的具体原型类所需要的接口。。 具体原型(Concrete Prototype)角色:被复制的对象。需要实现抽象原型角色所要求的接口。。 原型管理器(Prototype Manager)角色:创建具体原型类的对象,并记录每一个被创建的对象。//抽象原型角色public interface Prototype inplements Cloneable{public Object clone();}//具体原型角色public class ConcretePrototype implements Prototype{//克隆方法public synchronizer Object clone(){Prototype temp = null;try{temp = (Prototype)super.clone();return (Object)temp;}catch(CloneNotSupportedException e){System.out.println("Clone failed.");}finally{return (Object)temp;}}}//原型管理器角色保持一个聚集,作为多所有原型对象的登记,这个惧色提供必要的方法, 供外界增加新的原型对象和取得已经登记过的原型对象。//原型管理器类import java.util.Vector;public class PrototypeManager{private Vector objects = new Vector();//聚集管理方法:增加一个新的对象public void add(Prototype object){objects.add(object);}//聚集管理方法:取出聚集中的一个对象public Prototype get(int i){return (Prototype)objects.get(i);}//聚集管理方法:给出聚集的大小public int getSize(){return objects.size();}}//客户端public class Client{private PrototypeManager mgr;private Prototype prototype;public void registerPrototype(){prototype = new ConcretePrototype();Prototype copytype = (Prototype)prototype.clone();mgr.add(copytype);}}
//孙大圣本人类public class TheGreatestSage{//GreatestSage(大圣)private Monkey monkey = new Monkey();public void change(){//创建大圣本尊对象Monkey copyMonkey;//空循环一会儿for(int i = 0; i < 2000; i ++){}//克隆大圣本尊copyMonkey = (Monkey)monkey.clone();System.out.println("Monkey King's birth date = " + monkey.getBirthDate());System.out.println("Copy monkey's birth date = " + copyMonkey.getBirthDate());System.out.println("Monkey King == Copy Monkey?" + (monkey == copyMonkey));System.out.println("Monkey King's staff == Copy Monkey's staff?" + (monkey.getStaff() == copyMonkey.getStaff()));}public static void main(String args[]){TheGreatestSage sage = new TheGreatestSage();sage.change();}}////////////////import java.util.Date;public class Monkey inplements Colenable{private int height;private int weight;private GoldRingedStaff staff;private Date birthDate;public Monkey(){this.birhtDate = new Date();}public Object clone(){Monkey temp = null;try{temp = (Monkey)super.clone();}catch(CloneNotSupportedException e){System.out.println("Clone failed.");}finally{return (Object)temp;}}public int getHeight(){return height;}public void setHeight(int height){this.height = height;}//其他重复//生日的取值方法public Date getBirthDate(){return birthDate;}public void setBirthDate(Date birhtDate){this.birthDate = birhtDate;}}//金箍棒类public class GoldRingedStaff{private float height = 100.0F;private float diameter = 10.0F;public GoldRingedStaff(){//write your code here;}}//增长行为,每次调用长度和半径增加一倍public void grow(){this.diameter *= 2.0;this.height *= 2;}//缩小行为,每次调用长度和半径减少一半public void shrink(){this.diameter /= 2;this.height /= 2;}//移动public void move(){//write you code}public float getHeight(){return height;}public void setHeight(float heigth){this.height = height;}//半径的取值。赋值方法。。。。。。。。//深复制,必须要实现serializable接口import java.util.Date;import java.io.IOException;import java.lang.ClassNotFoundException;public class TheGreatestSage{private Monkey monkey = new Monkey();public void change() throws IOException,ClassNotFoundException{Monkey copyMonkey;for(int i = 0; i < 2000; i ++){}copyMonkey = (Monkey)monkey.deepClone();System.out.println("Monkey King's birth date = " + monkey.getBirthDate());System.out.println("Copy monkey's birth date = " + copyMonkey.getBirthDate());System.out.println("Monkey King == Copy Monkey?" + (monkey == copyMonkey));System.out.println("Monkey King's staff == Copy Monkey's staff?" + (monkey.getStaff() == copyMonkey.getStaff()));}public static void main(String args[]) throws IOException,ClassNotFoundException{TheGreatestSage sage = new TheGreatestSage();sage.change();}}//金箍棒类import java.util.Date;import java.io.Serializable;public class GoleRingedStaff implements Cloneable,Serializable{private float height = 100.0F;private float diameter = 10.0F;public GoldRingedStaff(){//write ..............}//跟上一样}