Design Pattern-builder pattern-JAVA Implementation

Source: Internet
Author: User

Builder mode, which separates the internal appearance of a product from the product generation process, so that a building process has different internal

The product object of the representation. The construction mode allows the product's internal appearance to change independently, so that the customer does not have to know the details of the product's internal composition. The construction model enforces a step-by-step construction process.

Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

Related models: Ideas andTemplate Method ModeLike this, the template method encapsulates algorithm flows and provides interfaces for certain details to be modified by sub-classes. The Builder mode is more advanced and all details are handed over to sub-classes for implementation.

Builder mode usage:

The builder mode should be used in the following situations:

1. The product objects to be generated have a complex internal structure.

2. The properties of the product objects to be generated are mutually dependent. The Builder mode can force the generation order.

3. Some other objects in the system will be used during object creation. These objects are not easy to obtain during product object creation.

Advantages and disadvantages:

Advantages: 1. the builder mode allows independent changes to the internal appearance of the product, so that the client does not have to know the internal composition details of the product.

2. Each builder is relatively independent of other builders.

3. The construction process can be controlled more precisely to reduce the risk of details.

4. Separate build code from presentation code.

Disadvantages: 1. Because the change point lies in the details of object creation, it is difficult to change the algorithm requirements for step-by-step construction, because it focuses on the object creation sequence.

The builder mode is generally applicable to stable systems. For example, it is also a description of people. If a normal person is described, it is available, but for the disabled, because the system is no longer stable, if it is used again, it will not be able to meet the "open and closed principle.

When a product to be produced has the same structure and the production of each component is complicated, you can use the builder mode to separate the production of specific components from the assembly of the entire finished product. Take the code in this article as an example. the manufacturer of a notebook or notebook does not need to know how the CPU is produced or how the memory is produced, you do not need to worry about how the motherboard is produced. When he is producing a notebook, he will only say that I want an Intel CPU, so I have a CPU (as for how Intel's CPU is produced, he doesn't care, he only needs a CPU), and he says that I want a Kingston memory, so I have Kingston memory until he gets all the components, and then he assembles them, make up a notebook and sell it to the customer. This is a typical builder mode. The following code is used:


/*** CPU abstract class */public abstract class CPU {public abstract CPU getcpu ();}

/*** Intel CPU */public class intelcpu extends CPU {public intelcpu () {}; @ overridepublic CPU getcpu () {// todo auto-generated method stubreturn new intelcpu ();} Public String tostring () {return "intelcpu ";}}

/*** Amd cpu */public class amdcpu extends CPU {public amdcpu () {}; @ overridepublic CPU getcpu () {// todo auto-generated method stubreturn new amdcpu ();} Public String tostring () {return "amdcpu ";}}

/*** Memory abstract class */public abstract class memory {public abstract memory getmemory ();}

/*** Kingston memory */public class kingstonmemory extends memory {public kingstonmemory () {}; @ overridepublic memory getmemory () {return New kingstonmemory ();} Public String tostring () {return "kingstonmemory ";}}

/*** Yuzhan memory */public class apacermemory extends memory {public apacermemory () {}; @ overridepublic memory getmemory () {// todo auto-generated method stubreturn new apacermemory ();} Public String tostring () {return "apacermemory ";}}

* ** Main board abstract class */public abstract class mainboard {public abstract mainboard getmainboard ();}

/*** Asus motherboard */public class asusmainboard extends mainboard {public asusmainboard () {}; @ overridepublic mainboard getmainboard () {// todo auto-generated method stubreturn new asusmainboard ();} Public String tostring () {return "asusmainboard ";}}

/*** Gigabyte motherboard */public class gamainboard extends mainboard {public gamainboard () {}@ overridepublic mainboard getmainboard () {return New gamainboard () ;}; Public String tostring () {return "gamainboard ";}}

/*** Computer components */public class computer {private CPU; private memory; private mainboard; Public CPU getcpu () {return CPU ;} public void setcpu (CPU) {This. CPU = CPU;} public memory getmemory () {return memory;} public void setmemory (Memory) {This. memory = memory;} public mainboard getmainboard () {return mainboard;} public void setmainboard (mainboard) {This. mainboard = mainboard ;}}

/*** Computer builder */public interface computerbuilder {public void buildcpu (); Public void buildmemory (); Public void buildmainboard (); Public Computer getcomputer ();}

/*** Lenovo computer builder */public class lenovecomputerbuilder implements computerbuilder {private computer lenovecomputer = NULL; Public lenovecomputerbuilder () {lenovecomputer = new computer () ;}@ overridepublic void buildcpu () {lenovecomputer. setcpu (New intelcpu () ;}@ overridepublic void buildmemory () {lenovecomputer. setmemory (New kingstonmemory () ;}@ overridepublic void buildmainboard () {lenovecomputer. setmainboard (New asusmainboard () ;}@ overridepublic computer getcomputer () {buildcpu (); buildmemory (); buildmainboard (); Return lenovecomputer ;}}

/*** HP computer builder */public class hpcomputerbuilder implements computerbuilder {private computer hpcomputer = NULL; Public hpcomputerbuilder () {hpcomputer = new computer () ;}@ overridepublic void buildcpu () {hpcomputer. setcpu (New amdcpu () ;}@ overridepublic void buildmemory () {hpcomputer. setmemory (New apacermemory () ;}@ overridepublic void buildmainboard () {hpcomputer. setmainboard (New gamainboard () ;}@ overridepublic computer getcomputer () {buildcpu (); buildmemory (); buildmainboard (); Return hpcomputer ;}}

public class Director { private ComputerBuilder builder;       public Director(ComputerBuilder builder) {           this.builder = builder;       }      public Computer construct() {           return builder.getComputer();      }   }

public class TestBuilder {/** * @param args */public static void main(String[] args) {Computer lenoveComputer,hpComputer;  ComputerBuilder lenoveComputerBuilder = new LenoveComputerBuilder();  ComputerBuilder hpComputerBuilder = new HPComputerBuilder();          Director director;          director = new Director(lenoveComputerBuilder);          lenoveComputer=director.construct();          director = new Director(hpComputerBuilder);          hpComputer=director.construct();          System.out.println("lenoveComputer is made by:"+lenoveComputer.getCpu()+lenoveComputer.getMemory()+lenoveComputer.getMainboard());          System.out.println("hpComputer is made by:"+hpComputer.getCpu()+hpComputer.getMemory()+hpComputer.getMainboard());  }}

Running result:

Lenovecomputer is made by: intelcpu kingstonmemory asusmainboard
Hpcomputer is made by: amdcpu apacermemory gamainboard

Note:

In the main function, director calls the getcomputer () method in builder. The getcomputer () method is actually the assembly process, and buildcpu () in getcomputer ();
Buildmemory (); buildmainboard (); is to buy components, and the specific production process of these components is placed in the class of these components. You can see that buildcpu () contains a new object, this is production. This achieves the separation between assembly and production components.

Finally, let's talk about the difference between the builder mode and the factory mode. The difference between builder and factory is the difference between assembly and production. Builder focuses on separating production of components and components, and factory focuses on optimizing the production process. The code in this article can also be reconstructed. For example, the new keyword is used in the buildcpu () function. In fact, the New Keyword can be changed to the factory class to allow the factory class to produce the CPU. In other words, the factory is not assembled and builder is assembled. When the factory is assembled, it becomes builder.

Coming soon, prototype mode!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.