Prototype Pattern

wangjh789·2022년 8월 31일
0

Design Pattern

목록 보기
4/13

프로토 타입 패턴은 생성된 객체가 자기 자신을 새롭게 복제하는 패턴을 의미한다.

public class Square extends Shape{

    public Square(int width, int height) {
        super(width, height);
    }

    @Override
    public void draw() {
        System.out.println("Square with width:" + this.width + " height:" + height);

    }

    @Override
    public Shape cloneObject() {
        return new Square(width, height);
    }
}
    public static void main(String[] args) {
        Shape square = new Square(10,5);
        square.draw();

        Shape squareClone = square.cloneObject();

        squareClone.draw();
    }

만약 객체를 생성하고 동등한 객체를 외부에서 생성하려고 하면 불필요한 의존성이 생길 수 있다.
그렇기 때문에 인스턴스에게 자신을 복사하는 능력(메서드)를 주는 것을 프로토타입 패턴 이라고 한다.

(아직까지 왜 쓰이는지 잘 와닿지 않음)

profile
기록

0개의 댓글