Learning reconstruction (4) - Re-organize data

1. Self Encapsulate Field (range from package)

You direct access to a range, but the coupling between the range and gradually become unwieldy. Establish value / set value function (get / set) for the range, and only these functions to access the range.

Scenario: In fact, this provides two operating functions for each variable approach is not desirable hundred percent, this was a good thing to do data storage and the use of isolation, especially for protection target data is very good, can be returned to the caller a copy of the data, do not worry about their data will be tampered with. But there are bad places, that will bring a lot of small functions, potentially increasing the amount of body type.

Example:

private int _low, _high

boolean includes (int arg) {

    return arg >= _low && arg <= _high;

}

Reconstructed as:

private int _low, _high

boolean includes (int arg) {

    return arg >= getLow() && arg <= getHigh();

}

int getLow() {return _low};

int getHigh() {return _high};

 

2. Replace Data Value with Object (instead of the data values ​​in the object)

Some data according to certain rules or features into one object

Scenario: early development code requires only some simple data, such as a person's basic information: name, ID number, over time, require additional data: phone number, address, education and so on, so will set out a row of variables, each dealing with a person's data is to pass a lot of parameters. At this time, these personal information can be structured into a single class, you can only declare an object, but with the continued wealth of information of the person, will not always impact to the original interface.

Example: slightly

 

3. Change Value to Reference (reference to the real value of the target object)

You have a class, derived from many entities equal, you want to replace them as a single object. The real value of this object becomes a reference object.

Scenario: We generally use a class all the time took on a new objects out of these out every new object is the real value of the object, but in many scenes we will change the internal state of these objects, and the need for these objects passed, this time with real-valued target to meet this demand will be very clumsy, like new out of a car, and then set up under the BMW brand is set next color is blue, and so on. We need a reference to the object under such a scenario, the same class can be a member variable, using different classes can use similar plants, such as the singleton pattern to have an object management class for other classes access.

Example: slightly

 

4. Change Reference to Value (the value of the real object reference to an object)

And 3 reverse.

Scenario: An important feature of an object in real terms remain the same, so you can ensure that each user need not worry about changes in the data and synchronization. Reference objects tend to make logic is complicated, so use the time to make choices.

Example: slightly

 

5. Replace Array with Object (object-substituted array)

Alternatively an array of an object, for each element of the array, expressed as a range.

Scenario: An array is a common structure for organizing data. But they should be used only in a certain order to accommodate a set of similar objects. Sometimes you will find an array accommodate a number of different objects, this will give users an array of trouble, because they are difficult to remember like "The first element of the array is the name" the convention. The object is not the same. Some information can be passed by name and function. Make it easier to understand.

Example:

String[] row = new String[3];

row[0] = "Liverpool";

row[1] = "15";

Reconstructed as:

Performance row  = new Performance();

row.setName("Liverpool");

row.setWins("15");

 

6. Duplicate Observed Data (Assignment "monitored data")

Do you have a sum domain data being in the GUI space, and domain method needs to access them. Copying this data into a domain object, create a Observer pattern, intended to duplicate data in a domain object and GUI object synchronous control.

Scenario:

Example:

To be continued

Guess you like

Origin www.cnblogs.com/youknowzcm/p/11797619.html