Mind Map
Introduction
Undertake the above PHP miscellaneous on restructuring-improving the design of existing code re-organize your function to continue refactoring. This chapter mainly focuses on Data Reconstruction. 1. argument: directly accessing the Field or accessing the Field through the function (Accessor)
2. Modify Array to Object: When you see an Array is similar to a data structure, you can use Replace Array with Object to convert Array into an Object. -- The data structure is clearer.
Terminology
Accessor: visitor, memory -- translated as "function" in this article"
Dumb: dumb
Domain class: used to process business logic
Presentation class: used to process "Data Representation"
Business logic: business logic
Unidirectional: unidirectional
Bidirectional: bidirectional
Collection: Cluster
Self Encapsulate Field: if the Client directly accesses the value Field, the coupling between the Client and the value Field will gradually become clumsy. Then, a value/setting function is created for the value Field, and only use these functions for access.
Motivation:
"Indirect access variable": supports more flexible data acquisition methods, such as lazy Initialization (meaning it is initialized only when a value is used .)
"Direct access to variables": the code is easier to read and does not need to be stopped. "Ah, this is just a value function ".
Choice: 1. Code specification, according to the practices of most people in the team.
2. individuals prefer "directly accessing variables" until this method brings trouble.
Example of martin (author): If you want to obtain the field in superclass, but want to change the field to the calculated value in subclass, Self Encapsulate Field should be used most.
My own example: I usually set the field to private. If the external variable needs to use this field, I will use Self Encapsulate Field. Or when the field value changes, use Self Encapsulate Field.
Replace Data Value with Object status: If you need additional Data and behavior for a basic type of field, convert this field into an Object.
Motivation:
At the initial stage of development, we may use basic data types to represent simple behaviors. For example, you may use a string to represent the phone number, but then there may be special behaviors such as "format", "verify", "extract area code" of the phone number. -- In this case, we need a new class.
Replace Array with Object: if you have an Array where each element represents something different, Replace the Array with an Object. Each element in the Array is represented by a value field.
Motivation:
Arrays are commonly used in a group of similar objects. If the elements in the array are different, it is difficult to understand that the first element in the array is a convention such as the person name. The object is different. You can use the value field name and function name to convey such information. -- In this way, you do not need to note it.
Encapsulate Field condition: If your class has a public value, declare it as pirvate and provide the corresponding access function.
Motivation:
One of the object-oriented principles is encapsulation (Encapsulate) or "data hiding". According to the original test, you should never declare the data as public. -- Public data is regarded as a bad practice. -- If it is encapsulated, the code modification will be relatively simple, because it is concentrated in one place. In addition to getting/setting, a function does not provide other behaviors. It is only a dumb class (dummy class ). This type of class cannot obtain the advantages of object technology. -- The solution to the dumb class is to Move the methods bridly to the new object. Conclusion I hope to share with you what I understand and welcome your valuable comments.