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
- Worker interface that defines the work done by each worker. They are responsible for the construction of specific components such as windows and floors.
- 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
- Public interface Builder {
- public void Makewindow ();
- public void Makefloor ();
- Public getroom ();
- }
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
- Designer. He knows how the house should be designed, but he does not build it himself, but directs the laborers to build it.
- Public class Designer {
- //directing migrant workers to work
- Public void order (Builder builder) {
- Builder.makewindow ();
- Builder.makefloor ();
- }
- }
3, migrant workers , he is responsible for the implementation of specific things.
Java code
- Migrant. Responsible for the construction of specific parts such as windows and floors.
- 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
- Public class Mingong implements Builder {
- private String window="";
- private String floor="";
- public void Makewindow () {
- window=New String ("window");
- }
- public void Makefloor () {
- floor=New String ("floor");
- }
- //House to homeowners
- Public Getroom () {
- if ((!window.equals (")) && (!floor.equals ( "))) {
- System.out.println ("ready!");
- return new Guest ();
- }
- else return null;
- }
- }
4, the landlord , is to hire people, to collect the house.
Java code
- 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.
- Public class Client {
- public static void Main (string[] args) {
- Builder Mingong = new Mingong ();
- Designer designer = new designer ();
- Designer.order (Mingong);
- Mingong.getroom ();
- }
- }
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)