A case study of building of Builder model

  • 2020-04-01 01:24:11
  • OfStack

The build pattern is primarily used to produce for complex products, separating component build details for good scalability.
Considering that design patterns come from architecture, let's take an example of building a house. Now a client is going to build a House, a House,
 
public class House{ 
//The client needs the house
} 

So he first need a Designer, Designer, but stylist can only do design, instructions on how to build a house, but he will not do in person, then you will also need a construction team BuildTeam, so first of all, the Designer to design it out how to build the house, must first construction group's foundation, then construction group to frame frame, then on the cement and so on (exactly how is not clear, need to consult professionals), then we can know from this Designer for construction group is a requirement, that is construction group must play ground, such as cement skeleton, meeting, Therefore, the following requirements are obtained for the recruitment of construction teams:
 
public interface BuildTeam{ 
public void  The base (); 
public void  Skeleton frame (); 
public void  On the cement (); 
 .  
} 

It can be seen from the above, to do this project construction team, must first sign the above conditions, will do all the above things. According to the designer's design, it is also known that the designer will issue an order to the construction team, and then the construction team will start the construction according to the designer's requirements:
 
public class Designer{ 
public void construct(BuildTeam team){ 
team. The base (); 
team. Skeleton frame (); 
team. On the cement (); 
} 
} 

Because from beginning to end is stylist at the next instruction design, and construction of actual construction, so customers will eventually find construction acceptance of the house, so house construction group must deliver to the customer, so construction group need to add a delivery terms of the house, or into a house, but the construction is not delivered to me, that's not a bad, so:
 
public interface BuildTeam{ 
public void  The base (); 
public void  Skeleton frame (); 
public void  On the cement (); 
 .  
public House deliverHouse(); //Add a method to deliver the house.
} 

Well, the house is designed, the way to do it is also designed, now it is left to who to do it, now there is a construction team:
 
public class BuildTeamA extends BuildTeam{ 
public void  The base (){} 
public void  Skeleton frame (){} 
public void  On the cement (){} 
 .  
public House deliverHouse(){} 
} 

From the situation of the construction team, this construction team fully meets the requirements of the designer for the construction team, which is the interface of BuildTeam. Ok, then they will make the final decision. The whole process from the beginning to the end is as follows:
Designer Designer = new Designer(); // find a designer
BuildTeam teamA = new BuildTeamA(); / / find a construction team to BuildTeamA
Designer. The construct (teamA); // the designer gave orders for the construction team to start building according to his design
House, House. = teamA deliverHouse (); // the construction team will deliver the house upon completion
Finally built the first house, at this point the same customers think the designer's design is good, then decided to use his design and directed by his construction group reengineering a same house, but construction group BuildTeamA lion big openings, suddenly want to have more money, the customer in order to save costs, once again have to recruit a new construction group construction, a construction team just BuildTeamB accords with a requirement, so the process is as follows:
 
BuildTeam teamB = new BuildTeamB(); 
designer.construct(teamB); //Since the designer has not changed and is building the same house, the designer does not need to find a new house, but just needs to replace the construction team he instructed with BuildTeamB
House house = teamB.deliverHouse(); //The house will be delivered when the construction team is finished

Well, the second house is finished, but is not much change in the whole process, due to the use of the building pattern, the entire process division of labor is very clear, the customer does not need to participate in any design and construction, the designer only responsible for design and orders, and construction group is responsible for the implementation of the specific details, only made detailed independence, replacement of different construction group all can at any time.

Related articles: