Reconstruction of Art

Copyright: Feel free to reprint! https://blog.csdn.net/qq_41723615/article/details/90240009

What is Reconstruction:

A method of adjusting the internal structure of the software, the software object is provided under the same conditions observable behavior, improving its understandability, modified to reduce its cost.

 

Why Reconstruction:

The program difficult to read, difficult to modify, duplicate the logic of the program, difficult to modify, to modify behavior when you add a new program existing code, it is difficult to modify with conditional logic repeat procedures, difficult to modify.


Reconstruction can

    1, change the software design

    2, to make the software easier to understand, read

    3, to help find the BUG, ​​all logic is specified only in a single location, a new crisis will not change existing behavior

    4, to improve the programming speed, expressed as simple logic conditions

 

When reconstruction: Shibuguosan, three reconstruction.

    1. reconstruction while adding features

    2. Modify the reconstruction errors

    3. Reconstruction of the code review

 

Indirection and Reconstruction:

Indirection: the program, split into a plurality of function objects or a plurality of sub-functions, which can allow sub-functions or sub-object sharing logic, and can achieve intended implemented separately.

Sometimes a subclass and modifications without risk of another, each subclass can be conditional logic package, flexible articulate conditional logic, the condition logic into message form, reducing code duplication, increase the sharpness and improve flexibility.

 

Reconstruction of the problem:

database:

Because of the tight coupling with the database program together, when you want the system stratification, but also inevitably migrate data, which is a tedious process.

In non-object database, a spacer layer may be inserted between the object model and database model, the isolation of change in the two models.

Modify the interface:

Published interfaces - when you need to modify the interface is what "can not find or can not find the changes" when using the code, modify the interface will become a problem to be solved.

At this time, both old and new interfaces must be maintained until all users have time to react to this change. Try to make the old interface calls the new interface, so need to leave the old interface.

Or try not to publish the interface, reduce maintenance efforts.

Design:

Before the design does not consider the security needs, and to consider when remodeling the reconstituted system has a good security system, this is a dilemma of choice, as far as possible according to the difficulty and intensity it takes to do trade-offs.

 

When not to Reconstruction:

When the existing code too complex, difficult to understand the logic, and the code does not run when it should not be reconstructed, it would be faster to rewrite some. Reconstruction on the premise that the code must be able to function properly in most cases. There are an estimated project deadlines and no one wants to reconstruct.

 

Reconstruction of warning:

Duplicated code, the function is too long, too large class parameter list is too long, divergent changes, modifications shotgun type, complex Attachment / dependence heavy, mud pie data, basic paranoid type, Switch horror appeared parallel inheritance system, verbose class, the future of rhetoric, confusing temporary fields, excessive coupling of the news chain, intermediary, too close class, similar to class, the library is not perfect, childlike data class, subclass inherits the superclass is rejected, annotated many.

 

How to reconstruct:

 

First, the function is too long

Refining function for too long

Second, the inline function

The name of a function body and as easy to understand.

Insert body function call point function, the function is then removed.

Third, inline temporary variables

There is a temporary variable is assigned only once with a simple expression, but it prevents other reconstruction techniques.

At this point we should all variables referenced action and replaced with the expression of its assignment.

Fourth, in order to replace the temporary variable query

Program to maintain the results of a calculation expression to a temporary variable.

This expression to extract a separate function. All this temporary variable reference point is replaced with a call to the new function. Since then, new functions can be other function calls.

Fifth, the introduction of explanatory variables

There is a complex expression.

The result of the expression (or part) is placed in a temporary variable expression in order to explain the use of the variable name.

Sixth, the decomposition temporary variables

The program has a temporary variable is assigned more than once, it is neither the loop variable, it is not being used to collect the results.

For each assignment, create a separate, temporary variables corresponding.

Seven, to remove the assignment of parameters

A code parameter assignment.

In a position of the temporary variable substitution parameter.

Eight, to replace the function of the object function

There is a large function, where the use of local variables so that you can not use Extract Method (110).

This function into a single object, this way it would be a local variable in the plurality of field objects. It may then be decomposed into a plurality of small major function of this function in the same object.

Nine, replacement algorithm

Wanted algorithm is replaced with another clearer algorithm.

The algorithm function is replaced with another body.

Ten, move function

There are procedures and functions in another class outside their classes much interaction: Call the latter, the latter calls or h.

The establishment of a new function has a similar behavior in the function of the most frequently cited category. The old function becomes a mere delegate function, or to completely remove the old function.

