Design Pattern with java (part three)
Specialized creation
-
PrototypeObjects are created by cloning a prototypical instance.
- 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) 評論(0) 編輯 收藏 所屬分類: java技術