Reconstruction Techniques: handling the generalization relation [2]. Reconstruction Techniques: generalization relations
Total returned directory
Directory of this section
- Push Down Method (function downward)
- Push Down Field (Field move Down)
- Extract Subclass (Extract Subclass)
4. Push Down Method (function Down) Overview
A function in the base class is only related to some (not all) subclasses.
Move this function to the related child classes.
Motivation
The Push Down Method is the opposite of the Pull Up Method. When it is necessary for me to move some actions from the base class to a specific subclass, I use the Push Down Method, which is usually only useful in this case. You may need it after using Extract Subclass.
Example
As shown in the following code, the method Bark () in the Animal class is only used in its subclass Dog, so the best solution is to move this method to the subclass Dog.
public class Animal{ public void Bark() { // code to bark }}public class Dog : Animal{}public class Cat : Animal{}
The restructured code is as follows. If there are no other fields or common methods in the parent class Animal, you can consider making the Bark () method an interface to remove the Animal class.
public class Animal{}public class Dog : Animal{ public void Bark() { // code to bark }}public class Cat : Animal{}
Summary
Object-oriented features (inheritance, encapsulation, and polymorphism) can often help us, but they may also cause excessive use or improper use. So how to grasp the design, this becomes crucial. When to use the inheritance method, when to use combination and aggregation, interface and inheritance class selection have become our focus.
5 Push Down Field (Field move Down) Summary
A field in the base class is used only by some (not all) subclasses.
Move this field to the Child classes that require it.
Motivation
If only some (not all) subclasses need a field in the base class, you can use this item to refactor.
Example
As shown in the following code, the _ resolution field in the basic class Task class will only be used in the subclass BugTask, so you should consider placing it in the BugTask class.
public class Task{ protected string _resolution;}public class BugTask : Task{}public class FeatureTask : Task{}
The restructured code is as follows. The benefits of doing so can simplify the base class and make other subclasses that do not use it easier. If there are many such fields, using this Refactoring can also save some memory.
public class Task{ }public class BugTask : Task{ protected string _resolution;}public class FeatureTask : Task{}
Summary
This refactoring is also a very simple refactoring, And we will unconsciously use it in many cases.
6 Extract Subclass (Extract Subclass) Overview
Some features in the class are only used by some (not all) instances.
Create a new subclass and transfer the features mentioned above to the subclass.
Motivation
The main motivation for using this refactoring is: You find that some behaviors in the class are only used by some instances, and other instances do not need them.
Extract Class is another choice except Extract Subclass. The choice between the two is actually the choice between delegation and inheritance. Extract Subclass is usually easier, but it also has restrictions: once an object is created, you cannot change the type-related behavior. However, if you use Extract Class, you only need to insert another component to change the behavior of the object. In addition, subclass can only represent a group of changes. If you want a class to change in several different ways, you must use the delegate.
Example
When some methods in your base class do not need to be accessed by all subclasses and you want to adjust them to subclass, This refactoring will become very useful. As shown in the following code, we need a registry class to process the information about the course selection for students. However, when the Registration class starts to work, we realize that we will use the Registration class in two different contexts. NonRegistrationAction and Notes will only be used when we process unregistered content.
public class Registration{ public NonRegistrationAction Action { get; set; } public decimal RegistrationTotal { get; set; } public string Notes { get; set; } public string Description { get; set; } public DateTime RegistrationDate { get; set; }}
The restructured code is as follows, which also meets the single responsibility of one of the five object-oriented principles. It also makes the class structure clearer and enhances maintainability.
public class Registration{ public decimal RegistrationTotal { get; set; } public string Description { get; set; } public DateTime RegistrationDate { get; set; }}public class NonRegistration : Registration{ public NonRegistrationAction Action { get; set; } public string Notes { get; set; }}
Summary
This refactoring method is often used for the role class, and is similar to some previous refactoring methods.
To Be Continued ......