Refactoring Method (v) Re-organization of data

Source: Internet
Author: User

1. Self Encapsulate field (auto-encapsulation fields)

Symptom: You have direct access to a field, but the coupling relationship with the field is gradually becoming unwieldy.

Workaround: Create a value/setpoint function for this field and access the field only with these functions.

2. Replace data value with object (replacing the values with objects)

Symptoms: You have a data item that is meaningful to use with other data and behaviors.

Workaround: Turn data into an object

3. Change value to Reference (changes the value object to a Reference object)

Symptoms: You derive from a class many entities that are equal to each other, and you want to replace them with the same object.

Workaround: Turn this value object into a reference object.

4. Change Reference to value (changing the Reference object to a value object)

Symptoms: You have a reference object that is small and immutable and difficult to manage.

Workaround: Turn it into a value object.

5. Replace array with object (replaces arrays with objects)

Symptoms: You have an array in which the elements each represent different things.

Workaround: Replace the array with an object. For each element in the array, it is represented as a field.

6. Duplicate observed data (copy "monitored")

Symptoms: You have some domain data in a GUI control, and domain functions need access to that data.

Workaround: Copy the data into a domain object. Establish a observer mode to synchronize duplicate data within domain objects and GUI objects.

7, change unidirectional Association to bidirectional (one-way association to two-way association)

Symptoms: Two classes need to use each other's attributes, but there is only one one-way connection in between.

Workaround: Add a reverse pointer and allow the modification function to update both connections at the same time.

8. Change Bidirectional Association to unidirectional (changing bidirectional association to one-way association)

Symptoms: There are two-way associations between two classes, but one class now no longer needs the attributes of another class.

Workaround: Remove unnecessary associations.

9. Replace magic number with symbolic Constant (literal constants replace magic numbers)

Symptoms: You have a literal value, with a special meaning.

Workaround: Create a constant, name it according to its meaning, and replace the literal value above with the constant.

10. Encapsulate field (Package fields)

Symptom: There is a public field in your class.

WORKAROUND: Declare it as private and provide the appropriate access function.

11. Encapsulate Collection (Package set)

Symptom: There is a function that returns a collection.

WORKAROUND: Let this function return a read-only copy of the collection and provide a function to add/remove elements of the collection in this class.

12. Replace record with data class (to replace the records with the date Class)

Symptoms: You need to face the record structure in a traditional programming environment.

Workaround: Create a "dummy" data object for the record.

13. Replace type code with class (substituting class code)

Symptom: There is a numeric type code in the class, but it does not affect the behavior of the class.

Workaround: Replace the numeric type code with a new class

14. Replace type code with subclasses (sub-category code)

Symptom: You have an immutable type code that affects the behavior of the class.

Workaround: Replace this type code with a subclass.

15. Replace type code with STATE/STRATEGY (replace the code with State/strategy)

Symptoms: You have a type code that affects the behavior of the class, but you can't eliminate it by inheriting it.

Workaround: Replace the type code with the state object.

16. Replace subclass with Fields (replace subclass with Field)

Symptom: The only difference between your subclasses is only the function that returns constant data.

Workaround: Modify these functions so that they return a (new) field in the superclass and then destroy the subclass

Refactoring Method (v) Re-organization of data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.