-
생성패턴 : Prototype 패턴디자인패턴 2013. 1. 15. 09:30
분류 : 생성패턴(Creational Patterns)
정의 :
새 객체를 생성하기위해 복사되는 프로토 타입형의 인스턴스를 통해서 객체를 생성하는 패턴.
용도 :
- 클라이언트 어플리케이션에서 객체의 생성자를 서브클래스화 하는 것을 방지
- 새로운 객체 생성 비용을 줄임.
코드
/** * Prototype class */ public interface Prototype { void setX(int x); void printX(); int getX(); } public class PrototypeImpl implements Prototype, Cloneable { private int x; /** * 생성자 */ public PrototypeImpl(int x){ setX(x); } @Override public void setX(int x) { this.x = x; } @Override public void printX() { System.out.println("Value: " + x); } @Override public int getX() { return x; } @Override public PrototypeImpl clone() throws CloneNotSupportedException{ return (PrototypeImpl) super.clone(); } } public class PrototypeTest { public static void main(String args[]) throws CloneNotSupportedException{ PrototypeImpl prototype = new PrototypeImpl(1000); for (int y = 1; y < 10; y++) { // 프로토타입형 객체 prototype를 이용해서 tempotype 객체를 생성함. Prototype tempotype = prototype.clone(); // Derive a new value from the prototype's "x" value tempotype.setX(tempotype.getX() * y); tempotype.printX(); } } }
참고자료
http://en.wikipedia.org/wiki/Prototype_pattern
http://ko.wikipedia.org/wiki/%ED%94%84%EB%A1%9C%ED%86%A0%ED%83%80%EC%9E%85_%ED%8C%A8%ED%84%B4
'디자인패턴' 카테고리의 다른 글
구조패턴 : Adapter (Wrapper) 패턴 (0) 2013.01.21 생성패턴 : Singleton 패턴 (0) 2013.01.16 생성패턴 : Factory Method 패턴 (0) 2013.01.10 생성패턴 : Builder 패턴 (0) 2012.12.28 생성패턴 : Abstract Factory 패턴 (0) 2012.12.18 댓글