XI, move fields

Program in a field is more than the use of another class in its category.

In a new target class field, modify the source field for all users, so that they use the new field.

Twelve, refining class

A class made two sort of thing.

To create a new class, the relevant fields and functions moving from the old to the new class category.

XIII, the class of inline

There is a class did not do too many things.

All properties of this class moved to another class, and then remove the source class.

XIV commissioned hidden relationship

Improve customer class to call a delegate of another object.

All the functions needed to build on the customer service category to hide the trust relationships.

Fifth, remove the middleman

A simple class to do too much trust action.

Allow customers to directly call fiduciary class.

Sixteen, the introduction of additional functions

We need to add a function to provide class of service, but you can not modify this class.

Establish a function of the client, and the shape parameter passed to the first instance of a service class.

XVII introduced local expansion

We need to provide some additional functions for the service class, but you can not modify this class.

Create a new class, so that it includes these additional functions, so this extension products become a source subclass or wrapper classes.

Eighteen, since the packaging field

Direct access to a field, but the coupling between the field and gradually become unwieldy.

This field value is established / set functions, and only a function of the type of access fields.

Nineteen, instead of the data values ​​in the object

There is a data item, use makes sense in conjunction with other data and behavior.

The data item becomes the object.

XX, change the value of the object referenced objects

Derived from a class equal to each other many instances, it is desirable to replace them with the same object.

This value becomes the object referenced objects.

XXI will be changed to reference the object value object

There is a reference to the object, it is small and constant and difficult to manage.

Turn it into a value object.

XXII, to replace an array of objects

There is an array whose elements each represent different things in order to replace the array objects.

For each element in the array to a field to FIG.

Xxiii, copy the data to be monitored

There are some areas of the data being in the GUI controls, functions and areas need to access the data.

Copying the data to a field object. Observer establish a pattern for synchronization is repeated and the data objects within the field of the GUI object.

XXIV will be changed to one-way association bidirectional associations

Both classes require the use of other features, but only during a one-way connection.

Add a reverse pointer, modify and update the two functions can be simultaneously connected.

XXV, the association changed to one-way two-way association

Bidirectional association between two classes, but do not need to have the characteristics of a class to another class today.

Remove unnecessary association.

XXVI, to replace the literal magic number

There is a literal value, with a special meaning.

Create a constant, according to its significance to name it, and replace the above literal value of this constant.

Xxvii, packaging field

There is a public class field.

Declare it as private, and provide access functions.

Xxviii, package set

There is a function that returns a collection.

Let this function returns a read-only copy of the collection, and provide add / remove function set of elements in this class.

XXIX data record class of substituted

We need to face the record structure of the traditional programming environment.

Create a dummy data object for the record.

Thirty to replace the class code type

Class has a type code value, but it does not affect the behavior of the class.

To replace a new class of the type code value.

Xxxi, substituted type code to subclass

There is an immutable type code, it will affect the behavior of the class.

In a subclass of this type of code is substituted.

XXXII, with State / Strategy substitution pattern code

There is a type code, it will affect the behavior of the class, but can not provide inheritance means to eliminate it.

In the state of the object type code substituent.

33 or to replace the subclass field

The only difference in each subclass only "return constant data" function of the body.

Modify these functions so that they return to the superclass of a (new) field, and then destroy the subclass.

Thirty - four, decomposition conditional expression

There is a complex conditional statements (if-then-else).

From if, then, else extracted three paragraphs each function independently.

Xxxv merger conditional expression

There are some columns conditional test, get the same result.

These tests will be combined into a conditional expression, and this conditional expression refined into a separate function.

XXXVI, merge duplicate fragments conditions

It has the same section of code on each branch of the conditional expression.

This code is repeated to move out of the conditional expression.

Thirty-seven, remove the control mark

In a series of Boolean expressions, a variable with the role of "control flag" (control flag) is.

Return statement or break statement substituent control flag.

Xxxviii guard statement as an alternative to the nested conditional expression

Conditional logic makes the function difficult to see normal execution path.

Wei statements using the performance of all special circumstances.

Thirty-nine to polymorphic substituted conditional expression

Have got a conditional expression, it is to choose a different behavior depending on the type of object.

Each branch of the conditional expression into the overwrite function within a subclass, and the original function is declared abstract function.

Forty introduced Null Object

You need to double check whether an object is null.

The null value is replaced with a null object.

XLI introduced assertion

A piece of code need to make some assumptions about the program status.

