PHP "refactoring-improving the design of existing code" three _php techniques of data re-organization

Source: Internet
Author: User
Mind Mapping


Introduced

To undertake the previous PHP chat "refactoring-improve the design of the existing code" to rearrange your function to continue refactoring aspects of the content.
This chapter mainly aims at the reconstruction of data.
1. The voice of contention--direct access to field or via function (accessor) Access field

2. Modify array As object: When you see an array much like a data structure, you can use the Replace array with object to turn an array into an object. --the data structure is clearer.

Professional terminology

Accessor: Visitor, memory--translated as "function" in this article

Dumb: Dumb

Domain class: Used to handle business logic

Presentation class: For dealing with "data representations"

Business logic: Business logic

Unidirectional: One-way

Bidirectional: Bi-directional

Collection: Clustering

Self Encapsulate Field
Status: If the client directly accesses the domain, it causes the coupling between the client and the domain to become unwieldy, then the value/Set function is established for this range and is accessed only by these functions.

Motivation:

Indirect access variable: Supports more flexible data acquisition methods, such as lazy initialization (meaning that it is initialized only when a value is used.) )

"Direct Access variable": code is easier to read, no need to stop to say: "Ah, this is just a value function."

Choice: 1, code specifications, according to the majority of the team's practice to do.

2, the individual prefers "direct access variables" until this way to bring trouble.

Example of Martin (author): You want to get field in superclass, but you want to change the field to a calculated value in subclass, which is the most use of the self encapsulate Field.

My own example: I usually set the field to private, and if the external variable requires this field, I will use self encapsulate field. Or when the value of the field changes, use self encapsulate Field.


Replace Data Value with Object
Status: If you have a basic type of field that requires additional data and behavior, then turn this field into an object.

motivation:

In the early stages of development, we might use basic data types to represent simple behavior. For example, you might use a string to represent a phone number, but then you might get a special behavior such as "format", "Validate", and "extract area code" for the phone number. That's when we need a new class.

Replace Array with Object
Condition: You have an array, and the elements in the array each represent something different, then replace the array with an object, representing each element of the array in a range of domain.

Motivation:

Arrays are commonly used in a set 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 a person's name. Objects are different and can convey such information through the domain name and function name. This does not need to be rote, without comment.

Encapsulate Field
Status: If you have a public domain in your class, declare it as pirvate and provide the appropriate access function.

motivation:

One of the object-oriented principles is encapsulation (encapsulate) or "data hiding." According to this test, you should never declare the data public.
--public data is seen as a bad practice.
--if encapsulated, the code will be easier to modify because it is concentrated in one place.
A function does not provide other behavior except the Access function (getting/setting), it is ultimately just a dumb class (dummy). Such classes do not have the advantage of object technology. --the way to solve dumb classes is to move methods to nudge them to new objects briskly.
 conclusion
 
I hope to share what I understand with you and welcome your valuable suggestions.

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.