Creation-Builder Pattern (Builder)

1. Intention ;

  Separating the construction of a complex object from its representation allows the same construction process to create different representations.

 

2. Scenario description ;

  The "Save As" function of editing software is an embodiment of the generator mode. For example, with the Save As function of Word, you can choose to save the file in doc, docx, pdf, txt and other formats, but when changing the storage format of the document through the Save As function of Word, "File --> Save As" is used. The same the creation process. When you need to support a new type conversion for word, for example, adding a *.newtype type conversion, you only need to add a line of "*.newtype" in "Select Storage Type" in the "Save As" dialog box. Without knowing the specific storage format conversion process, the user only needs to obtain the "newtype" type of document product.

 

3. Generator pattern class diagram ;

  
  
   Role :

  • Builder: Specifies an abstract interface for creating each component of a Product object;
  • ConcreteBuilder: Implements the Builder interface to construct and assemble the various components of the product; define and specify the representation it creates; provide an interface for retrieving objects (such as the GetResult method);
  • Director: Constructs an object using the Builder interface;
  • Product: Represents a constructed complex object (ConcreteBuilder creates an internal representation of the product and defines its assembly process); contains classes that define the components, including the interface for assembling these components into the final product.

4. Collaboration ;

  • The user class creates the Director object and configures it with the Builder object it wants to use;
  • The Director object controls the product parts you want to generate, and passes the part generation instructions to the Builder;
  • Builder produces the components that make up the product and assembles the components into the product;
  • The user class gets the product from the builder class.

5. Applicability ;

  • When the algorithm responsible for creating an object should be independent of what that object is made of and how they are assembled.
  • When the construction process must allow different representations of the constructed object.

6. Code implementation ;

 

  6.1 Product class FormattedFile;

package com.crazysnail.builder;

/**
 * Formatted file, super class for doc, docx, txt, pdf and other types of files
 */
public class FormattedFile {
	private String title;
	private String font;
	private String paragraph; //Basic information of the file
	
	/*getter, setter method*/
}
 

 6.2 Builder class and its subclasses;

    FileFormatConverter class; 

/**
 *
 *File format conversion parent class, which provides a default implementation of the interface
 *
 */
public class FileFormatConverter {
	private FormattedFile file;
	public FileFormatConverter(){
		file = new FormattedFile();
	}	
	/*Convert Word text*/
	public void convertCharacter(){
	}
	
	/* Format the Word font */
	public void convertFont(){
	}
	
	/* Format Word paragraphs, headings, etc. */
	public void convertParagraph(){
	}
	
	public FormattedFile getFile(){
		return file;
	}
}
   The parent class FileFormatConverter class of all Builders is defined as a class when it needs to provide default implementations for some methods. At this time, its subclasses can omit the overloading of methods in some parent classes; when the parent class of Builder defines When it is an interface, each specific Builder must provide a personalized definition of each interface method; two implementations can be selected according to the actual situation.

 

  PdfFormatConverter class;  

public class PdfFormatConverter extends FileFormatConverter {
	private FormattedFile file;

	public PdfFormatConverter(){
		file = new FormattedFile();
	}
	@Override
	public void convertCharacter(){
		System.out.println("Convert characters in file to corresponding representations in pdf");
	}
	
	@Override
	public void convertFont(){
		System.out.println("Convert the font in file to the default font in pdf");
	}
	
	@Override
	public void convertParagraph(){
		System.out.println("Convert the paragraph format of file to paragraph format in pdf");
	}
	
	public FormattedFile getFile(){
		return file;
	}
}
   PdfFormatConverter, as the ConcreteBuilder class, provides the implementation of converting files into pdf files.

  TxtFormatConverter class; 

public class TxtFormatConverter extends FileFormatConverter {
	private FormattedFile file;
	public TxtFormatConverter(){
		file = new FormattedFile();
	}	
	@Override
	public void convertCharacter(){
		System.out.println("Convert characters in file to representation in txt");
	}
	
	@Override
	public void convertFont(){
		System.out.println("Convert characters in file to representation in txt");
	}
	
	/*Use the default implementation in the parent class for the convertParagraph method, that is, do not convert the paragraph format*/
	
	public FormattedFile getFile(){ //When defining this method, the return type can be defined as a subtype TxtFile of FormattedFile
		return file;
	}
}
   The TxtFormatConverter class, as a ConcreteBuilder, provides the implementation of converting files into txt format files, in which the convertParagraph method directly uses the default definition in the parent class.

  6.3 Director class;

 

public class UseFormatConverter {
	public FormattedFile convertFile(FileFormatConverter converter){
		//The component process of the product is called by the Director class
		converter.convertCharacter();
		converter.convertFont();
		converter.convertParagraph();
		
		FormattedFile file = converter.getFile();
                return file;
	}
}
    The Director class calls the relevant interface in the Builder to build the product and obtain the product.

  6.4 Class relationship description;

  As the Builder's FileFormatConverter class, it provides interfaces for external use of ConcreteBuilder, the user class processes the product through these interfaces, and the Builder provides the user class with the method getFile to obtain the product.

7. Summary;  7.1 Features of generator mode;
  • 它使你可以改变一个产品的内部表示——Director类调用Builder类提供的抽象接口来构造产品,Builder类隐藏了整个产品的表示和内部结构;当你需要添加产品的一种新的内部结构和表示时,只需定义一个新的ConcreteBuilder类即可。例如场景描述中需要添加对”newType“类型的支持时,只需添加一个FileFormatConverter的子类NewTypeFormatConverter。
  • 它将构造代码和表示代码分开——整个产品的构造过程是通过Director来控制的,但是产品如何构造是完全在Builder的子类中进行实现的,二者是分离开来的。Builder的具体实现可在不同的Director中共享。
  • 它使你对构造过程进行更精细的控制——产品的构造过程是在Director中一步一步构造的,并不是一下生成的。当构建完成时,Director类从Builder类中获取产品。
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326569057&siteId=291194637