Understanding the builder pattern of Java design Patterns _java

Source: Internet
Author: User

The Builder model (Builder pattern) is primarily used to "step through a complex object", where "step-by-step" is a stable algorithm, and parts of complex objects often change. Therefore, the builder model is mainly used to solve the demand change of "object part". This allows finer control over the process of object construction.

Package com.shejimoshi.create.Builder;
/**
 * Function: The intention 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 algorithms for       creating complex objects should be independent of the components of the object and the way they are assembled
 *       When the construction process must allow the constructed object to have different representations
 /public
abstract class Personbuilder
{
  //painting a tool that a character needs to use
  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 body public
  abstract void Buildbody ();
  Draw the right foot of the left 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 derives the corresponding subclass to construct the corresponding actual usage type

Package com.shejimoshi.create.Builder;


/**
 * Function: The intention 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 algorithms for       creating complex objects should be independent of the components of the object and the way they are assembled
 *       When the construction process must allow the constructed object to have a different representation */public
class Personthinbuilder extends Personbuilder
{

  Public Personthinbuilder (String t1, string t2)
  {
    super (T1, T2);
  }

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

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

  @Override public
  void Buildarmleft ()
  {
    System.out.println ("Draw Skinny's left arm");
  }

  @Override public
  void Buildarmright ()
  {
    System.out.println ("Draw Skinny's right arm");
  }

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

  @Override public
  void Buildlegright ()
  {
    System.out.println ("Draw Skinny's right leg");
  }


Package com.shejimoshi.create.Builder;


/**
 * Function: The intention 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 algorithms for    creating complex objects should be independent of the components of the object and the way
 they are assembled *    When the construction process must allow the constructed object to have a different representation */public
class Personfatbuilder extends Personbuilder
{

  public Personfatbuilder (String t1, string t2)
  {
    super (T1, T2);
  }

  @Override public
  void Buildhead ()
  {
    System.out.println ("Draw a fat Man's Head");
  }

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

  @Override public
  void Buildarmleft ()
  {
    System.out.println ("Draw Fat Man's left arm");
  }

  @Override public
  void Buildarmright ()
  {
    System.out.println ("Draw Fat Man's right arm");
  }

  @Override public
  void Buildlegleft ()
  {
    System.out.println ("Draw fat left leg");
  }

  @Override public
  void Buildlegright ()
  {
    System.out.println ("Draw Fat Man's right Leg");
  }



Our generators are given to the conductor to implement the actions created

Package com.shejimoshi.create.Builder;


/**
 * Function: Create the character of the conductor/public
class Persondirector
{
  private personbuilder PB;
  
  Pass the corresponding character creation 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 corresponding character public
  static void Create (Personbuilder pb)
  {
    ////The model with parameters is used to create the corresponding object
    persondirector pd = new Persondirector (PB);
    Pd.createperson ();
  }
  
  public static void Main (String []args)
  {
    Personthinbuilder PTB = new Personthinbuilder ("Image Tool", "brush");
    Create (PTB); Create a skinny
    System.out.println ("==============================================================");
    Personfatbuilder PFB = new Personfatbuilder ("Image Tool", "brush");
    Create (PFB);
  }

Run Result:

Draw a skinny head, draw a skinny body, draw a skinny one's left arm, draw the skinny right arm, draw the skinny
left leg
, draw the skinny right thigh
============================== ================================ drew a fat man's head, drew a fat man's body, drew fat's left arm, drew fat
's right
arm, and drew fat's
left leg
. Draw a fat man's right leg.

This is the Java Builder model, and I want to help you learn Java programming.

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.