Design principles of design patterns (Java)

Source: Internet
Author: User

Design principles include:

1. Single responsibility principle,

2. Lee's replacement principle,

3. Dependency inversion principle,

4. Interface isolation principle,

5. The dimit law,

6. Open and Close principles.


1. Single Responsibility PrincipleSingle Responsibility Principle, SRP ):

There shoshould never be more than one reason for a class to change.

For a class, focus only on one thing and only one cause of change.

My own understanding is: A class needs to do exactly one thing it can do. Not many things require this class to complete. If you need to do other things, then create another class.

Advantages:

A. Reduce class accountability;

B. improve readability;

C. Improve code maintainability and reusability;

D. Reduce risks caused by changes.


2. Lee's replacement principleLiskov Substitution Principle, LSP ):

The following describes the advantages and disadvantages of inheritance:

Advantages:

Code sharing reduces the workload of creating classes. each subclass has all attributes and methods of the parent class;

Improve code reusability;

Improve code scalability;

Improve the openness of products or projects.

Disadvantages:

Inheritance is intrusive and has attributes and methods of the parent class;

To reduce code flexibility, you must have attributes and methods of the parent class;

To enhance coupling, child classes must be considered when parent class attributes or methods are changed.

To sum up, the inherited "benefits" are greater than "disadvantages". Now, the "Lee's replacement principle" is used to reduce the troubles caused by "disadvantages.


There are two definitions of the Lee's replacement principle:

①: If the object o1 of the S type has the object o2 of the T type, replace the program P defined by S

If the behavior of program P does not change, T is the child type of S.

②: All references to the base class must use its subclass objects transparently.


The design modes that embody the Lee's replacement principles include: policy mode, combination mode, and Agent Mode.

3. Dependency inversion principleDependency Inversion Principle, DIP)

Interface-oriented programming-OOD.

The high-level module should not depend on the underlying module, both of which depend on its abstraction; abstraction does not depend on details; details should depend on abstraction.

The dependency inversion principle is the basic principle behind the design model of components such as JavaBean, EJB, and COM.

NatureAbstract interfaces or abstract classes are used to make the implementation of each class or module independent from each other, so as to achieve loose coupling between modules.

Rules Using this principle:

① Each class, interface or abstract class; ② the expression type, interface or abstract class of the variable; ③ the class is not derived from the specific class; ④ No rewriting

Base class method; ⑤ combined with the Lee's replacement principle.

The dependency inversion principle has obvious advantages in the embodiment of large projects.


4. Interface isolation principleInterface Segregation Principle, ISP)

Two TypesDefinition:

① The client should not rely on interfaces not required by the client;

② The dependency between classes should be built on the smallest interface.

Description: A. The dependence of a class on another class should be based on the smallest interface;

B. An interface represents a role and should not be handed over to an interface to those different roles;

C. Customers should not be forced to rely on methods they do not need.


5. dimit's LawLaw of Demeter, dump)

Also called: Least Knowledge Principle, LKP)

A class needs to call a method of another class, which can be forwarded by a third party.

Reduces the relationship between classes, reduces the coupling between classes, and improves the class reuse rate.

The design patterns use the Demeter rules: appearance patterns, intermediary patterns.


6. Open and Close principlesOpen-Closed Principle, OCP)

In object-oriented programming, the open and closed principle is the most basic principle and plays a general guiding role. Other principles are the hands of the open and closed principle.

And tools.

①: Improves reusability;

②: Improves maintainability;

③: Improve flexibility;

④: Easy to test;

This article from the "Long Road, very short" blog, please be sure to keep this source http://10000001.blog.51cto.com/4600383/1297349

Related Article

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.