xxxx

来源:互联网 发布:app简约下载网页源码 编辑:程序博客网 时间:2024/04/26 21:40

 

package design.prototype.pattern;

public class Circle implements IPrototype, ICommand {

 @Override
 public Object Clone() {
  Object clone=null;
  try{
   clone=super.clone();
  }
  catch(CloneNotSupportedException e){
   System.err.println("Clone not support");
  }
  return clone;
 }

 @Override
 public String GetName() {
  // TODO Auto-generated method stub
  return "Circle";
 }

 @Override
 public void Draw() {
  System.out.println("Draw a Circle");

 }

}

####################

 

package design.prototype.pattern;

public interface ICommand {
public void Draw();
}
####################

package design.prototype.pattern;

/**
 * 指定创建对象的种类,并且通过拷贝这些原型  创建新的对象.prototype模式允许一个对象再创建另一个可定制的对象,<br>
 * 根本无需要知道任何创建的细节.工作原理是:通过将一个原型对象传给那个要发动创建的对象,<br>
 * 这个要发动创建的对象通过请求原型对象拷贝   原型自己   来实施创建过程.
 * @author gavin
 *
 */
public interface IPrototype extends Cloneable {
 public Object Clone();

 public String GetName();
}
######################

package design.prototype.pattern;

public class Lay1 implements Cloneable {
 public int x;
 public Lay2 lay2;

 public Object clone() {
  Object clone = null;
  try {
   clone = super.clone();
  } catch (CloneNotSupportedException e) {
   // TODO: handle exception
   e.printStackTrace();
  }
  return clone;
 }
}
#########################

package design.prototype.pattern;

public class Lay2 implements Cloneable {
 public int y;

 public Object clone() {
  Object clone = null;
  try {
   clone = super.clone();
  } catch (CloneNotSupportedException e) {
   e.printStackTrace();
  }
  return clone;
 }
}
##############################

package design.prototype.pattern;

import java.util.*;

public class PrototypeDemo {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
//  Lay1 obj1 = new Lay1();
//  obj1.lay2=new Lay2();
//  
//  List list=new ArrayList<Lay2>();
//  List list2=new ArrayList<Object>();
//  
//  Lay1 obj3= new Lay1(111,111);
//  for(Object l1:list){
//   obj3= (Lay1)obj3.clone();
//   
//   obj3.x= ((Lay2)l1).y;
//   
//   list2.add(obj3);
//  }

//  obj1.x=1;
//  obj1.lay2.y=1;
//  //Shallow Copy--浅表复制
//  Lay1 obj2=(Lay1)obj1.clone();
//  //Deep Copy--深层复制
////  obj2.lay2=(Lay2)obj1.lay2.clone();
//  obj2.x=2;
//  obj2.lay2.y=2;
//  System.out.println("obj1.x is:"+obj1.x+"/t obj1.lay2.y is:"+obj1.lay2.y);
//  System.out.println("obj2.x is:"+obj2.x+"/t obj2.lay2.y is:"+obj2.lay2.y);
 
  Toolbar toolbar=new Toolbar();
  String key="rectangle";
  Rectangle rect1=(Rectangle)toolbar.getClone(key);
  rect1.Draw();
  key="circle";
  Circle cir1=(Circle)toolbar.getClone(key);
  cir1.Draw();
  
 }

}

###################

package design.prototype.pattern;

public class Rectangle implements ICommand, IPrototype {

 @Override
 public void Draw() {
  System.out.println("Draw a Rectangle");

 }

 @Override
 public Object Clone() {
  Object clone=null;
  try{
   clone=super.clone();
  }catch(CloneNotSupportedException e){
   System.err.println("Clone not support");
  }
  return clone;
 }

 @Override
 public String GetName() {
  // TODO Auto-generated method stub
  return "Rectangle";
 }

}
########################

package design.prototype.pattern;

import java.util.Hashtable;
@SuppressWarnings("unchecked")
public class Toolbar {
 
 private Hashtable ht = new Hashtable();
 public Toolbar() {
  super();
  ht.put("circle", new Circle());
  ht.put("rectangle", new Rectangle());
 }
 public Object getClone(String key){
  return ((IPrototype)ht.get(key)).Clone();
 }
}

 

原创粉丝点击