A few days ago, from my colleague to the "Reconstruction" book of Matin Fowler, I was very enjoyable. Although it is very impressed, it is impossible to make a look, so it is impossible to make the book think it is important. As the saying goes: good memory is not as bad. Note: Many intuitive reconstruction methods have not explained, and they can guess their usage from their name. Reissue your function ============================================== ============= 1.Extract method2.inline method3.inline Temp4.Replace Temp with Query Temperature variable is: They are temporary, can only be used inside the function, this drive you write A longer function, because only this can access the temporary variable you want to access. If this method is used, this information can be obtained in all functions in the same class. This will give you great help so you can write a clearer code for this class. The assignment operation of the temporary variable that is only assigned once into a stand-alone function is then implemented on the temporary variable. Example: // original function double getprice () {int baseprice = _quatiTy * _Itemprice; double discountfactor; if ( basePrice> 1000) discountFactor = 0.95; else discountFactor = 0.98; return basePrice * discountFactor;} // Alternatively basePricedouble getPrice () {double dicountFactor; if (basePrice ()> 1000) discountFactor = 0.95; else discountFactor = 0.98; return basePrice ( ) * discountFactor;} int basePrice () {return _quatity * _itemPrice;} // Alternatively discountFactordouble getPrice () {return basePrice () * discountFactor ();} int basePrice () {return _quatity * _itemPrice;} double discountFactor () { if (basePrice ()> 1000) return 0.95; // benefit from replacing the last else return 0.98;} 5.Introduce Explaining Variable6.Split Temperary Variable7.Remove assignments to parameters not assigned 8.Replace Method with Method Object parameter Put this function into a separate object, so that local variables become a value domain within an object. Then you can break this large function into a small function in the same object.
9. Substitue Algorithm Replace Your Algorithm Trunge Features Between Objects ==================================== =============================== in your program, there is a function more communication with another class other than its Class: call The latter, or by the latter. Reduce the coupling between the class. Steps: Consider whether all features used by Source Class defined Source Method should also be moved. If Source Class's subclass and superclass have its statement, it may not be moved unless Target Class also exhibits polymorphism. In the Target Class, you will then copy the Source Method's code to Target method to determine how to correctly reference Target Object from SOURCE. Modify Source Method to make it a Delegating Method. Decide whether to delete Source Method and see if it is convenient. If deleted, the call to the Source Method is replaced with a call to TargetObject.TargetMethod. 2.Move Field3.extract class4.inline class5.hide delegate will call server.delegate.somethod () to Server.SomeMethod (). Remove the call to Server.deLegate. 6.Remove Middle Man7.Introduce Foreign Method Timer: The Server Class you use requires an additional function, but you can't modify this class. If you find that you have already built a lot of add-on functions for Server Class, or there are many CLASSs require the same external functions, you should not use this method, and you should use Introduce Local Exteset. Steps: Create a function in the Client Class to provide your feature. This function should not use any characteristics of the Client Class. If a value is required, it is transmitted to it as a parameter. The first parameter of the server with the Server Class entity. Comment this function as: external functions should be implemented at the Server Class. 8. Introduce local Extension Time: See 7. Introduce Foreign Method method: Create a new class, including those additional functions, making this class into SUBCLASS or Wrapper for Source Class.
Re-organizing data ================================================ =========== 1. The benefits of the Self Encapsulate Field package: Subclass can change the data reading, the write function is not encapsulated by overwriting the reading, the write function is not packaged: Simple 2. REPLACE DATA VALUE WITH OBJECT, for example: At the beginning, you will use a string to represent the concept of "Phone Number", but then you will find that the phone number needs to "format", "extracting the area code". Special behavior. If such a data item is only one or two, you can also put the relevant function in the object to which the data item belongs; but the duplicate code odor and Feature Envy smell will quickly exist from the code. When these stink begins, you should turn the data value into an object. 3.Change value to reference to turn this value object (Reference Object) 4. Change reference to value5.Replace Array with object6.duplicate Observed Data repeat meaning: Data in the interface class, Logical classes also have the same data. The OBServer mode is used to synchronize the data of the interface class and the logical class. It can be used for interfaces and logical separation. Further research is needed 7.Change Unidirectional Association to Bidirectional8.Change Bidirectional Association to Unidirectional9.Replace Magic Number with Symbolic Constant10.Encapsulate Field11.Encapsulate Collection12.Replace Record with DataClass13.Replace Type Code with Class alternative data type enumeration class BloodGroup { blood // public static final BloodGroup O = new BloodGroup (0); public static final BloodGroup A = new BloodGroup (1); public static final BloodGroup B = new BloodGroup (2); public static final BloodGroup AB = new BloodGroup (3) Private final int _code; private bloodgroup (int code) {_ code = code;}} 14.Replace Type Code with Subclasses You have a constant Type Code that affects Class behavior. Similar to the status processor mode 15. Repelace Type Code with State / Strategy You have a Type Code that affects the Class behavior, but you can't use Subclassing. For example: Type Code changes in the object lifecycle.
State Object Corresponding Field can be replaced with other state object16.Replace Subclass with Fields simplified conditional expression =============================== ============================ 1.Decompose Conditional Decompose it into multiple independent functions 2. ConsoLidate Conditional Expression (Merger Conditional) Merge Conditions of Conditions 3. ConsoLidate Duplicate Conditional Fragments (Conditional Conditions) There is the same piece of code on each branch of conditional. This reconstruction can better indicate that the changes of things will vary with the conditions. 4. Bremove Control Flag circulating statement, it is best to replace the control mark (used to determine whether to continue loop) 5.Replace Nested Conditional With Guard Clauses Replace the conditional type in the nested condition function in the guards, it is difficult to make people See the normal execution path. Give a branch to special attention. There are usually two forms: First, all branches are normal behaviors; the other is that there is only one normal behavior, and others are uncommon. It is best to return a clear value to see more clearly. // original code double getPayAmount () {double result; if (_isDead) result = deadAmout (); else {if (_isSeparated) result = separatedAmount (); else {if (_isRetired) result = retiredAmount (); else result = normalPayAmount ();}}} // New code double getpayamount () {if (_Indead) Return deadamount (); if (_ISseparate) returnidAmount (); if (_isretired) return retiredamount (); return normalpayamount ();} 6. Replace Conditional With Polymorphism's most fundamental benefits in polymorphisms are: If you need to take different behaviors according to different types of objects, polymorphism allows you to write a clear condition. 7. Introduce NULL Object Replaces Null Value to Null Object, such as you want to display a Persion object information, about 20 instance variables. If these variables can be set to null, then printing a Person object will be very complicated. If you replace NULL VALUE to null Object (they all know how to display them properly), the Person class can reduce a lot of code. Bigma Mode than NULL OBJECT mode: SPECAIL CASE mode. Steps: 1. Create a Subclass for the Source Class, its behavior like the NULL version of the Source Class. Add an isnull () function in Source Class and Subclass, the former returns false, the latter returns true. 2. Find all the source objects that get the NULL. Modify it to get NULL OBJECT.