Refactoring 21st Day Merge Inheritance (Collapse Hierarchy)

Source: Internet
Author: User

Understanding : "Merge Inheritance" in this article means that if the properties and methods of a subclass are also appropriate for the base class, you can remove the subclasses, thereby reducing the dependency.

explanation : The previous article we talked about "extracting sub-class" Refactoring refers to extracting these responsibilities into the appropriate subclasses when a responsibility in the base class is not required by all subclasses. And the "Merge inheritance" refactoring we're going to talk about today is generally used when we think we don't need subclasses.

1  Public classWebsite2     {3          Public stringTitle {Get;Set; }4          Public stringDescription {Get;Set; }5          PublicIenumerable<webpage> Pages {Get;Set; }6     }7 8      Public classStudentwebsite:website9     {Ten          Public BOOLIsActive {Get;Set; } One}

As shown in the code above, the Studentwebsite subclass has no responsibility in addition to having a property that indicates whether the site is active, in which case we realize that the IsActive property can be applied to all sites, so we can move the IsActive attribute up to the base class, and remove the Studentwebsite class.

1   Public classWebsite2     {3          Public stringTitle {Get;Set; }4          Public stringDescription {Get;Set; }5          PublicIenumerable<webpage> Pages {Get;Set; }6          Public BOOLIsActive {Get;Set; }7}

In fact, this article and the previous chapter mainly discuss the inheritance relationship between the subclass and the parent class, and how to determine when to use inheritance, generally we can deal with these relationships, so it is relatively simple.

Refactoring 21st Day Merge Inheritance (Collapse Hierarchy)

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.