Object-oriented programming in five basic principles

1, single responsibility principle SRP (Single Responsibility Principle)

It refers to a class of functions to be single, not all-inclusive. As a people, the distribution of work can not be too much, otherwise all day long, although busy, but the efficiency is not high up.

2, the open closed principle OCP (Open-Close Principle)

A module in scalability should be open in terms of changes it should be closed. For example: a network module, only the original server function, and now want to join the client function, then it should be in the code without modifying the server function of the premise, it is possible to increase the code that implements the client function, which requires early in the design, They should be separated from the server and the client, the public part of the abstract.

3, substitution principle (the Liskov Substitution Principle LSP)

Subclasses should be able to replace the parent class and appear anywhere in the parent class can appear. For example: the company engage in an annual party, all employees can participate in the drawing, so whether it is new or old staff employees, regardless of headquarters employees or expatriates, all should be able to participate in the drawing, otherwise the company would discord.

4, depending on the principle (the Dependency Inversion Principle DIP) particular abstract dependent, dependent on the lower layer an upper layer. Suppose B is lower than the module A, B but need to use the function A, this time, B should not be used directly in the concrete class A: and should define an abstract interface consisting of B, A to achieve this by the abstract interface, B using only the abstract interface: this achieves the object dependency inversion, B also lifted dependence of a, which in turn is dependent on the definition of a B abstract interface. Upper module by the lower module is difficult to avoid dependence, if B is also directly dependent on implement A, then it may result in a circular dependency. A common problem is the need to be included when compiling A module directly to cpp file module B, and B is also to be included when compiling cpp file directly to the A's.

If you are interested in C C / ++, you can add about our study and exchange Q group: 637 935 295, receive a free set of learning materials and video courses yo ~

5, the principle of separation of the interface (the Interface Segregation Principle ISP)

Abstract interface between the modules through isolated, rather than through a specific class of strong coupling together

Object-oriented programming in five basic principles

Published 177 original articles · won praise 22 · views 7951

Guess you like

Origin blog.csdn.net/weixin_45820912/article/details/104070972