Builder Mode Builders (create type)

Source: Internet
Author: User
Tags abstract

Reference Documentation:

1. Design mode-the basis of reusable object-oriented software

2.http://blog.csdn.net/hguisu/article/details/7518060 (design mode (iii) Builder mode creator (created))

3.http://www.cnblogs.com/happyhippy/archive/2010/09/01/1814287.html (the Myth of Builder mode: to encapsulate the construction of complex objects is the builder mode.) )


Knowledge of builder theory can be obtained from the above three articles;

Here's a case for builder.

Let's take a look at the structure of builder:


Here is the case: Buy KFC, choose the corresponding package, return the corresponding food to the user.

Code implementation:

Client.java:
Package com.rick.designpattern.builder;

/**
 * Created by MyPC on 2017/6/9.
 *
/public class Client {public

    void buy (int type) {
        builder builder = null;
        if (1 = = type) {
            builder = new ConcreteBuilder1 ();
        } else if (2 = = type) {
            builder = new ConcreteBuilder2 (); 
  } else {
            builder = new ConcreteBuilder3 ();
        }
        Directorcashier directorcashier = new Directorcashier ();
        Directorcashier.buildfood (builder);
        Builder.getproducts ();
    }

    public static void Main (string[] args) {
        Client client = new client ();
        Client.buy (2);
        System.out.println ("-------------");
        Client.buy (1);
        System.out.println ("-------------");
        Client.buy (3);

    }

}
Directorcashier.java:
Package com.rick.designpattern.builder;

/** *
 Instructor: Cashier
 * <p>
 * Created by MyPC on 2017/6/9.
 *
/public class Directorcashier {

    /**
     * Cashier Restaurant employee returned food *
     /public
    void Buildfood (Builder builder) {
        Builder.buildpart1 ();
        Builder.buildpart2 ();
    }
}
Builder.java:
Package com.rick.designpattern.builder;

/**
 * Created by MyPC on 2017/6/9.
 *
/Public abstract class Builder {

    /**
     * Creates the first part of the product *
     /public
    void BuildPart1 () {

    }

    /**
     * Create product second part *
     /public
    void BuildPart2 () {

    }

    /**
     * Return product
    */Public abstract void GetProducts ();

}
Concretebuilder1.java:
Package com.rick.designpattern.builder;

/**
 * Concrete Builder class: Restaurant staff, return package is: Hamburg two + drinks one
 * <p>
 * Created by MyPC on 2017/6/9.
 */Public
class ConcreteBuilder1 extends Builder {
    private Product product = null;

    Public ConcreteBuilder1 () {
        this.product = new product ();
    }

    @Override public
    void BuildPart1 () {
        product.add ("Hamburger", "2");
    }

    @Override public
    void BuildPart2 () {
        product.add ("Drink", "1");
    }

    @Override public
    void GetProducts () {
        product.getproducts ();
    }
}
Concretebuilder2.java:
Package com.rick.designpattern.builder;

/**
 * Concrete Builder class: Restaurant staff, Hamburg 1 + drinks 2
 * <p>
 * Created by MyPC on 2017/6/9.
 */Public
class ConcreteBuilder2 extends Builder {
    private Product product = null;

    Public ConcreteBuilder2 () {
        this.product = new product ();
    }
    
    @Override public
    void BuildPart1 () {
        product.add ("Hamburger", "1");
    }

    @Override public
    void BuildPart2 () {
        product.add ("Drink", "2");
    }

    @Override public
    void GetProducts () {
        product.getproducts ();
    }
}
Concretebuilder3.java:
Package com.rick.designpattern.builder;

/**
 * Created by MyPC on 2017/6/9.
 */Public
class ConcreteBuilder3 extends Builder {
    private Product product = null;

    Public ConcreteBuilder3 () {
        this.product = new product ();
    }

    @Override public
    void BuildPart1 () {
        product.add ("Hamburger", "3");
    }

    @Override public
    void GetProducts () {
        product.getproducts ();
    }
}
Product.java:
Package com.rick.designpattern.builder;

Import java.util.*;

/**
 * Created by MyPC on 2017/6/9.
 *
/public class Product {
    private map<string, string> products = new hashmap<string, string> ();

    /**
     * Add specific product */public
    void Add (string name, String value) {
        products.put (name, value);
    }

    public void GetProducts () {
        set<string> keys = Products.keyset ();
        for (String Key:keys) {
            System.out.println (key + "=" + products.get (key));}}}




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.