Refactoring Method 33: replace subclass with field (replace subclass with field)

Source: Internet
Author: User

 

The only difference between your sub-classes is the function that returns constant data. Modify these functions so that they return a (new) read-only value in the superclass and then destroy the subclass.

Motivation: the purpose of creating a subclass is to add new features or change its behavior. There is a change behavior called a constant function, which returns a hard-coded value. This has its own purpose: You can let the same access function of different subclasses return different values. You can declare an access function as an abstract function in a superclass and let it return different values in different subclasses.

Although a constant function has its own purpose, it does not have enough value if it has only a constant function in its subclass. You can design a field corresponding to the return value of the constant function in the superclass to completely remove such a subclass. In this way, the extra complexity caused by inheritance can be avoided.

 

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.