Object-Oriented design principles

Source: Internet
Author: User

in the use of object-oriented thinking system design, the predecessors summed up a total of 7 principles, respectively, are:

Single duty principle, open and close principle, Richter replacement principle, Dependency injection principle, interface separation principle, Dimitri principle and priority use combination rather than inheritance principle.

1. Single responsibility principle (SRP)     The core idea of a single responsibility principle is that every object in the system should have only one separate responsibility, and all objects are concerned with the completion of their own duties. Its English abbreviation is the SRP, the English name is single Responsibility Principle. In fact, the single principle of responsibility means that developers often say "high cohesion, low coupling." In other words, each class should have only one responsibility, and only one function can be provided externally, and the reason for the class change should be only a single cause. In design mode, all design patterns follow this principle.   2. Opening and closing principle (OCP)     The core idea of the open-close principle is that an object is opened for expansion and closed for modification. Its abbreviation is OCP, the English name is open for extension,closed for modification. In fact, the opening and closing principle means that changes to the class are made by adding code rather than altering the existing code. In other words, once the software developer writes out the code that can be run, it should not change it, but to ensure that it can continue to run, how to do this? This requires the help of abstraction and polymorphism, that is, to abstract the content of the possible changes, so that the abstract part is relatively stable, and the concrete implementation layer can be changed and extended.   3. Richter replacement principle (LSP)   The core idea of the Richter substitution principle is that it can be substituted with its subclasses wherever the parent class appears. Its English abbreviation is LSP, English full name is Liskov Substitution Principle. In fact, the Richter substitution principle means that objects in the same inheritance system should have common behavioral characteristics. The principle of substitution of the Richter scale is concerned with how to use inheritance well, that is, not abusing inheritance, which is the cornerstone of inheritance reuse.   4. Dependency Injection principle (DIP)     The core idea of the dependency injection principle is to rely on abstraction and not rely on concrete implementations. Its English abbreviation is dip, English full name is dependence inversion Principle. in fact, the principle of dependency injection is that all classes in an application, if used or dependent on other classes, should rely on the abstract classes of these other classes, rather than the concrete implementation classes of those other classes. The level of abstraction should not depend on the specific implementation details, so as to ensure the reusability and maintainability of the system. In order to implement this principle, developers are required to program for the interface programming, not for implementation.   5. Interface separation principle (ISP)     The core idea of the interface separation principle is that the client program should not be forced to rely on methods that they do not need to use. Its English abbreviation is ISP, English full name is interface segregation Principle. in fact, the interface separation principle means: an interface does not need to provide too much behavior, an interface should only provide an external function, should not be all operations are encapsulated in an interface.   6. Dimitri Principles (LOD)     The core idea of the Dimitri principle is that one object should know as little as possible about other objects. Its English abbreviation is LOD, the English full name is Law of Demeter. in fact, Dimitri principle means: Reduce the coupling between the various objects, improve the maintainability of the system. Between the modules, should only communicate through the interface, regardless of the internal workings of the module, it can make each module to minimize the degree of coupling, promote the reuse of software.   7. Prioritize use of combinations rather than inheritance principles (CARP)     The core idea of using a combination rather than a succession principle is to prioritize the use of composition rather than inheritance. Its English abbreviation is carp, the English full name is composite/aggregate reuse Principle. In fact, the precedence of the combination rather than the principle of inheritance means that when reusing objects, it is preferable to use combinations rather than inheritance, because any change to the parent class may affect the behavior of the subclass when using inheritance, and when using the combination, is defined dynamically at run time by obtaining a reference to other objects, helping to maintain a single responsibility principle for each class.

Object-Oriented design principles

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.