Design Pattern C # language Description--builder model

Source: Internet
Author: User
Tags abstract class definition
Design design Pattern C # language Description-builder (builder) mode



* This article refers to "Java and the pattern" part of the content, suitable for the design pattern of beginners.



The build pattern is the object's creation pattern. The construction pattern separates the internal representation of a product from the product's generation process, thus enabling a build process to produce a product object with a different internal representation.



A product often has a different composition as part of a product, which is often called the internal representation of a product. Different products can have different internal appearances, that is, different parts. Using construction mode allows customers to not know what parts of the product they are producing, what the respective parts of each product are, how it was built, and how to make the product. The following is an example of an implementation.

In this sample system, the end products product product is only two parts, namely Part1 and Part2. The corresponding construction methods also have two: BuilderPart1 and builderPart2. You can see that this pattern involves four roles:

Abstract Builder (Builder): An abstract interface is given to standardize the construction of various components of a Product object.

Concrete Builder (Concrete Builder): In this role are some classes that are closely related to applications that create instances of the product under the application's invocation.

Director (Director): is the role of dealing with the client, the creation of the product requirements are divided into the construction of each part of the request, and then delegate these requests to specific builders.

Product: An object in construction.


Builder:
public interface Builder 


void BuildPart1 (); 
void BuildPart2 (); 
Product Retrieveresult (); 
}//End INTERFACE DEFINITION Builder 
ConcreteBuilder: 
public class Concretebuilder:builder 

Private product product; 
public void BuildPart1 () 

Build the ' the ' of the ' product ' 

public void BuildPart2 () 

Build the second part of the product 

Public Product Retrieveresult () 

return product; 

}//End CLASS DEFINITION concretebuilder 
Director: 
public class Director 

Private Builder Builder; 
public void Director () 



public void construct ()//Product construction method, which is responsible for calling each part construction method 

Builder=new ConcreteBuilder (); 
Builder.builderpart1 (); 
Builder.builderpart2 (); 
Builder.retrieveresult (); 


}//End CLASS DEFINITION Director 

Product: 

public interface Product 

}//End INTERFACE DEFINITION Product 





There is only one product class in the above system, and there is also a specific builder class. There are a number of product and builder types of the construction model as follows:




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.