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)