Design model series (3) - BUILDER

zhaozj2021-02-16  51

Oh, friends, since I started writing "Design Mode Series Implemented by Java" article, I found that my understanding of design patterns is much better than the original, this may be because Java is used to implement design patterns more appropriate and easy. Causes of understanding. When writing these series, I will of course refer to the source code of other seniors on the Internet. The most helpful thing is that Design Patter in Java, it is very good, but unfortunately, the example, I still feel a bit complicated. According to my thoughts, since design patterns are so difficult, so when you start, for the design model, we just try to grab the most essential place in each model, and strive to make the code as easy as possible. As for waiting until the future, you understand the design mode correctly, and then to engage in complex code is not too late. Perhaps, it is because I have learned too bitterly, so I don't want everyone to go to me, so there is this series of articles. I hope that this design model series implemented with Java can really help everyone. Of course, I also hope that I can write all (huh, .. first add some strength).

Ok, let's go, let us take a look at today's protagonist ---- Builder, Chinese is also called generator. At the beginning I learned this design mode, I didn't understand what the factories and factory models were there, and I saw the source code achieved by many others, it seems to be realized in imitation of factories. There is no essential difference in Builder and Factory. In fact, since my understanding, since Builder and Factory assigns created mode, then their biggest work is to create a class object, at this point, not only the two modes, and other creation modes are the same. But as I said in "in-depth exploration of the Factory mode and the prototype mode", these patterns, functionality, only "similar to rather". Since this, that's good, let's take a look at the similarities between Builder and Factory in the functionality, and what is the difference in charm.

First, it is also the most important point, although Builder and Factory create products, the types of product created are totally different. Factory creates only a single product (single referring to it is a non-composite product), and the product created by Builder is a composite product, that is, the product itself is composed of other parts. For example, now you have to produce a car, assume it consisting of these three parts: glass, wheels, engine. For factory mode, he is returned after being created, can only be glass, or a wheel, or an engine. Anyway, he can't return a complete car to our customers, you have to get a complete car, and customers must do their own parts into a car. In this sense, the factories in the factory model are just acting as the role of the parts factory. How does Builder create a product? In Builder mode, it is generally unnecessary, and it is not charged to return a single component to customers. He returns to customers, just a car finished product that has already been fully assembled. For the production details of the automotive parts, the customer does not need, and it should not let them know. Writing this, I suddenly thought of the difference in assembling computers and brand computers. Although the computers were cheap, it was easy to change, but the performance did not guarantee. In addition, you must understand a lot of computers; for brand computers, expensive This is not to say, the key is that he is not flexible, but its performance can be well guaranteed (by the manufacturer), which is easy as we guarantee the quality of the components in Builder. In addition, for brand computers, customers do not need to understand how much computer assembly knowledge, you can take a computer home and start it. So, in the actual use, do you like to do a DIY family, or like stability and guaranteed quality? It seems that we compare our brand computers during this process we compiled. This also provides a direction for us to use these two design patterns: if the product you want to produce is composed of different parts, you'd better use the Builder mode, not Factory mode.

In addition, the difference in Builder and Factory is the problem of the product tree where they produce products. In this way, it may be a bit 拗. Specifically, in the factory model, we know that a factory can create multiple products, but a product created in a factory model will inherit the tree in the same inheritance with the products created in another plant. If you have seen my earliest write "Design Mode Series (1) -Factory" with Java, I will remember that I created a product called MacRam in CFActoryMac, and another created another in CFactoryWin. The product is called WinRam, it is clear that both products are on the same inherited tree. The reason why they will appear on the same inheritance tree is completely determined by the Factory mode itself. If you read the UML map of Factory, you should remember that you must have a Factory mode, you must have an Abstract Product product class, and the specific product is derived from it. Ok, I finished Factory, let's take a look at whether it will do this in Builder! In fact, in the Builder mode, we just encapsulate the interface of creating components in Abstract Builder, and what parts create, different actual Builders may produce a completely different parts, this will not have any problems, because I have said above, Builder just returns a finished product to the customer, and does not return to the specific components, which, of course, the components of the product will be arbitrarily changed as required. Let's take an example, if you want to create two things that are not interlaced now, for example, a person, it is only consisting of these parts: brain, body, limbs; the other is tree, Three parts composition: roots, leaves, stems. Ok, you have to create these two products with Builder mode, can you do it? Take a look at the code below: Interface abuilder {

Public void builderparta ();

Public void builderpartb ();

Public void builderpartc ();

}

