최근 포토로그


2008/07/30 09:02

[DesignPattern - Prototype] sample1 study. note

Design Pattern - Prototype

1. 클래스다이어그램
2. Source
  • Product.java
    package com.dazzilove.prototype.sample1.framework;

    public interface Product extends Cloneable {
    public abstract void use(String s);
    public abstract Product createClone();
    }
  • Manager.java
    package com.dazzilove.prototype.sample1.framework;

    import java.util.Hashtable;

    public class Manager {
    private Hashtable showcase = new Hashtable();

    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();
    }
    }
  • MessageBox.java
    package com.dazzilove.prototype.sample1;

    import com.dazzilove.prototype.sample1.framework.Product;

    public class MessageBox implements Product {

    private char decoChar;

    public MessageBox(char decoChar) {
    this.decoChar = decoChar;
    }

    public Product createClone() {
    Product p = null;

    try {
    p = (Product) clone();
    } catch(CloneNotSupportedException e) {
    e.printStackTrace();
    }

    return p;
    }

    public void use(String s) {
    int length = s.getBytes().length + 2;

    for(int i=0; i System.out.print(decoChar);
    }
    System.out.println("");
    System.out.println(decoChar + s + decoChar);
    for(int i=0; i System.out.print(decoChar);
    }
    System.out.println("");
    }
    }
  • UnderlinePen.java
    package com.dazzilove.prototype.sample1;

    import com.dazzilove.prototype.sample1.framework.Product;

    public class UnderlinePen implements Product {
    private char underlineChar;

    public UnderlinePen(char underlineChar) {
    this.underlineChar = underlineChar;
    }

    public Product createClone() {
    Product p = null;

    try {
    p = (Product) clone();
    } catch(CloneNotSupportedException e) {
    e.printStackTrace();
    }

    return p;
    }

    public void use(String s) {
    int length = s.getBytes().length + 2;

    System.out.println("\"" + s + "\"");
    for(int i=0; i System.out.print(underlineChar);
    }
    System.out.println("");
    }
    }
  • Main.java
    package com.dazzilove.prototype.sample1;

    import com.dazzilove.prototype.sample1.framework.Manager;
    import com.dazzilove.prototype.sample1.framework.Product;

    public class Main {
    public static void main(String[] args) {
    //prepared
    Manager manager = new Manager();
    UnderlinePen uPen = new UnderlinePen('-');
    MessageBox mBox = new MessageBox('*');
    MessageBox sBox = new MessageBox('/');
    manager.register("strongMessage", uPen);
    manager.register("warningBox", mBox);
    manager.register("slashBox", sBox);

    //create
    Product p1 = manager.create("strongMessage");
    p1.use("Hellow, world.");
    Product p2 = manager.create("warningBox");
    p2.use("Hellow, world.");
    Product p3 = manager.create("slashBox");
    p3.use("Hellow, world.");
    }
    }
3. 결과출력

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://dazzilove.egloos.com/tb/641995 [도움말]

핑백

  • dazzilove : 디자인 패턴 공부 2008-07-30 09:22:55 #

    ... copy)clone은 복사만 할 뿐이고 생성자를 호출하지 않는다. 또한 인스턴스 생성시 특수한 초기화를 필요로 하는 클래스에서는 clone 메서드 내에 처리를 기술해야 한다. sample1 : "Java 언어로 배우는 디자인 패턴 입문" Chapter6. Prototype - 복사해서 인스턴스를 만든다. ... more

덧글

덧글 입력 영역



미투데이

문화꽃 키우기