The builder pattern for Java design patterns

Source: Internet
Author: User

I. Defining the backgroundThe original definition is that the construction of a complex product is separated from its representation, so that the same build process can create different representations. It is still not good to understand the simple literal meaning of the construction and the marking phase separation. The complex construction process is basically the same, when the representation of the time to separate it, can appear a variety of branches. For example, China Mobile's package type: The same is 58 yuan grade, there are two kinds of packages to choose from: the first: local free calls 100 minutes, traffic 500M, SMS: 20, local directional traffic 15G; the second type: local free calls 50 minutes, traffic 700M, SMS: 10, local traffic 5G; This kind of words will be displayed when there are two kinds of packages to choose from, provided to the customer is two kinds of representations, the construction process you do not need to understand. This makes the construction and presentation separate. Two. Structure diagram
in this flowchart, product products, sub-package A and B two inherit from the production category, a set of specific implementation to achieve a set of properties, B package of the specific implementation of the set of the properties of the B package. The builder creates the corresponding object. will be provided to us directly for use. Three. Source codeProduct class
<span style= "Font-family:courier new;font-size:18px;" >public class Mobileproduct {private string traffic; private string  call; private string SMS; private string directed traffic; public Stri ng get Traffic () {return traffic;} public void set traffic (String traffic) {this. traffic = flow;} Public String Get Call () {return call;} public void Set Call (String call) {this. call = call;} Public String Get SMS () {return SMS;} public void Set SMS (String SMS) {this. sms = SMS;} Public String get directional traffic () {return directed traffic;} public void set directed traffic (String directed traffic) {this. Directed traffic = directed traffic;}} </span>
Set 1
<span style= "Font-family:courier new;font-size:18px;" >public class One58 extends Mobileproduct {}</span>
Set 2
<span style= "Font-family:courier new;font-size:18px;" >public class Two58 extends mobileproduct{}</span>
Builders ' Bulider

Public interface Bulider {public void Bulid traffic ();p ublic void Bulid call ();p ublic void Bulid SMS ();p ublic void Bulid directed traffic ();p Ubli C mobileproduct getproduct ();}
implementation of package 1 construction
<span style= "Font-family:courier new;font-size:18px;" >public class One58impl implements Bulider{private mobileproduct product;p ublic One58impl () {  product = new ONE58 ( );} public void Bulid Traffic () {//TODO auto-generated method Stubproduct.set Traffic ("Package One: 500M");} public void Bulid Call () {//TODO auto-generated method Stubproduct.set Call ("Package one: 100 minutes");} public void Bulid SMS () {//TODO auto-generated method Stubproduct.set SMS ("Package One: 20");} public void Bulid directed traffic () {//TODO auto-generated method Stubproduct.set directed traffic ("Package One: 15G");} Public Mobileproduct getproduct () {//TODO auto-generated method Stubreturn product;} </span>

Implementation of package 2 construction
<span style= "Font-family:courier new;font-size:18px;" >public class Two58impl implements Bulider{private mobileproduct product;public Two58impl () {Product = new Two58 ();} public void Bulid Traffic () {//TODO auto-generated method Stubproduct.set Traffic ("set two: 700M");} public void Bulid Call () {//TODO auto-generated method Stubproduct.set Call ("set two: 50 minutes");} public void Bulid SMS () {//TODO auto-generated method Stubproduct.set SMS ("set two: 10");} public void Bulid directed traffic () {//TODO auto-generated method Stubproduct.set directed traffic ("set two: 5G");} Public Mobileproduct getproduct () {//TODO auto-generated method Stubreturn product;} </span>

Director Class
<span style= "Font-family:courier new;font-size:18px;" >public class Director {public mobileproduct getproduct (Bulider bulid) {Bulid.bulid directed traffic (); Bulid.bulid traffic (); Bulid.bulid SMS (); Bulid.bulid call (); return bulid.getproduct ();} </span>
Test code
<span style= "Font-family:courier new;font-size:18px;" >public class Test {public static void main (string[] args) {Director Director = new Director (); Bulider bulid = new One58impl (); Mobileproduct Product = director.getproduct (bulid); SYSTEM.OUT.PRINTLN ("Package One details:"); System.out.println (Product.get directional flow ()); System.out.println (product.get flow ()); SYSTEM.OUT.PRINTLN (Product.get SMS ()); System.out.println (Product.get call ()), bulid = new Two58impl ();p roduct = Director.getproduct (bulid); SYSTEM.OUT.PRINTLN ("Package II details:"); System.out.println (Product.get directional flow ()); System.out.println (product.get flow ()); SYSTEM.OUT.PRINTLN (Product.get SMS ()); System.out.println (Product.get call ());}} </span>
//Results
<span style= "Font-family:courier new;font-size:18px;" > Package One details: Package One: 15G Package one: 500M Package one: 20 Package One: 100-minute Package Two: set two, 5G Package II: 700M Package II: 10 Package II: 50 minutes </span>
Four. Summary

 1. A part is the builder interface, here is the definition of how to build the individual parts, that is, how each part function is realized, and how to assemble the parts into the product;

2. Another part is that Director,director is aware of how to assemble a product, which means that the Director is responsible for the overall build algorithm and is usually executed in a step-by.

No matter how it changes, there are two parts in the construction mode, one part is the component construction and the product assembly, the other part is the overall built algorithm . It is important to recognize this because in the construction model, the emphasis is on fixing the overall built algorithm, while flexibly extending and switching the specific construction of the part and the way the product is assembled.

To put it bluntly, the focus of the construction model is to separate the construction algorithm and the concrete construction implementation, so that the construction algorithm can be reused. The concrete construction implementation can be easily extended and switched, which can be flexibly combined to construct different product objects.

Bulider and director these two are fixed things. The rest are extensions of the business. If there is a third package in the demo above, it is also easy to expand. No need to modify anything else. This is the package.
























The builder pattern for Java design patterns

Related Article

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.