Translation Tipatterns - Specialization Creatment

xiaoxiao2021-03-06  183

Specialize Creatment

Prototype

Create an object by cloning an example of a prototype. "Pattern Refactoring" chapter gives an example of this mode.

The purpose of the Builder builder mode is to separate the structure of the object with its "representation", so that the object can have multiple different "representations". The process of object construct remains unchanged, but the finally created object can have different representations. GOF pointed out that the main difference between the Abstract Factory mode and the Builder mode is that the Builder mode follows a certain step by step to create an object, so that it is very important to create an object in time order. In addition, "Director" seems to be in Addition, It See That the "Director" gets a stream of pieces That it passes to the builder, and each pieser is buy to perform process. Gof gives one The example is a text format converter. The text to be converted is RTF format. When the text is parsed, the parsing instruction is transmitted to the text converter, and the text converter may have different implementations depending on the different output formats (ASCII, Tex, or "GUI Text Tools"). Despite the final "object" (the entire converted file) is Over Time? ? ? However, if the conversion instructions for each RTF format are considered an object, I think this is more like a Bridge mode because a specific type of converter extends the interface of the base class. In addition, this problem usual solution allows front end to have multiple readers, while the converter operates in the back end, which is the main feature of the Bridge mode. In my opinion, Builder and General Factory's most essentially, Builder uses multiple steps to create objects, and these steps are external to Builder objects. However, GOF emphasizes that you can create different object entities with the same steps. They never explain what "representation" does nothing. (Is "REPRESENTATION" refers to an excessive object? So, if the object entity is split into a small object, don't you need to use Builder?)

GOF also gives another example, create a labyrinth object, add a room to the inside of the maze, add the door to the room. This is a process that requires multiple completions, but the so-called "rerestations", "Standard" and "Complex)" labyrinth - actually not It is different types of maze, but it is different from their complexity. If you change it is me, I think I will try to create a maze builder (Maze Builder) uses it to create a maze of any complexity. The final variant of Maze Builder did not create a maze object at all, it just calculated the number of rooms included in the existing maze.

Both examples of the RFT converter and maze constructors are not a very persuverable example of the Builder mode. Some readers suggested that SAX's XML parser, or standard compiler parsers may be more suitable for the Builder mode. The following example may be more suitable for the Builder mode, at least it will more describe the intent of the Builder mode. We may create media (Media) as different forms, this example may be books, magazines, or websites. This example mainly shows the steps of creating an object so that they can be abstract into the Director class.

//: BuildMedia.java

// EXAMPLE of the Builder Pattern

Package builder;

Import java.util. *;

Import junit.framework. *;

// Different "Repesentations" 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 ("Add 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" articles;

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 ();

Iterator it = INPUT.ITERATOR (); it.hasnext ();)

Mb.addmediaITEM (MediaITEM) IT.Next ());

Return mb.getfinished;

}

}

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

}

} ///: ~

Notice that from a certain point of view, the above example can also be seen as a complicated State mode because Director's behavior depends on the specific type of Builder you use. Director is not a simple STATE object that will be passed to the next layer, but uses the State object as a policy object, and finally to complete a series of operations. For this, the Builder mode can be described in order to create an object using a certain policy.

Exercise: 1. Taking a text file as a series of words read (An Input Stream of Words), consider using regular expressions. Write a Builder to read these words into a java.util.treeset, then write a Builder to create a java.util.hashmap used to save the words and the number of times it appears (that is, the number of statistical words).

table of Contents

转载请注明原文地址:https://www.9cbs.com/read-128731.html

New Post(0)