Java design Mode _ (Create) _ Builder mode __java

Source: Internet
Author: User
Quote Encyclopedia
The builder pattern is a form of design that separates the construction of a complex object from its representation, allowing the same build process to create different representations.

Practical Range
1 The algorithms for creating complex objects should be independent of the components of the object and the way they are assembled.
2 when the construction process must allow the constructed object to have a different representation.

Role

In such a design pattern, there are several roles:
1 Builder: Specifies an abstract interface for each part that creates a product object.
2 ConcreteBuilder: Implements the builder interface to construct and assemble the parts of the product, define and identify the representations it creates, and provide an interface to retrieve the product.
3 Director: Constructs an object that uses the builder interface.

4 Product: Represents a constructed complex object. ConcreteBuilder creates an internal representation of the product and defines its assembly process, containing the classes that define the components, including the interfaces that assemble the parts into the final product.


such as : The client has a demand is to assemble a computer computer, the customer's final request is to deliver a assembled computer, he/she does not care about the specific assembly process and required components, but the computer assembly relies on a lot of components, such as CPU, motherboard, graphics, memory 、、、 And so on some column components, but also need an assembly process, what to do first, and then what to install, and then use the builder model to achieve;

Here we only use CPU and motherboard board to create the implementation of the computer, to create a model to achieve the computer assembly;


The relationship is implemented as follows:



specific code examples are as follows:

1. Define Builder Abstract Interface

Abstract Builder (Builder) Role: Public
interface Builder {
	//Create CPU component public
	void Createcpu ();
	Create a motherboard component public
	void Createboard ();
	//.....
	Assemble computer public
	Computer buildcomputer ();
}


2, the definition of specific creation implementation

Specific builder (computerbuilder) role public
class Computerbuilder implements Builder {
	private Computer Computer;
	
	Public Computerbuilder () {
		computer = new computer ();
	}

	@Override public
	void Createcpu () {
		//TODO auto-generated method stub
		CPU CPU = new CPU (1, "Intel 6700");
		COMPUTER.SETCPU (CPU);

	@Override public
	void Createboard () {
		Board Board = new Board (1, "Lenovo");
		Computer.setboard (board);
	}

	@Override public
	Computer Buildcomputer () {return
		Computer;
	}
}


3. Define Director Directer role management creation

Director (Director) role public
class Computerdirector {public
	 Computer createcomputer (Builder bud) {  
		 Bud.createcpu ();
		 Bud.createboard ();
         return Bud.buildcomputer ();  
    }  


4. Related Business entity class

public class Computer {//CPU private CPU CPU;

	Motherboard private Board Board;
	Public CPU Getcpu () {return CPU;
	public void Setcpu (CPU CPU) {THIS.CPU = CPU;
	Public Board Getboard () {return Board;
	public void Setboard (Board Board) {this.board = Board;
	} public class Cpu {private int id;

	private String name;
		public Cpu (int ID, String name) {super ();
		This.id = ID;
	THIS.name = name;
	public int getId () {return id;
	The public void setId (int id) {this.id = ID;
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name;
	} public class Board {private int id;

	private String name;
		public Board (int ID, String name) {super ();
		This.id = ID;
	THIS.name = name;
	public int getId () {return id;
	The public void setId (int id) {this.id = ID;
	Public String GetName () {return name;
	public void SetName (String name) {this.name = name; }
}


5, client-side test implementation

public class Client {

	private static void Build () {
		computerdirector cd = new Computerdirector ();
		Computer Computer = Cd.createcomputer (New Computerbuilder ());
		System.out.println (Computer.getcpu (). GetName ());
		System.out.println (Computer.getboard (). GetName ());

	public static void Main (string[] args) {build
		();
	}
}

Run test code, output

Intel 6700
Association


Reference Description:

The client is responsible for creating the director and the concrete Builder object. Then, the client hands the specific builder object to the director, the director operates the concrete builder, and begins to create the product. When the product is finished, the builder returns the product to the client.

The Director role is the role of dealing with clients. The director divides the client's request to create the product into a construction request for each part, and then delegates the requests to the specific builder role. The specific builder's role is to do concrete construction work, but is not known to the client.

Generally speaking, there is a specific builder class for each product category. These products should have the same number of parts, and each one has a corresponding construction method in all the builders ' roles.

The task of creating a concrete builder object is given to the client rather than the Director object, in order to make the coupling of the Director object and the Concrete builder object dynamic, so that the Director object can manipulate any of the concrete builder objects.

 











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.