<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ù)

    主站蜘蛛池模板: 中文字幕在线免费| xxxxx做受大片视频免费| 日韩一区二区三区免费播放| 国外亚洲成AV人片在线观看| 麻豆安全免费网址入口| 国产日本亚洲一区二区三区| 日韩免费观看视频| 污网站免费在线观看| 国产亚洲Av综合人人澡精品| 亚洲男人的天堂在线va拉文| 久久大香香蕉国产免费网站 | 久久夜色精品国产噜噜噜亚洲AV| 国产免费怕怕免费视频观看| 精品免费国产一区二区三区| 又色又污又黄无遮挡的免费视 | 久久久久久成人毛片免费看| 中国好声音第二季免费播放| 中文字幕免费观看视频| 精品四虎免费观看国产高清午夜| 久9热免费精品视频在线观看| 精品无码人妻一区二区免费蜜桃 | 亚洲AV永久无码精品网站在线观看| 国产国拍亚洲精品mv在线观看 | 女人裸身j部免费视频无遮挡| aa级女人大片喷水视频免费| 久久永久免费人妻精品下载 | 亚洲久热无码av中文字幕| 4444亚洲国产成人精品| 亚洲国产精品成人精品无码区| 亚洲欧洲精品久久| 久久久久国产精品免费网站| 久久福利资源网站免费看| 国产日韩一区二区三免费高清| 一级特黄a免费大片| 99爱视频99爱在线观看免费| 在线免费观看韩国a视频| 久久精品国产亚洲香蕉| 亚洲第一se情网站| 91香蕉成人免费网站| 亚洲午夜福利AV一区二区无码| 亚洲男人天堂2022|