<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    junhong

    Design Pattern with java (part three)

    Specialized creation


    1. Prototype
      Objects are created by cloning a prototypical instance.
    2. Builder
      The goal of builder is to separate the construction from the “representation,” to allow multiple
      different representations. The construction process stays the same, but the resulting object
      has different possible representations. GoF points out that the main difference with Abstract
      Factory is that a Builder creates the object step-by-step, so the fact that the creation process is
      spread out in time seems to be important. In addition, it seems that the “director” gets a
      stream of pieces that it passes to the Builder, and each piece is used to perform one of the
      steps in the build process.
      One example given in GoF is that of a text format converter. The incoming format is RTF, and
      once it is parsed the directives are passed to the text converter, which may be implemented in
      different ways depending on whether the resulting format is ASCII, TeX, or a “GUI Text
      Widget.” Although the resulting “object” (the entire converted text file) is created over time, if
      you consider the conversion of each RTF directive to be an object, this feels to me a little more
      like Bridge, because the specific types of converters extend the interface of the base class.
      Also, the general solution to the problem would allow multiple readers on the “front end” and
      multiple converters on the “back end,” which is a primary characteristic of Bridge.
      To me, the fact that Builder has multiple steps in creating an object, and those steps are
      accessed externally to the Builder object, is the essence of what distinguishes it (structurally,
      anyway) from a regular factory. However, GoF emphasizes that you’re able to create different
      representations using the same process. They never define exactly what they mean by
      representation. (Does the “representation” involve an object that is too large? Would the need
      for Builder vanish if the representation was broken into smaller objects?)
      The other example in GoF creates a maze object and adds rooms within the maze and doors
      within the rooms. Thus it is a multistep process, but alas, the different “representations” are
      the “Standard” and “Complex” mazes – not really different kinds of mazes, but instead
      different complexity. I think I would have tried to create one maze builder that could handle

      arbitrarily complex mazes. The final variation of the maze builder is something that doesn’t
      create mazes at all, but instead counts the rooms in an existing maze.
      Neither the RTF converter nor the Mazebuilder example makes an overwhelmingly
      compelling case for Builder. Readers have suggested that the output of the Sax XML parser,
      and standard compiler parsers, might naturally be fed into a Builder.
      Here’s an example that may be a little more compelling, or at least give more of an idea of
      what Builder is trying to do. Media may be constructed into different representations, in this
      case books, magazines and web sites. The example argues that the steps involved are the
      same, and so can be abstracted into the director class.
      //: builder:BuildMedia.java
      // Example of the Builder pattern
      package builder;
      import java.util.*;
      import junit.framework.*;
      // Different "representations" of media:
      class Media extends ArrayList {}
      class Book extends Media {}
      class Magazine extends Media {}
      class WebSite extends Media {}
      // ... contain different kinds of media items:
      class MediaItem {
      private String s;
      public MediaItem(String s) { this.s = s; }
      public String toString() { return s; }
      }
      class Chapter extends MediaItem {
      public Chapter(String s) { super(s); }
      }
      class Article extends MediaItem {
      public Article(String s) { super(s); }
      }
      class WebItem extends MediaItem {
      public WebItem(String s) { super(s); }
      }
      // ... but use the same basic construction steps:
      class MediaBuilder {
      public void buildBase() {}
      public void addMediaItem(MediaItem item) {}
      public Media getFinishedMedia() { return null; }
      }
      class BookBuilder extends MediaBuilder {
      private Book b;
      public void buildBase() {
      System.out.println("Building book framework");
      b = new Book();
      }
      public void addMediaItem(MediaItem chapter) {
      System.out.println("Adding chapter " + chapter);
      b.add(chapter);
      }
      public Media getFinishedMedia() { return b; }
      }
      class MagazineBuilder extends MediaBuilder {

      private Magazine m;
      public void buildBase() {
      System.out.println("Building magazine framework");
      m = new Magazine();
      }
      public void addMediaItem(MediaItem article) {
      System.out.println("Adding article " + article);
      m.add(article);
      }
      public Media getFinishedMedia() { return m; }
      }
      class WebSiteBuilder extends MediaBuilder {
      private WebSite w;
      public void buildBase() {
      System.out.println("Building web site framework");
      w = new WebSite();
      }
      public void addMediaItem(MediaItem webItem) {
      System.out.println("Adding web item " + webItem);
      w.add(webItem);
      }
      public Media getFinishedMedia() { return w; }
      }
      class MediaDirector { // a.k.a. "Context"
      private MediaBuilder mb;
      public MediaDirector(MediaBuilder mb) {
      this.mb = mb; // Strategy-ish
      }
      public Media produceMedia(List input) {
      mb.buildBase();
      for(Iterator it = input.iterator(); it.hasNext();)
      mb.addMediaItem((MediaItem)it.next());
      return mb.getFinishedMedia();
      }
      };
      public class BuildMedia extends TestCase {
      private List input = Arrays.asList(new MediaItem[] {
      new MediaItem("item1"), new MediaItem("item2"),
      new MediaItem("item3"), new MediaItem("item4"),
      });
      public void testBook() {
      MediaDirector buildBook =
      new MediaDirector(new BookBuilder());
      Media book = buildBook.produceMedia(input);
      String result = "book: " + book;
      System.out.println(result);
      assertEquals(result,
      "book: [item1, item2, item3, item4]");
      }
      public void testMagazine() {
      MediaDirector buildMagazine =
      new MediaDirector(new MagazineBuilder());
      Media magazine = buildMagazine.produceMedia(input);
      String result = "magazine: " + magazine;
      System.out.println(result);
      assertEquals(result,
      "magazine: [item1, item2, item3, item4]");
      }

      public void testWebSite() {
      MediaDirector buildWebSite =
      new MediaDirector(new WebSiteBuilder());
      Media webSite = buildWebSite.produceMedia(input);
      String result = "web site: " + webSite;
      System.out.println(result);
      assertEquals(result,
      "web site: [item1, item2, item3, item4]");
      }
      public static void main(String[] args) {
      junit.textui.TestRunner.run(BuildMedia.class);
      }
      } ///:~

    posted on 2006-04-10 23:51 junhong 閱讀(562) 評論(0)  編輯  收藏 所屬分類: java技術

    主站蜘蛛池模板: 亚洲精品久久久久无码AV片软件| 爱情岛亚洲论坛在线观看| aa在线免费观看| 全免费a级毛片免费看| 国产偷窥女洗浴在线观看亚洲| 亚洲精品少妇30p| 成人免费无码H在线观看不卡| 亚洲熟妇少妇任你躁在线观看无码| 久久精品国产亚洲AV麻豆~| 免费无码一区二区三区蜜桃| 日韩va亚洲va欧洲va国产| 亚洲aⅴ天堂av天堂无码麻豆| 中文在线免费视频| 亚洲AV无一区二区三区久久| 无码国产精品一区二区免费3p| 国产午夜免费秋霞影院| 又硬又粗又长又爽免费看| 亚洲无人区一区二区三区| 中文字幕免费在线看电影大全| 亚洲AV成人片色在线观看| 添bbb免费观看高清视频| 国产一级a毛一级a看免费人娇| 亚洲人成色77777| 99免费观看视频| 亚洲色大成网站www永久男同 | 免费在线观看一级毛片| 亚洲啪啪免费视频| 久久国产精品萌白酱免费| 亚洲国产综合人成综合网站00| 好吊妞998视频免费观看在线| 久久精品国产亚洲AV大全| 一级毛片不卡免费看老司机| 亚洲国产精品VA在线看黑人| 黄页网站免费在线观看| 日本精品久久久久久久久免费| 久久精品亚洲一区二区| 啦啦啦手机完整免费高清观看| 一级白嫩美女毛片免费| 亚洲女人18毛片水真多| 亚洲A丁香五香天堂网| 1a级毛片免费观看|