ArticleDirectory
- Explanation:
- Before impulse:
- After Impulse:
- Explanation:
- Before impulse:
- After Impulse:
- Explanation:
- Before impulse:
- After Impulse:
- Explanation:
- Before impulse:
- After Impulse:
- Explanation:
- Explanation:
- Explanation:
- Explanation:
- Before impulse:
- After Impulse:
- Explanation:
- Explanation:
- Explanation:
1. Explanation of pull up field (raised value range:
If you find that each subclass has the same value range, usePull up fieldPromote this value to the parent class.
Before impulse:
After Impulse:
2. Pull up method (Lifting function) Explanation:
If each subclass has the same function, this function does the same thing and the result is the same, then usePull up MethodPromote this function to the parent class.
Before impulse:
After Impulse:
3. Pull up constructor body (Lifting constructor) Explanation:
Pay special attention to the repeatedCodeIf possible, extract them into methods and move them to the parent class. for the constructor of the subclass, we need to find the same part and use the same part to form the constructor of the parent class.
For exampleSalesmanAndEngineerAnd so on. When constructing them, all employees needNameAndLevelAttribute, you can consider usingPull up constructor bodyUpgrade these two attributes to the constructor of the parent class.
Before impulse:
Class employee {public string name {Get; set;} public int level {Get; set ;}//...} class Salesman: Maid {Get; set;} public salesman (string name, int level, string holobby) {This. name = Name; this. level = level; this. holobby = holobby ;}//...} //...
After Impulse:
Class employee {public string name {Get; set;} public int level {Get; set;} public employee (string name, int level) {This. name = Name; this. level = level ;}//...} class Salesman: Maid {Get; set;} public salesman (string name, int level, string holobby): Base (name, level) {This. holobby = holobby ;}//...} //...
4. Explanation of push down method (lower function:
A function in the parent class is only related to some subclasses, not all subclasses.Push down MethodRefactoring means to put these functions into sub-classes that use them, rather than in the parent class.
Before impulse:
After Impulse:
5. Explanation of push down field (lowering the value range:
AndPush down MethodThe problem described is similar. If a value field in the parent class is not useful to each subclass, you should put it in the subclass that requires it.
6. Explanation of extract subclass (extract subclass:
We have created some instances of the class, but not every instance uses all the features of the class, which is often caused by excessive functional design in the class. try to extract some subclasses from this class, and the functions in the subclass should be clearly divided.
7. Extract superclass (refined parent class) Explanation:
If you find two classes have many identical features, try to find the same features in the two classes, if you can find a suitable reason for these two classes to inherit from one parent class, you can extract the parent class that contains the same parts of the two classes.
8. Extract interface (extract Interface) Explanation:
Classes and classes often call each other, suchClassaIn a functionClassbInClassbPassed as a parameterClassaThis function, which meansClassaThis function can callClassbCan all the functions inClassaDoes this function define a specific function? Let it do only some things, and avoid other "unauthorized behaviors". Some people often talk about it-use interfaces to reduce coupling, which isExtract InterfaceCredit.
This refactoring method is often used to solve the dependency problem of the class on another class and reduce coupling.
Before impulse:
Class XML {public void read (){//...} public void translate (){//...} // some other methods} class weatherservice {Public String getweather (XML) {XML. read (); XML. translate (); // other code, but without XML Object} // some other methods}
After Impulse:
Interface ioperation {void read (); void translate ();} class XML: ioperation {public void read (){//...} public void translate (){//...} // some other methods} class weatherservice {Public String getweather (ioperation operation) {operation. read (); operation. translate (); // other code, but without XML Object} // some other methods}
Note that the line highlighted by the Code has become called interface, instead of relying only on the callXMLClass instances are not very valuable for a single class to implement such an interface. If many classes implement the same interface, this will be very useful.
9. Collapse hierarchy (remove unnecessary inheritance relationships) Explanation:
A huge inheritance system can easily become complex. It is very important to clarify the functions of parent classes and child classes. You may find unnecessary child classes.Pull up fieldAndPull up MethodKill it.
10. Replace inheritance with delegation (replace inheritance with delegation) Explanation:
If you wantClassaUse a class (suchClassb).ClassaInherited fromClassbWhat a terrible design! You canClassaContainsClassbYou can use this value field to call the function you need. This value field is "delegate", and you can remove it at this time.ClassaAndClassbThere should be no inheritance relationship between them.
11. Replace delegation with inheritance (replace delegation with inheritance) Explanation:
This andReplace inheritance with DelegationOn the contrary, if you need to use all the functions in the delegate, you should consider whether there is an inheritance relationship between them.
Link: http://www.cnblogs.com/technology/archive/2011/05/16/2047861.html