Some personal understanding of the Builder (creator mode) of the design pattern

Source: Internet
Author: User

The builder model is simple, but has always been trying to figure out why it should be designed so that the builder wants product rather than the director who knows the construction process. Just now Google to an article, finally clear. Here's a richardluo metaphor.

Simply put, it is as if I want a house to live, but I do not know how to cover (simple walls, low level), do not know how to design (build several rooms, several doors look good, higher level), so I need to find a bunch of migrant workers, they will drywall, but also to find a designer, he knows how to design, I also want to make sure that the migrant workers listen to the leadership of the designers, and the designers themselves do not work, just under the order, here to build a wall, here a door, so that the migrant workers started construction, finally, I can to the migrant workers to the House. In this process, the designer is nothing, except his in the mind of the design and command, so the house is also with the migrant workers to, remember!

The following is the code for Richardluo, and I added the corresponding comments according to his ideas.


1, define the worker interface , is to be able to complete the construction of the House task of the general requirements of the people.

Java code
    1. Worker interface that defines the work done by each worker. They are responsible for the construction of specific components such as windows and floors.
    2. At the same time because the house is built by migrant workers, so after the completion of the building by him to return the house to the landlord
    3. Public interface Builder {
    4. public void Makewindow ();
    5. public void Makefloor ();
    6. Public getroom ();
    7. }


2, the definition of the designer, his duty is to direct the landlord assigned to his workers according to their own design intentions to build the house.

Java code
    1. Designer. He knows how the house should be designed, but he does not build it himself, but directs the laborers to build it.
    2. Public class Designer {
    3. //directing migrant workers to work
    4. Public void order (Builder builder) {
    5. Builder.makewindow ();
    6. Builder.makefloor ();
    7. }
    8. }


3, migrant workers , he is responsible for the implementation of specific things.

Java code
  1. Migrant. Responsible for the construction of specific parts such as windows and floors.
  2. At the same time because the house is built by migrant workers, so after the completion of the building by him to return the house to the landlord
  3. Public class Mingong implements Builder {
  4. private String window="";
  5. private String floor="";
  6. public void Makewindow () {
  7. window=New String ("window");
  8. }
  9. public void Makefloor () {
  10. floor=New String ("floor");
  11. }
  12. //House to homeowners
  13. Public Getroom () {
  14. if ((!window.equals (")) && (!floor.equals ( "))) {
  15. System.out.println ("ready!");
  16. return new Guest ();
  17. }
  18. else return null;
  19. }
  20. }


4, the landlord , is to hire people, to collect the house.

Java code
  1. Homeowners. The task of the homeowner is to hire a migrant worker, a designer, and instruct the migrant worker to work with the designer. Finally from the migrant workers to collect the house.
  2. Public class Client {
  3. public static void Main (string[] args) {
  4. Builder Mingong = new Mingong ();
  5. Designer designer = new designer ();
  6. Designer.order (Mingong);
  7. Mingong.getroom ();
  8. }
  9. }


Well, I think that's probably clear. I wonder what you think? Or there is a better explanation for the application, please enlighten me.

Some personal understanding of the Builder (creator mode) of the design pattern (GO)

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.