PHP Object oriented programming 61 Golden rule

 

You don't have to stick to these principles , Violation of them will not be subject to religious penalty . But you should think of these principles as alarm bells , If one of them is violated , Then the alarm will ring . ----- Arthur J.Riel

(1) All data should be hidden inside the class in which it is located .

(2) Users of a class must rely on the common interface of the class , But a class cannot rely on its users .

(3) Minimize messages in class protocols .

(4) Implement the most basic public interface that all classes understand [ for example , Copy operation ( Deep copy and light copy )、 Equality judgment 、 Output content correctly 、 from ASCII Description analysis and so on ].

(5) Don't take the implementation details ( For example, private functions that place common code ) Put it in the public interface of the class .

If two methods of a class have a piece of common code , Then you can create a private function that prevents this common code .

(6) Don't disturb the public interface of a class with something that users can't use or are not interested in .

(7) There should be zero coupling between classes , Or only export coupling relations . That is to say , One class either has nothing to do with another , Or just use the operations in the public interface of another class .

(8) Classes should represent only one key abstraction .

All classes in a package should be closed together for changes in properties of the same class . If a change affects a package , Will affect all classes in the package , And not for other bags Cause any effect .

(9) Put relevant data and behaviors together .

Designers should pay attention to those who pass get Objects that get data from other objects . This type of behavior implies that this principle of experience has been violated .

(10) Put irrelevant information in another class ( That is to say : The act of not communicating with each other ).

Rely on stability .

(11) Make sure that the abstraction you model for is the class , It's not just the role of the object .

(12) Distribute system functions as uniformly as possible in the horizontal direction , That is to say : according to the design , Top level classes should share work uniformly .

(13) Don't create omnipotent classes in your system / object . Yes, the name contains Driver、Manager、System、Susystem You should be very careful with the class .

Plan an interface instead of implementing an interface .

(14) Be careful with classes that have a large number of access methods defined in the common interface . A large number of access methods means that relevant data and behaviors are not stored centrally .

(15) Be careful with classes that contain too many uncommunicative behaviors .

Another manifestation of this problem is that you create a lot of... In the public interfaces of classes in your application get and set function .

(16) In an application made up of an object-oriented model that interacts with the user interface , The model should not depend on the interface , The interface should depend on the model .

(17) Model as much as possible in the real world ( We often follow the system function distribution principle 、 Avoid violating the omnipotent principle and the principle of centralizing relevant data and behaviors This principle ) .

(18) Remove unnecessary classes from your design .

Generally speaking , We'll demote this class to a property .

(19) Remove classes outside the system .

The characteristics of classes outside the system are , Abstractly, they only send messages to the system domain, but they don't 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 word , In particular, there is only one class of meaningful behavior . Consider that the meaningful behavior is No, it should be migrated to a class that already exists or has not been found .

(21) We often introduce proxy classes when we create analysis models for applications . In the design stage , We often find that many agents are useless , Should be removed .

(22) Minimize the number of collaborators for the class .

One class should use as few other classes 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 to say : Reduce the number of different messages passed between classes and collaborators .

(25) Minimize the fan out of classes , That is to say : Reduce the product of the number of messages defined by the class and the number of messages sent .

(26) If a class contains objects from another class , Then the containing class should send a message to the contained object . That is to say : Including relationships always means using relationships .

(27) Most methods defined in a class should use most data members most of the time .

(28) Class should not contain more objects than the developer's short-term memory . This number is often 6.

When a class contains more than 6 When there are two data members , You can divide logically related data members into a group , Then use a new include class to include this group of members .

(29) Vertical distribution of system functions in a narrow and deep inheritance system .

(30) When implementing semantic constraints , It's best to implement it according to the class definition . This often leads to a flood of classes , under these circumstances , Constraints should be implemented in the behavior of the class , Usually in Constructor , But it doesn't have to be .

(31) When implementing semantic constraints in the constructor of a class , Place Constraint testing in the deepest level of inclusion allowed in the constructor domain .

