PHP miscellaneous refactoring-design for improving existing code 3 reorganizing data PHP miscellaneous refactoring-design for improving existing code-reorganizing your function

Source: Internet
Author: User
Mind Map

This article introduces how to re-organize your function to continue refactoring by taking on the above PHP miscellaneous "refactoring-improving the design of existing code. 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 ":CodeIt is easy to read, and you don't need to stop and say, "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.

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.