To assert a clear manifestation of this hypothesis.

Forty-two, the function was renamed

It failed to reveal the name of the function uses the function.

Modify the function name.

Forty-three, add parameters

A function needs more information from the calling end.

Adds an object parameter for this function, the information required to make the object into the function.

Forty-four, remove parameters

No longer need a body function parameters.

The removal parameters.

Forty-five, query and modify the function separated from the function

A function not only returns the object state value, and modify the state of the object.

Create two different functions, one of which is responsible for querying the other for modification.

By 46 parameters so that the function to carry

Some do a similar job function, but function in the body, but contain different values.

Creation of a single function, parameter to express those different values.

Forty-seven to clarify replace function parameters

There is a function, which depends entirely on the parameter values ​​and behave differently.

For each possible value of the parameter, the establishment of an independent function.

Forty-eight keep objects intact

Some values ​​taken from an object, when they are used as a parameter of a function call.

Instead pass an entire object.

Forty-nine to replace the function parameters

A function call to the object, and the results are passed as a parameter to another function, the parameter received before the function itself can be a function call.

Let the receiver parameters to remove this parameter, and a direct call function before.

Fifty introducing parameter object

Some parameters are always naturally occur simultaneously.

These parameters are substituted to an object.

Fifty-first, remove setters

Class of a field value should be set when the object is created and then never change.

Remove all set values ​​of the attributes of function.

Fifty-second, hidden function

There is a function that has never been used in any other category.

This function will be changed to private.

Fifty-third, to replace the plant constructor function

We do not want to type a simple construction of the action when you create the object.

The constructor function replaced with the factory.

Fifty-fourth, packaging downcast

Object returned by a function, you need to perform downcast by the function caller.

The action moved to the downcast function.

Fifty-five to replace abnormal error code

A function to return a specific code to indicate some error condition.

Use exception.

Fifty-six to replace abnormal test

The caller may face a pre-condition check, it throws an exception.

Modify the caller, so he do first check before calling the function.

Fifty-seven, move the field

Two subclasses have the same field.

The field is moved to the superclass.

Fifty-eight, the shift function

Some functions, produce identical results in each subclass.

The function to move the superclass.

159 has shifted the constructor body

Some constructor has respective subclass, almost identical to the body thereof.

Create a constructor in the superclass, and call it in a subclass constructor.

Sixty function down

Superclass of a function only with some (but not all) the relevant subclasses.

This function will move related to those subclasses.

Sixty-one, field down

Superclass of a field is only used in part (but not all) sub-class.

This field will need to move it to those subclasses.

Sixty-two, refining subclass

Some characteristics of the class is only some (but not all) instances used.

A new subclass, a portion of the above mentioned properties that move subclass.

Sixty-three, refining superclass

Both classes have similar characteristics.

For both classes to create a super class, super class will move to the same characteristics.

Sixty-four, refining Interface

Several customers use the same subset of the class interface, an interface, or both the same section.

The same subset of a separate interface to extract.

Sixty-five, iterative inheritance system

No big difference between the superclass and subclasses

Combine them into one.

Sixty-six, create a template function

There are some subclasses, wherein the corresponding sequence of certain functions perform similar operations, but the details will differ for each operation.

These operations are placed in an independent function, and keep them the same signature, then it becomes the same as the original function, and then moved to the original function of the superclass.

Sixty-seven commissioned to replace inheritance

Only a subclass uses only a portion of the super-class interface, or do not need the data inherited.

In a new subclass field to hold a superclass; subclass adjustment function, so it is to delegate the superclass; then remove the inheritance relationship between the two.

Sixty-eight to inherit replace commission

Use trust relationships between two classes, and often commissioned to write many simple functions for the entire interface.

Let delegate class inherits fiduciary class.

Sixty-nine, sort and dispersed inheritance system

An inherited system and to assume two responsibilities.

Establish two successor system, and let a relationship which can be commissioned by another call.

Seventy design into the process of design objects

We do have some traditional procedural style code.

Recording data becomes an object, the behavior of the chunks into small squares, and pushed into the behavior of related objects.

Seventy-one the art and the expression / display separation

Among some of the art class contains the GUI logic.

The logical separation of the art, is to establish a separate domain class thereof.

Seventy-two refining inheritance system

There is a class to do too much work, part of a lot of work to complete the conditional expression.

Establish inheritance system to a sub-class represents a special case.

 

 

Guess you like

Origin blog.csdn.net/qq_41723615/article/details/90240009