Home Backend Development PHP Tutorial Summary of 61 experiences in object-oriented analysis and design of PHP programs_PHP Tutorial

Summary of 61 experiences in object-oriented analysis and design of PHP programs_PHP Tutorial

Jul 21, 2016 pm 03:49 PM
php internal analyze object data of program kind experience design For

(1) All data should be hidden inside the class where it is located.
(2) Users of a class must rely on the shared interface of the class, but a class cannot rely on its users.
(3) Minimize the messages in the class protocol.
(4) Implement the most basic public interface that all classes understand [for example, copy operations (deep copy and shallow copy), equality judgment, correct output content, parsing from ASCII description, etc.].
(5) Do not put implementation details (such as placing private functions with shared code) into the public interface of the class.
If two methods of a class have a common code, then you can create a private function that prevents this common code.
(6) Do not disturb the public interface of a class with things that users cannot use or are not interested in.
(7) There should be zero coupling between classes, or only exported coupling relationships. That is, one class either has nothing to do with another class, or it only uses operations in the public interface of another class.
(8) Classes should only represent one key abstraction.
All classes in the package should be jointly closed for changes in properties of the same class. If a change affects a package, it will affect all classes in the package, but will not have any impact on other packages.
(9) Centralize relevant data and behaviors.
Designers should pay attention to objects that obtain data from other objects through operations such as get. This type of behavior implies that this empirical principle is violated.
(10) Put irrelevant information in another class (that is, the behavior of not communicating with each other).
Move towards stable dependencies.
(11) Make sure that the abstract concepts you model are classes, not just the roles played by objects.
(12) Distribute system functions as uniformly as possible in the horizontal direction, that is: according to the design, top-level classes should share work uniformly.
(13) Do not create omnipotent classes/objects in your system. Be especially careful with classes whose names include Driver, Manager, System, and Susystem.
Plan an interface rather than implement an interface.
(14) Be careful with classes that define a large number of access methods in the public interface. The large number of access methods means that relevant data and behavior are not stored centrally.
(15) Be careful with classes that contain too many behaviors that do not communicate with each other.
Another manifestation of this problem is creating a lot of get and set functions in the public interface of the class in your application.
(16) In an application composed of an object-oriented model that interacts with the user interface, the model should not depend on the interface, but the interface should depend on the model.
(17) Model as much as possible according to the real world (we often violate this principle in order to comply with the principle of system function distribution, avoid the all-purpose class principle, and centrally place related data and behaviors).
(18) Remove unnecessary classes from your design.
Generally speaking, we will downgrade this class to a property.
(19) Remove classes outside the system.
The characteristic of classes outside the system is that in abstract terms they only send messages to the system domain but do not accept messages from other classes in the system domain.
(20)Don’t turn operations into classes. Question any class whose name is a verb or derived from a verb, especially a class with only one meaningful action. Consider whether the meaningful behavior should be moved to a class that already exists or has not yet been discovered.
(21) We often introduce proxy classes when creating analysis models of applications. During the design phase, we often find that many agents are useless and should be removed.
(22) Minimize the number of collaborators of a class.
The number of other classes used by a class should be as small as possible.
(23) Minimize the number of messages passed between classes and collaborators.
(24) Minimize the amount of collaboration between classes and collaborators, that is: reduce the number of different messages passed between classes and collaborators.
(25) Try to reduce the fan-out of the class, that is: reduce the product of the number of messages defined by the class and the number of messages sent.
(26) If a class contains an object of another class, the containing class should send a message to the contained object. That is: an inclusion relation always implies a use relation.
(27) Most methods defined in a class should use most data members most of the time.
(28) The number of objects contained in a class should not exceed the capacity of the developer's short-term memory. This number is often 6.
When a class contains more than 6 data members, you can divide the logically related data members into a group, and then use a new containing class to contain this group of members.
(29) Allow system functions to be distributed vertically in a narrow and deep inheritance system.
(30) When implementing semantic constraints, it is best to implement them according to class definitions. This often leads to class overflow, in which case the constraints should be implemented in the behavior of the class, usually but not necessarily in the constructor.
(31) When implementing semantic constraints in the constructor of a class, place the constraint test in the deepest inclusion level allowed by the constructor domain.
(32) If the semantic information that constraints rely on changes frequently, it is best to put it in a centralized 3rd-party object.
(33) If the semantic information on which a constraint depends rarely changes, it is best distributed among the various classes involved in the constraint.
(34) A class must know what it contains, but it cannot know who contains it.
(35) Objects that share literal scope (that is, are contained by the same class) should not have a usage relationship with each other.
(36) Inheritance should only be used to model specialization hierarchies.
(37) Derived classes must know the base class, and base classes should not know any information about their derived classes.
(38) All data in the base class should be private, do not use protected data.
Class designers should never put things in public interfaces that are not needed by users of the class.
(39) In theory, the inheritance hierarchy should be deeper, the deeper the better.
(40) In practice, the depth of the inheritance hierarchy should not exceed the short-term memory capacity of an average person. A widely accepted depth value is 6.
(41) All abstract classes should be base classes.
(42) All base classes should be abstract classes.
(43) Put commonalities in data, behavior, and/or interfaces as high-end as possible in the inheritance hierarchy.
(44) If two or more classes share common data (but no common behavior), then the common data should be placed in a class that is included in each class that shares this data.
(45) If two or more classes have common data and behavior (that is, methods), then each of these classes should inherit from a common base class that represents these data and methods.
(46) If two or more classes share a common interface (referring to messages, not methods), then they should inherit from a common base class only if they need to be used polymorphically.
(47) The case-by-case analysis of the display of object types is generally wrong. In most such cases, designers should use polymorphism.
(48) Case-by-case analysis of the display of attribute values ​​is often wrong. Classes should be decoupled into an inheritance hierarchy, with each attribute value transformed into a derived class.
(49) Do not model the dynamic semantics of a class through inheritance relationships. Attempting to model dynamic semantics with static semantic relations results in switching types at runtime.
(50)Do not turn class objects into derived classes. Be careful with any derived class that has only one instance.
(51) If you think you need to create a new class at runtime, take a step back and realize that you are creating objects. Now, generalize these objects into a class.
(52) It should be illegal to use an empty method (that is, a method that does nothing) in a derived class to override a method in the base class.
(53) Don’t confuse optional inclusion with the need for inheritance. Modeling optional inclusion as inheritance leads to a proliferation of classes.
(54) When creating inheritance hierarchies, try to create reusable frameworks rather than reusable components.
(55) If you use multiple inheritance in your design, assume you made a mistake. If you didn't make a mistake, you need to try to prove it.
(56) As long as inheritance is used in object-oriented design, ask yourself two questions: (1) Is the derived class a special type of the thing it inherits? (2) Is the base class part of the derived class?
(57) If you find multiple inheritance in an object-oriented design, make sure that no base class is actually a derived class of another base class.
(58) In object-oriented design, if you need to choose between inclusion and association, please choose inclusion.
(59) Do not use global data or global functions for bookkeeping of class objects. Class variables or class methods should be used.
(60) Object-oriented designers should not let physical design principles undermine their logical designs. However, we often use physical design criteria in making decisions about logical design.
(61) Do not bypass the public interface to modify the state of the object.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319598.htmlTechArticle(1) All data should be hidden inside the class where it is located. (2) Users of a class must rely on the shared interface of the class, but a class cannot rely on its users. (3) Minimize the messages in the class protocol...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles