Reconstruction Method 59: Pull up constructor body (constructor ontology moving up)

Source: Internet
Author: User

You have some constructors in each subclass, and their ontology is almost identical.Create a new constructor in the superclass and call it in the subclass constructor.

Motivation: constructor is a wonderful thing. They are not common functions, and they are more restrictive than normal functions.

If you see that the functions in each subclass share the same behavior, the first thought should be to extract the common behavior into an independent function and then promote the function to a superclass. For constructors, the common behavior of each other is often "Object Construction ". At this time, you need to provide a constructor in the superclass, and then let the subclass call it. In many cases, the only action of a subclass constructor is to call a superclass. Here, you cannot use the pull up method (method up) because you cannot inherit the superclass constructor from the subclass.

If the refactoring process is too complex, you can consider using replace constructor with factory method (replace constructors with factory functions ).

 

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.