ԭ��ģʽ

ԭ��ģʽ��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 ����

05原型模式 - 图1

���� 1

����һ��ʵ���� Cloneable �ӿڵij����ࡣ

  1. public abstract class Shape implements Cloneable {
  2. private String id;
  3. protected String type;
  4. abstract void draw();
  5. public String getType(){
  6. return type;
  7. }
  8. public String getId() {
  9. return id;
  10. }
  11. public void setId(String id) {
  12. this.id = id;
  13. }
  14. public Object clone() {
  15. Object clone = null;
  16. try {
  17. clone = super.clone();
  18. } catch (CloneNotSupportedException e) {
  19. e.printStackTrace();
  20. }
  21. return clone;
  22. }
  23. }

���� 2

������չ������������ʵ���ࡣ

  1. public class Rectangle extends Shape {
  2. public Rectangle(){
  3. type = "Rectangle";
  4. }
  5. @Override
  6. public void draw() {
  7. System.out.println("Inside Rectangle::draw() method.");
  8. }
  9. }
  1. public class Square extends Shape {
  2. public Square(){
  3. type = "Square";
  4. }
  5. @Override
  6. public void draw() {
  7. System.out.println("Inside Square::draw() method.");
  8. }
  9. }
  1. public class Circle extends Shape {
  2. public Circle(){
  3. type = "Circle";
  4. }
  5. @Override
  6. public void draw() {
  7. System.out.println("Inside Circle::draw() method.");
  8. }
  9. }

���� 3

����һ���࣬�����ݿ��ȡʵ���࣬�������Ǵ洢��һ�� Hashtable �С�

  1. import java.util.Hashtable;
  2. public class ShapeCache {
  3. private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();
  4. public static Shape getShape(String shapeId) {
  5. Shape cachedShape = shapeMap.get(shapeId);
  6. return (Shape) cachedShape.clone();
  7. }
  8. // ��ÿ����״���������ݿ��ѯ������������״
  9. // shapeMap.put(shapeKey, shape);
  10. // ���磬����Ҫ����������״
  11. public static void loadCache() {
  12. Circle circle = new Circle();
  13. circle.setId("1");
  14. shapeMap.put(circle.getId(),circle);
  15. Square square = new Square();
  16. square.setId("2");
  17. shapeMap.put(square.getId(),square);
  18. Rectangle rectangle = new Rectangle();
  19. rectangle.setId("3");
  20. shapeMap.put(rectangle.getId(),rectangle);
  21. }
  22. }

���� 4

PrototypePatternDemo ʹ�� ShapeCache ������ȡ�洢�� Hashtable �е���״�Ŀ�¡��

  1. public class PrototypePatternDemo {
  2. public static void main(String[] args) {
  3. ShapeCache.loadCache();
  4. Shape clonedShape = (Shape) ShapeCache.getShape("1");
  5. System.out.println("Shape : " + clonedShape.getType());
  6. Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
  7. System.out.println("Shape : " + clonedShape2.getType());
  8. Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
  9. System.out.println("Shape : " + clonedShape3.getType());
  10. }
  11. }

���� 5

ִ�г�����������

  1. Shape : Circle
  2. Shape : Square
  3. Shape : Rectangle