在这种情况下,原型模式的好处是什么?(what is the benefit of prototype pattern in this case)
我看到一些代码以这种方式定义了原型模式:
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; } }
扩展上述类的两个具体类:
public class Rectangle extends Shape { public Rectangle(){ type = "Rectangle"; } @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } public class Square extends Shape { public Square(){ type = "Square"; } @Override public void draw() { System.out.println("Inside Square::draw() method."); } }
创建一个类以从数据库获取具体类并将它们存储在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(); } public static void loadCache() { Square square = new Square(); square.setId("2"); shapeMap.put(square.getId(),square); Rectangle rectangle = new Rectangle(); rectangle.setId("3"); shapeMap.put(rectangle.getId(), rectangle); } }
我的问题是getShape方法这两个实现之间的区别和好处:
实施1:
public static Shape getShape(String shapeId) { Shape cachedShape = shapeMap.get(shapeId); return (Shape) cachedShape.clone(); }
并且: 实施2:
public static Shape getShape(String shapeId) { Shape cachedShape = shapeMap.get(shapeId); // return (Shape) cachedShape.clone(); return cachedShape ; }
我尝试了这两种实现,并且他们工作得很好,只是我想知道如果使用第一个实现的好处
I saw some code defines the prototype pattern in this way :
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; } }
Two concrete classes extending the above class :
public class Rectangle extends Shape { public Rectangle(){ type = "Rectangle"; } @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } public class Square extends Shape { public Square(){ type = "Square"; } @Override public void draw() { System.out.println("Inside Square::draw() method."); } }
Create a class to get concrete classes from database and store them in a 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(); } public static void loadCache() { Square square = new Square(); square.setId("2"); shapeMap.put(square.getId(),square); Rectangle rectangle = new Rectangle(); rectangle.setId("3"); shapeMap.put(rectangle.getId(), rectangle); } }
My question is in the getShape method what is the difference and benefit between these two implementations :
Implementation 1 :
public static Shape getShape(String shapeId) { Shape cachedShape = shapeMap.get(shapeId); return (Shape) cachedShape.clone(); }
And : Implementation 2 :
public static Shape getShape(String shapeId) { Shape cachedShape = shapeMap.get(shapeId); // return (Shape) cachedShape.clone(); return cachedShape ; }
I tried the two implementations and they work well just I want to know the benefit if I use the first Implementation
原文:https://stackoverflow.com/questions/40419778