.
클래스이름을 지정하지 않고 인스턴스를 생성할 때.
- 종류가 너무 많아 클래스로 정리되지 않는 경우
- 클래스로부터 인스턴스 생성이 어려운 경우
- framework와 생성할 인스턴스를 분리하고 싶은 경우
.
package framework; public interface Product extends Cloneable { public abstract void use(String s); public abstract Product createClone(); }. . .
package framework; import java.util.HashMap; public class Manager { private HashMap<String, Product> showcase = new HashMap<String, Product>(); public void register(String name, Product proto) { showcase.put(name, proto); } public Product create(String protoname){ Product p = (Product) showcase.get(protoname); return p.createClone(); } }. . .
import framework.*; public class MessageBox implements Product{ private char decochar; public MessageBox(char decochar) { super(); this.decochar = decochar; } @Override public void use(String s) { int length = s.getBytes().length; for (int i=0; i<length+4;i++){ System.out.print(decochar); } System.out.println(""); System.out.println(decochar + " " + s + " " + decochar); for (int i=0; i<length+4;i++){ System.out.print(decochar); } System.out.println(" "); } @Override public Product createClone() { Product p = null; try { p = (Product) clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return p; } }. . .
import framework.Product; public class UnderlinePen implements Product{ private char ulchar; public UnderlinePen(char ulchar) { super(); this.ulchar = ulchar; } @Override public void use(String s) { int length = s.getBytes().length; System.out.println("\"" + s + "\""); System.out.print(" "); for(int i=0; i<length; i++){ System.out.print(ulchar); } System.out.println(""); } @Override public Product createClone() { Product p = null; try { p = (Product) clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return p; } }. . .
import framework.Manager; import framework.Product; public class Main { public static void main(String[] args) { //준비 Manager manager = new Manager(); UnderlinePen upen = new UnderlinePen('~'); MessageBox mbox = new MessageBox('*'); MessageBox sbox = new MessageBox('/'); manager.register("strong message", upen); manager.register("warning box", mbox); manager.register("slash box", sbox); //생성 Product p1 = manager.create("strong message"); p1.use("Hello, world."); Product p2 = manager.create("warning box"); p2.use("Hello, world."); Product p3 = manager.create("slash box"); p3.use("Hello, world."); } }. . .
"Hello, world." ~~~~~~~~~~~~~ ***************** * Hello, world. * ***************** ///////////////// / Hello, world. / /////////////////
.
Prototype 패턴의 등장인물
- Prototype(원형)의 역할
Prototype은 인스턴스를 복사하여 새로운 인스턴스를 만들기 위한 메소드를 결정.
Product 인터페이스
- ConcretePrototype(구체적인 원형)의 역할
ConcretePrototype은 인스턴스를 복사해서 새로운 인스턴스를 만드는 메소드를 실제로 구현.
MessageBox 클래스, UnderlinePen 클래스
- Client(이용자)의 역할
Client는 인스턴스를 복사하는 메소드를 이용해서 새로운 인스턴스를 생성.
Manager클래스
clone 메소드와 java.lang.Cloneable 인터페이스
- cloneable 인터페이스 : http://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html
- clone 메소드 : http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()
..
'Patterns' 카테고리의 다른 글
Template Method - 하위 클래스에서 구체적으로 처리하기 (0) | 2013.05.06 |
---|---|
Abstract Factory - 관련 부품을 조합해서 제품만들기 (0) | 2013.05.06 |
Singleton - 인스턴스를 한 개만 만들기 (0) | 2013.05.04 |
02. 객체지향 설계 원칙 (0) | 2013.05.04 |
01. 객체-지향(Object-Oriented) 개념 (0) | 2013.05.04 |