Java实现(05)——原型模式(Prototype Pattern)
2016-12-06 10:55
281 查看
原型模式通过给出一个原型对象来指明所创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象。
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;
}
}
输出:
Shape: Circle
Shape: Square
Shape: Rectangle
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 void draw() { System.out.println("Inside Rectangle::draw() method."); } }
public class Square extends Shape { public Square() { type = "Square"; } @Override void draw() { System.out.println("Inside Square::draw() method."); } }
public class Circle extends Shape { public Circle() { type = "Circle"; } @Override void draw() { System.out.println("Inside Circle::draw() method."); } }
import java.util.Hashtable; /** * Created by wang on 16-12-6. */ 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() { 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); } }
public class Demo { 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()); } }
输出:
Shape: Circle
Shape: Square
Shape: Rectangle
相关文章推荐
- Object.clone()方法引申出的浅克隆、深克隆、java原型模式的实现、不可变类的实现
- Java设计模式菜鸟系列(十六)原型模式建模与实现
- 设计模式(Design Patterns)-可复用面向对象软件的基础 05:原型模式(Prototype)和Java中的深、浅拷贝
- java 23种设计模式 05 原型模式
- Java与设计模式(05)--原型模式
- 原型模式的java实现例子
- java设计模式之原型模式,如何实现深克隆
- 设计模式(Design Pattern) - 创建型模式(Creational Pattern) - 原型模式(Prototype) - Java实现
- 原型模式的java实现例子
- JAVA设计模式(05):创建型-原型模式(Prototype)
- JAVA设计模式(05):创建型-原型模式(Prototype)
- 设计模式之第9章-原型模式(Java实现)
- Java 实现原型(Prototype)模式
- (Prototype)原型模式的Java实现 推荐
- Java 实现原型(Prototype)模式
- Java常用的设计模式05:常用设计模式之原型模式(创建型模式)
- JAVA 原型模式的实现
- Rhyme/Java 设计模式之原型模式prototype通过clone方法和反序列化实现深复制
- 《大话设计模式》java实现之原型模式
- php,java实现原型模式