Three main characteristics of object oriented, five principles

Source: Internet
Author: User
Tags closure
It has always been thought that the classes in the program used to encapsulate inheritance polymorphism is object-oriented design, it is not encapsulation, inheritance, polymorphism is only the three main characteristics of object-oriented, but in the design of the program is not to say that the structure of the class used (or reflected) these three features are object-oriented, In fact, the real object-oriented design is to meet the following five principles, object-oriented five basic principles
Single responsibility principle (SRP)
Open closure principle (OCP)
Richter replacement principle (LSP)
Dependency inversion principle (DIP)
Interface isolation principle (ISP) Single duty principle (SRP) • A class should have only one reason to cause it to change (the simplest, easiest to understand, but the least easy to do design principle)
Employee Class Example:
For example, in the staff category, the engineer, sales staff, sales managers These situations are considered in the staff category, the results will be very confusing, under this assumption, every method in the employee class will be if else to determine what kind of situation, from the class structure would be very bloated, and the above three types of staff, Any change in demand will change the staff class. This is not what everyone wants to see.

The Open closure principle (OCP) is both open and closed, open to expansion, and closed to change. Expansion is the expansion of existing modules, when the actual application of our software changes, the emergence of new requirements, we need to expand the module to enable it to meet new requirements.

Change closure means that when we extend a module, we do not need to modify or recompile the source program code and DLL.

This principle is very helpful when we design the class, we must consider the interface encapsulation, abstract mechanism and polymorphic technology as far as possible.


The Richter Replacement principle (LSP) • Subclasses can replace the parent class and appear anywhere the parent can appear • This principle is also in the implementation of GOF advocated interface-oriented programming.
In this principle, the parent class should use interfaces or abstract classes to implement as much as possible.

Subclasses can be used to place the parent class by implementing the parent class interface.
With this principle, our client uses the parent class interface to implement it through subclasses.
It means that we rely on the parent class interface to declare a parent class interface at the client, which is implemented through its subclass
This time the subclass must be able to replace anywhere the parent class appears, and the advantage of this is that it does not affect the current client's use when it expands the new subclass of the parent class interface according to the new requirements.

Dependency inversion (DIP) • Traditional structured programming, the top-level modules are usually dependent on the following sub modules to implement, and
Called upper-level dependency low.
So the dip principle is to reverse this dependency, so that the high-level modules do not rely on low-level modules, so called the dependency inversion principle.

ISP interface Isolation Principle • This principle means that it is much better to use multiple specialized interfaces than to use a single interface.

This I have experience, in my actual programming, in order to reduce the definition of interface, putting many similar methods in one interface, and finally discovering that it takes too much effort to maintain and implement interfaces, and that the operations defined by the interface are equivalent to a commitment to the client, the less the better, the more refined, the better, Too much commitment brings you a lot of energy and time to maintain.

Understanding the three fundamental characteristics of object-oriented is the basis of understanding the five basic principles of object-oriented

Three basic features: encapsulation, inheritance, polymorphism

Encapsulation is to encapsulate objective things into abstract classes, and classes can put their own data and methods only to allow trusted classes or objects to operate, and to hide untrusted information. A class is a logical entity that encapsulates the data and the code that operates it. Inside an object, some code or some data can be private and inaccessible to outsiders. In this way, the object provides different levels of protection for the internal data to prevent accidental changes in the part of the program, or to use the private part of the object incorrectly.

Inheritance, a method that allows an object of a type to obtain properties of another type of object. It supports the concept of classification by level. Inheritance refers to the ability to use all the features of an existing class and to extend them without having to rewrite the original classes. A new class created by inheritance is called a subclass or derived class, and the inherited class is called a base class, parent class, or superclass. The process of inheritance is the process from general to special. To implement inheritance, you can do so by inheriting (inheritance) and combining (composition). The inheritance concept is implemented in two categories: implementation inheritance and interface inheritance. Implementation inheritance is the ability to use the properties and methods of a base class directly without additional coding; Interface inheritance is the ability to use only the names of properties and methods, but subclasses must provide implementations.

Polymorphism means that the same method of a class instance has different representations in different situations. Polymorphic mechanisms enable objects with different internal structures to share the same external interface. This means that, although specific operations are different for different objects, they (those operations) can be invoked in the same way through a common class.

Five basic principles: SPR, OCP, LSP, DIP, ISP

Single Duty principle SRP (Responsibility principle)

Refers to the function of a class to be single, not all-encompassing. As a person, the allocation of work can not be too much, otherwise busy all the way, but not high efficiency.

Open closure principle OCP (Open-close principle)

A module should be open in terms of extensibility and should be closed in terms of modification. For example: A network module, originally only server-side features, and now to join the client function, then you should not modify the service-side function code, you can increase the implementation of the client function code, this requirement at the beginning of the design, it should be the server and the client separate, the public part of the abstraction.

Replacement principle LSP (the Liskov substitution principle LSP)

Subclasses should be able to replace the parent class and appear anywhere the parent class can appear. For example: The company engaged in the annual party, all employees can participate in the lottery, then whether the old staff or new staff, also whether the headquarters staff or the staff outside, should be able to participate in the lottery, otherwise the company will not be harmonious.

Dependency principle Dip (the Dependency inversion principle dip)

Depending on the abstraction, the upper layer relies on the lower layer. Suppose B is a lower module than a, but B needs to use the function of a, at this time, B should not directly use the specific classes in a: Instead, the B defines an abstract interface, and the abstract interface is implemented by a, B uses only this abstract interface: This achieves the purpose of dependency inversion, and B also relieves dependency on a, In turn, a relies on the abstract interface defined by B. It is difficult to avoid relying on the lower modules through the upper module, if B also directly depends on the implementation of a, then can cause circular dependency. A common problem is that when compiling a module, you need to include a CPP file directly to the B module, while compiling B is also included directly in a CPP file.

Interface separation principle ISP (the Interface segregation principle ISP)

Modules are separated by an abstract interface, rather than by a specific class strong coupling.



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.