pattern2018. 7. 18. 18:47

Prototype pattern 


-performance를 염두하고 clone객체를 생성하는 것을 말함.

-역시나 괜찮은 creational pattern 중 하나.

-실제 객체를 생성하는 것이 어려운 경우 prototype interface는 현재 객체에 대한 clone을 만든다.

-일례로, 무거운 db operation이 후 객체가 생성되는 경우, 객체를 cache할 수 있으며 clone을 리턴하므로써, db operation에 대한 호출을 줄인다.

- Clonable interface를 Shape에서 구현한다. (override clone method ). 

문맥 상, prototype 중의 하나로 Clonable로 인식 가능.



Main 호출 부에서 loadCache를 호출하여 overhead가 있는 객체 생성을 미리하여 type지정 후,

HashMap에 저장하여  getShape으로 clone된 객체를 얻오는 것이 가능하다.


	ShapeCache.loadCache();
	Shape clonedShape1 = (Shape) ShapeCache.getShape("1");
	Log.d("hello","shape1 : "+clonedShape1.getType());
	clonedShape1.draw();

	Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
	Log.d("hello","shape2 : "+clonedShape2.getType());
	clonedShape2.draw();

	Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
	Log.d("hello","shape3 : "+clonedShape3.getType());
	clonedShape3.draw();



Cloneable interface를 구현하기 위해 clone method를 재정의.

Shape.java
public abstract class Shape implements Cloneable{
    protected final String TAG = Shape.class.getSimpleName();
    private String id;
    protected String type;
    public abstract void draw();

    public String getType(){
        return type;
    }
    public void setType(String id){
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    protected Object clone()  {
        Object clone = null;
        try {
            clone= super.clone();

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

        return clone;
    }
}



Shape 객체들의 type을 정해주고, HashMap에 저장해 둔다. (모든 method는 static임에 주의)

ShapeCache.java
public class ShapeCache {

    private static HashMap shapeMap = new HashMap();
    public static Shape getShape(String shapeId){
        Shape cacheShape = shapeMap.get(shapeId);
        return (Shape) cacheShape.clone();
    }

    //assume that each Shape need database update.
    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 Circle extends Shape {
    //private final String TAG = Circle.class.getSimpleName();

    public Circle(){
        this.type="Circle";
    }
    @Override
    public void draw() {
        Log.d(TAG, "Circle draw");
    }
}
public class Rectangle extends Shape {
    //private final String TAG = Rectangle.class.getSimpleName();

    public Rectangle(){
        this.type="Rectangle";
    }

    @Override
    public void draw() {
        Log.d(TAG, "Rectangle draw");
    }
}
public class Square extends Shape {
    //private final String TAG = Square.class.getSimpleName();

    public Square(){
        this.type="Shape";
    }
    @Override
    public void draw() {
        Log.d(TAG, "Square draw");
    }
}

결과:
07-18 18:35:18.816 29224 29224 D hello   : shape1 : Circle
07-18 18:35:18.816 29224 29224 D Shape   : Circle draw

07-18 18:35:18.816 29224 29224 D hello   : shape2 : Shape
07-18 18:35:18.816 29224 29224 D Shape   : Square draw

07-18 18:35:18.816 29224 29224 D hello   : shape3 : Rectangle
07-18 18:35:18.816 29224 29224 D Shape   : Rectangle draw

출처:


'pattern' 카테고리의 다른 글

Bridge pattern  (0) 2018.07.19
Adapter pattern  (0) 2018.07.19
Builder pattern  (0) 2018.07.18
Singleton pattern  (0) 2018.07.18
Abstract Factory Pattern  (0) 2018.07.18
Posted by easy16