Class CBuilderhuman Implements Abuilder {

Private human;

Public cbuilderhuman () {

Human = new human ();

}

Public void BuilderParta () {human.head = new head ()};

Public void builderpartb () {human.body = new body ()};

Public void builderpartc () (human.limb = new limb ()};

Public human getproduct () {

BuilderParta ();

Builderpartb ();

BuilderPartc ();

Return Human;

}

Class CBuildertree's code is similar, here is not written here.

Let's see the client code:

Public stacic void main (String [] args) {

CBUilderhuman Builderhuman = new cbuilderhuman ();

Cbuildertree buildrtree = new cbuildertree ();

Human man;

Tree tree;

MAN = builderman.getProduct ();

Tree = buildertree.getProduct ();

}

I saw the above code, I believe everyone has a clear understanding of the product tree of Factory and Builder. Although the Builder mode can create a product that is not phase-free, but we will not do this. More common use is that parts products are on the same inheritance tree. The reason for this, everyone thinks about the object-oriented nature of it.

Ok, write this hand, I can't think of such a big article. Or that sentence, the code explains everything, then let us look at the code!

/ **

* Design Pattern in Java

* Name: Builder

* Objective: To create two car CARA and Carb with Builder mode

* Car = Glass Wheel Engine

* cara = americanglass japanesewheel chinaEngine

* Carb = JapaneseGlass AmericanWheel FranceEngine

* A: Abstract

* C: Concret

* Author: Blackphoenix

* Modify Date: 2002-08-19

* /

/ **

* Define the abstract class ACLASS for the part

* And two specific classes Americanglass, JapaneseGlass

* /

Abstract class aglass {

}

Class americanglass extends aglass {

Public string toString () {

Return "/" American Glass / ""

}

}

Class JapaneseGlass Extends Aglass {

Public string toString () {

Return "/" Japanese Glass / ";

}

}

/ **

* Define the abstract class of the part Wheel Awheel

* And two specific classes Americanwheel, Japanesewheel

* /

Abstract class awheel {

}

Class Americanwheel extends awheel {

Public string toString () {

Return "/" American wheelf / "";

}

}

Class Japanesewheel Extends awheel {

Public string toString () {

Return "/" Japanese Wheel / ";

}

}

/ **

* Define the abstraction of the part ENGINE AENGINE

* And two specific classes for chineseEngine, FranceEngine

* /

Abstract class angine {

}

Class chineseEngine extends angu {

Public string toString () {

Return "/" chinese engine / "";

}

}

Class FranceEngine Extends AEngine {

Public string toString () {

Return "/" france engine / ""

}

}

/ **

* Define product class car

* /

Class car {

Aglass Glass;

Awheel whee;

Angine Engine;

}

/ **

* Define abstract builder interface Abuilder

* /

Interface abuilder {

Public void buildglass ();

Public void buildwheel (); public void buildne ();

}

/ **

* Specific builder CBUildercara

* cara = americanglass japanesewheel chineseEngine

* /

Class CbuilderCara Implements Abuilder {

Private carproduct = NULL;

Public cbuildercara () {

Product = new car ();

}

Public void buildglass () {

Product.glass = new Americanglass ();

}

Public void buildingwheel () {

Product.wheel = new Japanesewheel ();

}

Public void buildengine () {

Product.EnGine = new chineseEngine ();

}

/ **

* Work packages of the construction part are mainly hidden to customers in getProduct ().

* In this way, the concrete construction class also plays a Director role.

* /

Public car getProduct () {

Buildglass ();

Buildwheel ();

Buildengine ();

Return Product;

}

}

/ **

* Specific builder CBUILDERCARB

* Carb = JapaneseGlass AmericanWheel FranceEngine

* /

Class CbuilderCarb IMPLEments Abuilder {

PRIVATE Car Product;

Public cbuildercarb () {

Product = new car ();

}

Public void buildglass () {

Product.glass = new japaneseseglass ();

}

Public void buildingwheel () {

Product.wheel = new Americanwheel ();

}

Public void buildengine () {

Product.Engine = new FranceEngine ();

}

/ **

* Work packages of the construction part are mainly hidden to customers in getProduct ().

* In this way, the concrete construction class also plays a Director role.

* /

Public car getProduct () {

Buildglass ();

Buildwheel ();

Buildengine ();

Return Product;

}

}

/ **

* Client code, create two different types of CARA and CARB using Builder

* /

Public class builder {

Public static void main (String [] args) {

Builder client = new builder ();

Car cara, carb;

Cbuildercara buildra;

CbuilderCarb Builderb;

Buildera = new cbuildercara ();

Builderb = new cbuildercarb ();

CARA = Buildra.getProduct ();

Carb = builderb.getProduct ();

System.out.println ("Car a is name by:" cara.glass cara.wheel caa.eEngine;

System.out.println ("Car B Is Made by: Carb.glass Carb.wheel Carb.Engine);

}

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

New Post(0)