Five principles of object-oriented three characteristics + low coupling high cohesion

Source: Internet
Author: User

Five principles of object-oriented three characteristics + low coupling high cohesion

The three main characteristics of object-oriented are "encapsulation," polymorphism "and" inheritance ", the Five Principles are" single duty Principle "," open closure principle "," Richter substitution Principle "," dependency inversion principle "and" Interface Separation principle ".

What is object-oriented

Object Oriented,oo is a software development method. Object-oriented concepts and applications have gone beyond programming and software development to extend to such fields as database system, interactive interface, application structure, application platform, distributed system, network management structure, CAD technology, artificial intelligence and so on. Object-Oriented is a method of understanding and abstraction of the real world, and it is the product of the development of computer programming technology [1] to a certain stage.

Here's an example of programming with PHP's OOP.

Three basic features: encapsulation, inheritance, polymorphic encapsulation

Encapsulation is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding. A class is a logical entity that encapsulates the data and the code that operates the data. Within an object, some code or some data can be private and cannot be accessed by the outside world. In this way, objects provide different levels of protection to internal data to prevent unintended changes to unrelated parts of the program or to use the private parts of the object incorrectly.

Inherited

Inheritance, a method that allows an object of a type to obtain the properties of another type of object. It supports the concept of classification by class. Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class. A new class created through inheritance is called a subclass or derived class, and the inherited class is called the base class, parent class, or superclass. The process of inheritance is from the general to the special process. To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition). The concept of inheritance is implemented in two categories: implementation inheritance and interface inheritance. Implementation inheritance refers to the ability to directly use the properties and methods of the base class without additional coding; Interface inheritance is the ability to use only the names of properties and methods, but the subclasses must provide the implementation.

Polymorphic

Polymorphic means that the same method of a class instance has different manifestations in different situations. Polymorphic mechanisms enable objects with different internal structures to share the same external interface. This means that although specific actions are different for different objects, they can be called 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. Like a person, the distribution of work can not be too much, otherwise busy all the way, but the efficiency is not high up.

Open closure principle OCP (Open-close Principle)

A module should be open in terms of extensibility and should be closed in terms of change. For example: A network module, the original only server function, and now to join the client function, then you should not modify the server function code under the premise, you can add the client function of the implementation code, which requires at the beginning of the design, the server should be separated from the client, the public part of the abstraction.

In-place 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 annual party, all employees can participate in the lottery, then whether it is the old staff or new employees, whether it is the headquarters staff or the outside staff, should be able to participate in the lottery, or the company will not be harmonious.

Dependency Inversion 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, but B needs to use the function of a, at this time, B should not directly use the specific Class A: Instead, it should be defined by B an abstract interface, and a to implement the abstract interface, b only use this abstract interface: this is to achieve the purpose of dependency inversion, B also lifted the dependence on a, In turn, a relies on the abstract interface defined by B. It is difficult to avoid relying on the lower module through the upper module, if B is also directly dependent on the implementation of a, then it may cause cyclic dependence. A common problem is that when compiling a module, you need to include the CPP file directly into the B module, while compiling B will also include the CPP file of a directly.

Interface separation principle ISP (the Interface segregation Principle ISP)

The modules are separated by an abstract interface, rather than by a specific class-strong coupling.

Coupling

To put it simply, the coupling between objects in software engineering is the dependence between objects. The main problem in guiding the use and maintenance of objects is the multiple dependencies between objects. The higher the coupling between objects, the higher the maintenance cost. Therefore, the design of the object should minimize the coupling between the class and the component.

There is a coupling between hardware and software, and the coupling between each module.
Coupling is a measure of how each module in a program structure is interrelated. It depends on the complexity of the interface between each module, how the module is called, and what information passes through the interface.

Coupling can be divided into the following types, the coupling between them from high to low in the following:

    • Content coupling. This coupling is known as content coupling when one module modifies or operates the data of another module directly, or when one module does not pass through a normal entry to another module. Content coupling is the highest degree of coupling and should be avoided.
    • Public coupling. Two or more than two modules collectively refer to a global data item, which is called public coupling. In structures with a large number of public coupling, it is difficult to determine exactly which module is assigning a specific value to a global variable.
    • External coupling. A set of modules accesses the same global simple variable instead of the same global data structure, and it is not the information of the global variable passed through the parameter table, it is called an external coupling.
    • Control coupling. A module transmits a control signal to another module through an interface, and the module that receives the signal carries out the appropriate action according to the signal value, which is called control coupling.
    • Tag coupling. If a module a passes a common parameter to two modules B and C through an interface, then there is a tag coupling between module B and C.
    • Data coupling. Data is passed between modules through parameters, then it is called data coupling. Data coupling is the lowest form of coupling, and this type of coupling is generally present in the system, because in order to accomplish some meaningful functions, the output data of some modules is often needed as input data of other modules.
    • Non-direct coupling. There is no direct relationship between the two modules, and the connection between them is fully realized through the control and invocation of the main module.

Summarize
Coupling is an important factor affecting software complexity and design quality, we should adopt the following principles: if there must be coupling between the modules, we should use data coupling, less control coupling, limit the scope of public coupling, and avoid using content coupling as far as possible.

Cohesion and coupling

The cohesion marks the degree of cohesion of each element within a module, which is a natural extension of the concept of information concealment and localization. Cohesion is the measurement of the connection within the module from a functional point of view, and a good cohesive module should do exactly one thing. It describes the functional linkages within the module.
Coupling is a measure of the connection between modules in a software structure, and the coupling strength depends on the complexity of the interface between the modules, the points entering or accessing a module, and the data through the interface. The program stresses low coupling, high cohesion. Is that the elements within the same module are highly dense, but the interdependence between the modules is less dense.
Cohesion and coupling are closely related, and modules that have high coupling with other modules mean low cohesion, while the high cohesion module implies a low coupling between the module and other modules. In the software design, we should strive to achieve high cohesion, low coupling.

Five principles of object-oriented three characteristics + low coupling high cohesion

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.