<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 閱讀(554) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java技術(shù)

    主站蜘蛛池模板: 在线精品一卡乱码免费| 亚洲无码黄色网址| 看成年女人免费午夜视频| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 亚洲黄色免费网站| 免费黄色网址入口| av永久免费网站在线观看| 亚洲一区精彩视频| 中文亚洲AV片在线观看不卡 | 成年人免费的视频| 一区二区视频免费观看| 亚洲视频一区在线| 亚洲一区日韩高清中文字幕亚洲| 亚洲视频免费在线看| 一级毛片正片免费视频手机看| 老汉色老汉首页a亚洲| 免费v片在线观看品善网| 18未年禁止免费观看| 一级看片免费视频| 亚洲人成无码网站在线观看| 国产V亚洲V天堂无码久久久| 国产精品另类激情久久久免费| 99久久人妻精品免费一区| 鲁啊鲁在线视频免费播放| 亚洲无圣光一区二区| 国产啪亚洲国产精品无码| 在线免费观看污网站| 91精品手机国产免费| 一级人做人a爰免费视频| 亚洲综合激情五月色一区| 精品日韩亚洲AV无码 | 亚洲第一精品在线视频| 日韩亚洲精品福利| 成在人线AV无码免费| 免费在线视频你懂的| 久久久久国色av免费看| 久久精品无码免费不卡| 美女免费视频一区二区| 亚洲性色AV日韩在线观看| 亚洲人成影院午夜网站| 亚洲人成影院在线|