Understand the builder pattern of java design pattern

  • 2020-05-05 11:12:45
  • OfStack

The builder pattern (Builder Pattern) is primarily used to "build a complex object in steps", where "steps" is a stable algorithm, and the parts of a complex object are constantly changing. Therefore, the builder pattern is primarily used to address requirements changes in the "object part." This allows for more fine-grained control over the process of object construction.


package com.shejimoshi.create.Builder;
/**
 *  Functionality: the intent is to separate the construction of a complex object from its representation so that the same build process can create different representations 
 *      Applicability: 
 *        When the algorithm for creating a complex object should be independent of the object's components and how they are assembled 
 *        When the construction process must allow for a different representation of the object being constructed 
 */
public abstract class PersonBuilder
{
  // The tools needed to draw a character 
  protected String tool1;
  protected String tool2;
  
  public PersonBuilder(String t1, String t2)
  {
    this.tool1 = t1;
    this.tool2 = t2;
  }
  
  // Painting head 
  public abstract void buildHead();
  // Draw the body 
  public abstract void buildBody();
  // Draw the left foot and the right foot 
  public abstract void buildArmLeft();
  public abstract void buildArmRight();
  // Draw left hand right hand 
  public abstract void buildLegLeft();
  public abstract void buildLegRight();
}

Our abstract generator subclasses corresponding to the actual usage type


package com.shejimoshi.create.Builder;


/**
 *  Functionality: the intent is to separate the construction of a complex object from its representation so that the same build process can create different representations 
 *      Applicability: 
 *        When the algorithm for creating a complex object should be independent of the object's components and how they are assembled 
 *        When the construction process must allow for a different representation of the object being constructed 
 */
public class PersonThinBuilder extends PersonBuilder
{

  public PersonThinBuilder(String t1, String t2)
  {
    super(t1, t2);
  }

  @Override
  public void buildHead()
  {
    System.out.println(" Draw the head of a thin man ");
  }

  @Override
  public void buildBody()
  {
    System.out.println(" Draw a thin body ");
  }

  @Override
  public void buildArmLeft()
  {
    System.out.println(" Draw the thin man's left arm ");
  }

  @Override
  public void buildArmRight()
  {
    System.out.println(" Draw the thin man's right arm ");
  }

  @Override
  public void buildLegLeft()
  {
    System.out.println(" Draw the thin left leg ");
  }

  @Override
  public void buildLegRight()
  {
    System.out.println(" Draw the thin man's right leg ");
  }

}


package com.shejimoshi.create.Builder;


/**
 *  Functionality: the intent is to separate the construction of a complex object from its representation so that the same build process can create different representations 
 *  Applicability: 
 *     When the algorithm for creating a complex object should be independent of the object's components and how they are assembled 
 *     When the construction process must allow for a different representation of the object being constructed 
 */
public class PersonFatBuilder extends PersonBuilder
{

  public PersonFatBuilder(String t1, String t2)
  {
    super(t1, t2);
  }

  @Override
  public void buildHead()
  {
    System.out.println(" Draw the head of a fat man ");
  }

  @Override
  public void buildBody()
  {
    System.out.println(" Draw the body of a fat man ");
  }

  @Override
  public void buildArmLeft()
  {
    System.out.println(" Draw the fat man's left arm ");
  }

  @Override
  public void buildArmRight()
  {
    System.out.println(" Draw the fat man's right arm ");
  }

  @Override
  public void buildLegLeft()
  {
    System.out.println(" Draw the fat man's left leg ");
  }

  @Override
  public void buildLegRight()
  {
    System.out.println(" Draw the fat man's right leg ");
  }

}

Our generator is handed over to the commander to implement the created action


package com.shejimoshi.create.Builder;


/**
 *  Function: creates a character commander 
 */
public class PersonDirector
{
  private PersonBuilder pb;
  
  // Pass the corresponding character to create the model 
  public PersonDirector(PersonBuilder pber)
  {
    this.pb = pber;
  }
  
  // Create a person 
  public void createPerson()
  {
    pb.buildHead();
    pb.buildBody();
    pb.buildArmLeft();
    pb.buildArmRight();
    pb.buildLegLeft();
    pb.buildLegRight();
  }
}

test case:


package com.shejimoshi.create.Builder;


/**
 *  Function: client program 
 */
public class Test
{
  // Create the appropriate personas 
  public static void create(PersonBuilder pb)
  {
    // Create the corresponding object with the model from which the parameters are passed 
    PersonDirector pd = new PersonDirector(pb);
    pd.createPerson();
  }
  
  public static void main(String []args)
  {
    PersonThinBuilder ptb = new PersonThinBuilder(" Image tools ", " The brush ");
    create(ptb); // Create a thin person 
    System.out.println("==============================================================");
    PersonFatBuilder pfb = new PersonFatBuilder(" Image tools ", " The brush ");
    create(pfb);
  }
}

Result:


 Draw the head of a thin man 
 Draw a thin body 
 Draw the thin man's left arm 
 Draw the thin man's right arm 
 Draw the thin left leg 
 Draw the thin man's right leg 
==============================================================
 Draw the head of a fat man 
 Draw the body of a fat man 
 Draw the fat man's left arm 
 Draw the fat man's right arm 
 Draw the fat man's left leg 
 Draw the fat man's right leg 

Above is java builder mode, hope to help you learn java programming.


Related articles: