Design Pattern-six design principles

Source: Internet
Author: User

1. Single Responsibility Principle
The English name of a single responsibility principle is single responsibility principle (SRP.
A single responsibility is defined as: There is only one reason for a change.
A single responsibility principle requires that one interface or class has only one reason for change, that isAn interface or class has only one responsibility and is responsible for one thing..
The suggestion is: the interface must have a single responsibility, and the class world should be changed for only one reason.
2. Lee's replacement principle
The English name of the Rys replacement principle is liskov substitution principle (LSP.
The definition of the Rys replacement principle: All references to the base class must be transparent to use subclass objects. In layman's terms,Any child class that can appear in the parent class can appear.And replacing it with a subclass does not produce any errors or exceptions.
The definition of RYS replacement includes the following four meanings:
(1) subclass must fully implement the parent class Method
(2) subclass can have its own personality
(3) When overwriting or implementing the parent class method, the input parameter can be expanded.
It is not an inheritance, but a method overload.
The preconditions in the subclass must be the same or loose as the preconditions of the overloaded method in the superclass.
(4) When overwriting or implementing the parent class method, the output result can be reduced.
3. Dependency inversion principle
The English name of the dependency inversion principle is dependence inversion principle (dip for short.
The dependency inversion principle has the following three meanings:
The high-level module should not depend on the underlying module, and both should depend on its abstraction.
Abstraction should not depend on specific details
Details should depend on abstraction
The principle of dependency inversion in Java is as follows:
Dependencies between modules occur through abstraction, so that no direct dependency occurs between classes. The dependency is generated through interfaces or abstract classes.
The interface or abstract class does not depend on the Implementation class.
Implement class dependency interface or abstract class
The simplified definition is"Frontend Interface Programming"-- One of the essence of OOD (object-oriented design, object-oriented.
Three methods of dependency:
(1) constructor transmits dependent objects
(2) Pass the dependency object using the setter Method
(3) interface life

4. Interface isolation principle
Interface isolation principle definition: In general,The interface should be as detailed as possible, and the methods in the interface should be as few as possible.
Differences between the interface isolation principle and a single responsibility
A single responsibility requires a single responsibility for the class and interface, and focuses on the responsibility. This is the division of the business logic, and the interface isolation principle requires that the interface methods should be as few as possible.
The interface isolation principle is to standardize the interface, which includes the following four layers of definition:
(1) The interface should be as small as possible
(2) highly cohesive Interfaces
High Cohesion improves the processing capabilities of interfaces, classes, and modules and reduces external interactions.
(3) customized services
Customized services provide excellent services for each individual.
(4) Limited Interface Design
The smaller the interface design granularity, the more flexible the system is, This is an indisputable fact.
In practice, it can be measured according to the following rules:
(1) An interface serves only one sub-module or business logic
(2) through the public method in the business logic compression interface, the interface is often reviewed, try to make the interface "full of bones and muscles", rather than a lot of methods of "fat.
(3) Modify contaminated interfaces as much as possible. If there is a high risk of changes, use the adapter mode for conversion.
(4) understand the environment and reject blind access. Each project or product has its own specific environmental factors. Do not copy the information that the master did.
5. dimit's Law
The English name of the dimit rule is Law of Demeter (loap), also known as the least knowledge principle (least Knowlede principle, LKP)
The dimit rule is defined as follows:One object should have less understanding of other objects.
The DEMETER rule sets clear requirements for low coupling of classes, which includes the following four meanings:
(1) only communicate with friends
(2) there is a distance between friends.
(3) Your Own is your own
(4) use serializable with caution
6. Open and Close principles
Definition of the open/closed Principle: A software entity, such as classes, modules, and functions, shouldOpen to extension and disable Modification.
The software entity contains the following parts:
Modules in projects or software products divided according to certain logical rules
Abstraction and class
Method

2014-07-22 10:01:13

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.