The call order of the builder

Source: Internet
Author: User

The builder for the base class is definitely called in the builder of a derived class, and is gradually linked up so that the builder used by each base class can be called. The reason to do this is because the builder has a special task: to check whether the object is properly built.

Let's take a look at an example that shows the effects of compositing, inheritance, and polymorphism in the build order:

Class meal {  meal ()  { system.out.println ("Meal ()")  }}class bread  {  bread ()  { system.out.println ("Bread ()"); }}class cheese {   cheese ()  { system.out.println ("Cheese ()");  }}class lettuce{  lettuce ()  { system.out.println ("lettuce ()"); }}class lunch extends meal {   lunch ()  { system.out.println ("Lunch ()");}} Class portablelunch extends lunch {  portablelunch ()  {   System.out.println ("Portablelunch ()");   }}class sandwich extends portablelunch {   bread b = new bread ();   cheese c = new cheese ();   lettuce l = new lettuce ();   sandwich ()  {     system.out.println ("Sandwich ()");   }  public static void main (String[] args)  {    new sandwich ();   }} 

lose the results are as follows:

Meal ()

Lunch ()

Portablelunch ()

Bread ()

Cheese ()

Lettuce ()

Sandwich ()

This means that for a complex object, the builder's call is in the following order:

(1) call the base class Builder. This step will be repeated, the first to be constructed is the root of the hierarchical structure, and then

is the next derivative class, and so on. Until it reaches the deepest layer of the derivative class.

(2) The member initialization module is called in the order of Declaration.

(3) call the main body of the derived builder.

Behavior of the Polymorphism method inside the builder

Whether the builder should build the current class or the derived class is a problem,

Here's an example:

abstract class glyph {  abstract void  draw ();   glyph ()  {  system.out.println ("Glyph ()  before draw ()");   draw ();   system.out.println ("Glyph ()  after draw ()");  }}class  Roundglyph extends glyph {  int radius = 1;  roundglyph (int  r)  {  radius = r;  system.out.println (   " Roundglyph.roundglyph (), radius =  "+ radius);   }  void draw ()  {  system.out.println ("Roundglyph.draw (), radius = "  + radius);   }}public class polyconstructors {  public static void main ( String[] args)  {    new roundglyph (5);   }} 

Output Result:

Glyph () before draw ()

Roundglyph.draw (), radius = 0

Glyph () after draw ()

Roundglyph.roundglyph (), radius = 5

The initialization order of the above sections is not quite complete, and that is the key to solving the problem. The actual process of initialization is this:

(1) before any other action is taken, the storage space allocated for the object is initialized to binary zero.

(2) call the base class builder as described earlier. At this point, the overridden draw () method will be called (the

Before the roundglyph Builder Call), the radius value of 0is found, because the step (1)

Caused by.

(3) call the member initialization code in the order in which it was originally declared.

(4) call the principal of the derived class builder.

Therefore, a particularly effective rule when designing a builder is to put the object into a ready state in as simple a way as possible, and avoid calling any method if possible. The only methods that are safe to invoke in the builder are those that have a final attribute in the underlying class (also applicable to private methods, which automatically have final property). These methods cannot be overwritten, so there is no such potential problem.


The call order of the builder

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.