(32) If the semantic information on which the constraint depends changes frequently , Well, it's better to put it in a centralized second 3 In square object .

(33) If the semantic information on which the constraint depends is rarely changed , So it's better to distribute it among the classes involved in the constraint .

(34) Class must know what it contains , But you don't know who contains it .

(35) Sharing literal scope ( That is, it is contained in the same class ) There should be no usage relationship between objects of .

(36) Inheritance should only be used to model specialized hierarchies .

(37) Derived classes must know the base class , Base classes should not know anything about their derived classes .

(38) All data in the base class should be private , Don't use protected data .

Class designers should never put things that users of a class don't need in a public interface .

(39) In theory , The hierarchy of inheritance should be deeper , The deeper the better .

(40) In practice , The depth of inheritance hierarchy should not exceed the short-term memory ability of an ordinary person . A widely accepted depth value is 6.

(41) All abstract classes should be base classes .

(42) All base classes should be abstract .

(43) The data 、 Behavior and / Or the commonness of interfaces should be put at the high end of inheritance hierarchy as far as possible .

(44) If two or more classes share common data ( But there's no public behavior ), So we should put the public data in a class , Every class that shares this data contains this class .

(45) If two or more classes share data and behavior ( That's the way. ), Then each of these classes should inherit from a common base class that represents the data and methods .

(46) If two or more classes share a common interface ( It means news , Not the way ), So only when they need to be polymorphic , They should start from a common base class Inherit .

(47) It is generally wrong to analyze the display of object types according to different situations . In most cases , Designers should use polymorphism .

(48) It is often wrong to analyze the display of attribute values according to different situations . Classes should be decoupled into an inheritance hierarchy , Each property value is transformed into a derived class .

(49) Don't model the dynamic semantics of a class through inheritance relationships . Trying to model dynamic semantics with static semantic relationships leads to type switching at run time .

(50) Don't make the object of a class a derived class . Be careful with any derived class that has only one instance .

(51) If you feel the need to create new classes at runtime , So step back and recognize that you're creating objects . Now? , Generalize these objects into a class .

(52) Use empty methods in derived classes ( That's the way to do nothing ) To override a method in a base class should be illegal .

(53) Don't confuse optional inclusion with the need for inheritance . Modeling optional inclusions as classes with inheritance can lead to flooding .

(54) When creating an inheritance hierarchy , Try to create reusable frameworks , Instead of reusable components .

(55) If you use multiple inheritance in your design , Suppose you make a mistake . If you don't make a mistake , You need to try to prove .

(56) As long as inheritance is used in object-oriented design , Ask yourself two questions :(1) Whether a derived class is a special type of the thing it inherits ?(2) Whether a base class is part of a derived class ?

(57) If you find multiple inheritance relationships in an object-oriented design , Make sure that no base class is actually a derivative of another base class .

(58) In object-oriented design, if you need to choose between inclusion and Association , Please select include relationship .

(59) Don't use global data or global functions for the 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 design . however , We often use physical design criteria in making decisions about logical design .

(61) Don't bypass the public interface to modify the state of an object .

PHP Object oriented programming 61 More articles on the golden rule

  1. JavaSE-12 Several basic principles of object-oriented programming

    Extract the changing behavior in the code , Form an interface When designing base classes , If the implementation of a member method in a subclass of this class changes a lot ( A part of the molecular class implements the same method ), There are two problems with being a base class : One is that the method is no longer universal : Second, if subclasses override this method , There is ...

  2. Zasipingcuo 201571030332 《 Object oriented programming (java) Course progress bar 》

    <2019 Object oriented programming (java) Course progress bar > Week ( read / To write ) Lines of code Blog posts / Number of blogs commenting on others After school study time ( Hours ) Learn the most rewarding program Reading or compiling makes me The first week 20/ ...

  3. 2019 Object oriented programming (java) Course progress bar

    2019 Object oriented programming (java) Course progress bar Week ( read / To write ) Lines of code Blog posts / Number of blogs commenting on others After school study time ( Hours ) Learn to get the most out of program reading or programming tasks 1 20/10 1/0 5 Nine nine ...

  4. 201871010106- Ding Xuanyuan 《2019 Object oriented programming (java) Course progress bar 》

    <2019 Object oriented programming (java) Course progress bar > Week ( read / To write ) Lines of code Blog posts / Number of blogs commenting on others After school study time ( Hours ) Learn to get the most out of program reading or programming tasks 1 25/10 ...

  5. 201871010135 Zhang Yujing 《2019 Object oriented programming (java) Course progress bar 》

    <2019 Object oriented programming (java) Course progress bar > Week ( read / To write ) Lines of code Blog posts / Number of blogs commenting on others After school study time ( Hours ) Learn the most rewarding program Reading or compiling makes me The first week 25/ ...

  6. 201871020225- Mou Xingyuan 《 Object oriented programming (java)》 Course progress bar

    <2019 Object oriented programming (java) Course progress bar > Week ( read / To write ) Lines of code Blog posts / Number of blogs commenting on others After school study time ( Hours ) Learn the most rewarding program Reading or compiling makes me The first week 25/ ...

  7. # 61 An empirical principle of object-oriented design -《OOD Kai Si Lu 》Arthur J.Riel

    61 An empirical principle of object-oriented design -<OOD Kai Si Lu >Arthur J.Riel original text http://blog.csdn.net/cpluser/article/details/129291 61 ...

  8. 201771010135 Yang Rongqing 《2018 Object oriented programming (java) Course progress bar 》

                                                                                                        ...

  9. Java Object oriented programming -- And C++ Contrast notes : series 3(Java Inheritance mechanism )

    Inherit (inheritance) The core idea behind it is :       bonus = b;    }      } Java Didn't like C++ That provides multiple inheritance mechanisms , But it provides an interface mechanism , Later, we will explore the implementation of interface mechanism in detail ...

Random recommendation

  1. ReactNative And NativeScript Comparison report

    All in all, this period of time is very important for ReactNative( Hereinafter referred to as RN) and NativeScript( Hereinafter referred to as NS) Simple learning to understand , Analyze and compare the advantages and disadvantages of the two from different aspects . Page structure NS The directory structure of a page : RN A page of is usually a ...

  2. reshape2 package --R Efficient data processing package

    How to use reshape2 Packages transform wide data into growing data , Convert long data to wide data .Reshape2 yes Hadley Wickham Developed and maintained . 1. Long data VS Wide data Wide data : Each column represents a different variable . ...

  3. TextView Achieve rounded corners

    Customize a Xml style : <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android ...

  4. summary Selenium Automated test methods ( 5、 ... and ) Automated testing framework

    5、 ... and . Automated testing framework 1. Unit test framework unittest class loginTests(unittest.TestCase): ① The initialization part of the beginning @classmethod def setUpClas ...

  5. iOS Development of Runloop( turn )

    Objective-C And run loop Detailed explanation author :wangzz Original address :http://blog.csdn.net/wzzvictory/article/details/9237973 Please indicate the reprint ...

  6. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  7. PL/SQL“ ORA-14551: Cannot execute in query DML operation ” solve

    Environmental Science Oracle 11.2.0 + SQL Plus problem Write the function as follows : take scott.emp The wages of employees whose wages are lower than the average in the table plus 200, And return the total number of people whose salary has been modified .PL/SQL There are update operations in , ...

  8. Node.js Path modular

    Node.js path Module provides some small tools for processing file paths , We can introduce the module in the following ways : var path = require("path") Method Serial number Method & ...

  9. intelij IDEA Crack

    Crack intelij IDEA See the link :https://blog.csdn.net/weixin_37937646/article/details/79119540

  10. python3+ Regular expression crawling The cat's eye film

    '''Request+ Regular expressions grab cat's eye movies TOP100 Content ''' import requests from requests.exceptions import RequestException i ...