PHP "refactoring-improving the design of the existing code" three reorganize data _php tutorial

Source: Internet
Author: User
Mind Mapping


Introduced

Take the following PHP talk about refactoring-improving the design of existing code to reorganize your function to continue refactoring. This chapter focuses on the reconstruction of data. 1. The voice of controversy-access field directly or through a function (Accessor)

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

Professional terminology

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

Dumb: Mute

Domain class: Used to process business logic

Presentation class: Used to process "data representations"

Business logic: Operational logics

Unidirectional: one-way

Bidirectional: Two-way

Collection: Cluster

Self encapsulate Field Condition: If the client accesses the domain directly, it will cause the coupling between the client and the range to become clumsy, then set the value/Set function for this range and access it only with 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 to variables": The code is easier to read, no need to stop and say: "Ah, this is just a value function."

Select: 1, code specification, according to the practice of most people in the team to do.

2, individuals prefer to "direct access to variables" until this way brings trouble.

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

My own example: I usually set the field to private, if the external variables, need to use this field, I will use the Self encapsulate field. Or when the value of field changes, use Self encapsulate field.


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

Motive:

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 you might then have 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, the elements in the array represent different things, then replace the array with the object, and for each element in the array, it is represented by a range of domains.

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 contract with 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, no annotations.

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

Motive:

One of the object-oriented principles is encapsulation (encapsulate) or "data hiding."  According to this original test, you should never declare the data as public.  --public data is considered a bad practice. -If encapsulated, the code will be easier to modify because it's all in one place. A function, in addition to the Access function (getting/setting), does not provide any other behavior, it is only a dumb class (dumb Class). This class does not gain the advantage of object technology. --the way to solve dumb classes is to move them to new objects briskly. Conclusion I hope to be able to share what I understand with you, and welcome your valuable suggestions.

http://www.bkjia.com/PHPjc/325369.html www.bkjia.com true http://www.bkjia.com/PHPjc/325369.html techarticle Mind Map Introduction to the above-mentioned PHP "refactoring-improve the design of existing code" to reorganize your function to continue refactoring content. This chapter focuses on the reconstruction of data. ...

  • Related Article

    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.