Seven Principles of design patterns

Source: Internet
Author: User

Single Duty principle (Responsibility Principle)

Each object in the system should have only one separate responsibility, and all objects are concerned with the completion of their own duties.

A reasonable class provides only one function to the outside, and the cause of the class change should be only one

Richter Replacement principle (Liskov Substitution Principle)

Wherever the parent class appears, it can be replaced with its subclasses.

Objects in the same inheritance system should have common behavioral characteristics

The Richter substitution principle focuses on how to use inheritance well, that is, not abusing inheritance, which is the cornerstone of inheritance reuse.

Dependency Injection principle (dependence inversion Principle)

In an application, all classes, if used or dependent on other classes, should rely on the abstract classes of these other classes instead of the specific implementation classes of those other classes

The level of abstraction should not depend on specific implementation details to ensure system reusability and maintainability

The essence of the dependency injection principle is to make the implementation of each class or module independent of each other by abstraction (abstract class or pretext), and to achieve loose coupling between modules.

Interface separation principle (Interface segregation Principle)

An interface does not need to provide too much behavior, it should only provide an external function (the interface here is not only the interface defined by the interface keyword, but also the object interface, such as phone phone = new phone ())

Interface separation principle requires that in a module should only rely on the interface it requires, which requires the design of the interface should be as thin as possible

The principle of interface separation is somewhat similar to that of a single duty principle, except that the principle of single responsibility requires a single function of class and interface, the responsibility is the division of Business logic, and the interface separation principle requires that the method of interface is as few as possible, which is as useful as possible for a module.

Dimitri Principles (Law of Demeter)

An object should know as little as possible about other objects, which means reducing coupling between objects and improving maintainability of the system

Between the modules, should only communicate through the interface, regardless of the internal workings of the module, it can minimize the coupling of each module, promote the reuse of software

Opening and closing principle (Open for extension,closed for modification)

Object should be open for extension, closed for modification

Changes to the class are made by adding code instead of altering the existing code

In other words, once the developer writes out the code that can be run, it should not change it, it needs to be implemented by abstraction and polymorphism, that is, to abstract the possible changes, so that the abstract part is relatively stable, and the concrete implementation can be changed and extended

Seven Principles of design patterns

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.