ԭ��ģʽ
ԭ��ģʽ��Prototype Pattern�������ڴ����ظ��Ķ���ͬʱ���ܱ�֤���ܡ��������͵����ģʽ���ڴ�����ģʽ�����ṩ��һ�ִ����������ѷ�ʽ��
����ģʽ��ʵ����һ��ԭ�ͽӿڣ��ýӿ����ڴ�����ǰ����Ŀ�¡����ֱ�Ӵ�������Ĵ��۱Ƚϴ�ʱ�����������ģʽ�����磬һ��������Ҫ��һ���ߴ��۵����ݿ����֮���������ǿ��Ի���ö�������һ������ʱ�������Ŀ�¡������Ҫ��ʱ��������ݿ⣬�Դ����������ݿ���á�
����
��ͼ����ԭ��ʵ��ָ��������������࣬����ͨ��������Щԭ�ʹ����µĶ���
��Ҫ������������ڽ�����ɾ��ԭ�͡�
��ʱʹ�ã� 1����һ��ϵͳӦ�ö��������IJ�Ʒ���������ɺͱ�ʾʱ�� 2����Ҫʵ����������������ʱ��ָ��ʱ�����磬ͨ����̬װ�ء� 3��Ϊ�˱��ⴴ��һ�����Ʒ����ƽ�еĹ�������ʱ�� 4����һ�����ʵ��ֻ���м�����ͬ״̬����е�һ��ʱ��������Ӧ��Ŀ��ԭ�Ͳ���¡���ǿ��ܱ�ÿ���ú��ʵ�״̬�ֹ�ʵ�������������һЩ��
��ν�����������е�һ��ԭ�Ͷ����ٵ����ɺ�ԭ�Ͷ���һ����ʵ����
�ؼ����룺 1��ʵ�ֿ�¡�������� JAVA �̳� Cloneable����д clone()���� .NET �п���ʹ�� Object ��� MemberwiseClone() ������ʵ�ֶ����dz������ͨ�����л��ķ�ʽ��ʵ������� 2��ԭ��ģʽͬ�����ڸ���������ʹ���ߺ;������ͣ��ױ��ࣩ֮�����Ϲ�ϵ����ͬ��Ҫ����Щ”�ױ���”ӵ���ȶ��Ľӿڡ�
Ӧ��ʵ���� 1��ϸ�����ѡ� 2��JAVA �е� Object clone() ������
�ŵ㣺 1��������ߡ� 2���ӱܹ��캯����Լ����
ȱ�㣺 1���䱸��¡������Ҫ����Ĺ��ܽ���ͨ�̿��ǣ������ȫ�µ���Ǻ��ѣ����������е��һ�������ף��ر�һ�������ò�֧�ִ��л��ļ�Ӷ��������ú���ѭ���ṹ��ʱ�� 2������ʵ�� Cloneable �ӿڡ�
ʹ�ó����� 1����Դ�Ż������� 2�����ʼ����Ҫ�����dz������Դ�������Դ�������ݡ�Ӳ����Դ�ȡ� 3�����ܺͰ�ȫҪ��ij����� 4��ͨ�� new ����һ��������Ҫ�dz������������������Ȩ�ޣ������ʹ��ԭ��ģʽ�� 5��һ�����������ߵij����� 6��һ��������Ҫ�ṩ������������ʣ����Ҹ��������߿��ܶ���Ҫ����ֵʱ�����Կ���ʹ��ԭ��ģʽ���������������ʹ�á� 7����ʵ����Ŀ�У�ԭ��ģʽ���ٵ������֣�һ���Ǻ�������ģʽһ����֣�ͨ�� clone �ķ�������һ������Ȼ���ɹ��������ṩ�������ߡ�ԭ��ģʽ�Ѿ��� Java ��Ϊ��Ȼһ�壬��ҿ�����������ʹ�á�
ע�������ͨ����һ�������ʵ�����������¶���ͬ���ǣ�ԭ��ģʽ��ͨ������һ�����ж��������¶���ġ�dz����ʵ�� Cloneable����д�������ͨ��ʵ�� Serializable ��ȡ����������
ʵ��
���ǽ�����һ�������� Shape ����չ�� Shape ���ʵ���ࡣ��һ���Ƕ����� ShapeCache������� shape ����洢��һ�� Hashtable �У����������ʱ�����ǵĿ�¡��
PrototypePatternDemo ��ʹ�� ShapeCache ������ȡ Shape ����

���� 1
����һ��ʵ���� Cloneable �ӿڵij����ࡣ
public abstract class Shape implements Cloneable {private String id;protected String type;abstract void draw();public String getType(){return type;}public String getId() {return id;}public void setId(String id) {this.id = id;}public Object clone() {Object clone = null;try {clone = super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return clone;}}
���� 2
������չ������������ʵ���ࡣ
public class Rectangle extends Shape {public Rectangle(){type = "Rectangle";}@Overridepublic void draw() {System.out.println("Inside Rectangle::draw() method.");}}
public class Square extends Shape {public Square(){type = "Square";}@Overridepublic void draw() {System.out.println("Inside Square::draw() method.");}}
public class Circle extends Shape {public Circle(){type = "Circle";}@Overridepublic void draw() {System.out.println("Inside Circle::draw() method.");}}
���� 3
����һ���࣬�����ݿ��ȡʵ���࣬�������Ǵ洢��һ�� Hashtable �С�
import java.util.Hashtable;public class ShapeCache {private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();public static Shape getShape(String shapeId) {Shape cachedShape = shapeMap.get(shapeId);return (Shape) cachedShape.clone();}// ��ÿ����״���������ݿ��ѯ������������״// shapeMap.put(shapeKey, shape);// ���磬����Ҫ����������״public static void loadCache() {Circle circle = new Circle();circle.setId("1");shapeMap.put(circle.getId(),circle);Square square = new Square();square.setId("2");shapeMap.put(square.getId(),square);Rectangle rectangle = new Rectangle();rectangle.setId("3");shapeMap.put(rectangle.getId(),rectangle);}}
���� 4
PrototypePatternDemo ʹ�� ShapeCache ������ȡ�洢�� Hashtable �е���״�Ŀ�¡��
public class PrototypePatternDemo {public static void main(String[] args) {ShapeCache.loadCache();Shape clonedShape = (Shape) ShapeCache.getShape("1");System.out.println("Shape : " + clonedShape.getType());Shape clonedShape2 = (Shape) ShapeCache.getShape("2");System.out.println("Shape : " + clonedShape2.getType());Shape clonedShape3 = (Shape) ShapeCache.getShape("3");System.out.println("Shape : " + clonedShape3.getType());}}
���� 5
ִ�г�����������
Shape : CircleShape : SquareShape : Rectangle
