Six Principles of design pattern

Source: Internet
Author: User

1. Single Duty principle (Responsibility Principle)

Definition: There should be and only one cause for class changes

Note: The classes here refer not only to classes, but also to methods and interfaces, such as a method we often say to implement a function

2. The principle of substitution on the Richter scale (Liskov Substitution Principle)

Definition: As long as the parent class appears where the subclass is bound to appear, and no exception or error occurs with the substitution of a subclass, the consumer does not need to know whether it is a parent class or a subclass. But it doesn't come back, there are subclasses,

The parent class is not necessarily available

Usage Specification:

    • Subclasses must fully implement the methods of the parent class, and if the subclass cannot fully implement the parent class's methods, it is recommended to break the parent-child inheritance relationship with dependency | Gathering | Combination and other relationships to replace
    • Subclasses can have their own personalities.
    • When overriding or implementing a method of the parent class, the input parameter can be magnified, such as the parent class has a method input parameter is HASHMAP, the child class parameter can be the MAP type, so that the parent class can be replaced by the quilt class, if the reverse, it violates the Richter replacement principle, so the subclass of the method's precondition must be the parent class
The pre-condition of the overridden method is the same or more lenient
    • When overriding or implementing a method of a parent class, the output can be scaled down, that is, if the parent class method returns a type T, the return value type of the same method (overloaded or overridden) of the subclass S,s and T are either of the same type, or S is a subclass of T;
Note: When using the Richter scale substitution principle, try to avoid the subclass "personality", once the subclass has a "personality", the relationship between the subclass and the parent will become bad Harmony 3. Dependency Inversion principle (dependence inversion Principle)

Definition: The dependency inversion principle consists of three meanings

    • High-level modules should not rely on low-layer modules, both should rely on their abstraction
    • Abstraction should not depend on details
    • Detail should be dependent on abstraction
high-rise modules and low-level modules better understand that each logic is composed of atomic logic, the indivisible atomic logic is a low-level module, Atomic logic re-assembly is a high-rise module;abstract refers to an interface or abstract class, neither of which can be directly instantiated;The detail is the realization class, implements the interface or inherits the abstract class to produce the class is the detail, its characteristic is can be instantiated; The implementation of the dependency inversion principle in Java is manifested by:
    • Dependencies between modules occur through abstraction, and there is no direct dependency between classes, and their dependencies are generated through interfaces or abstract classes;
    • An interface or abstract class is not dependent on the implementation class
    • Implementing a class-dependent interface or abstract class
This is one of the essences of interface-oriented programming. rules to follow:
    • Each class has an interface or abstract class as much as possible, or both have
    • The surface type of the variable is as far as the interface or abstract class
    • No class should derive from a specific class
    • Try not to overwrite the base class method, if the base class is an abstract class, and this method has been implemented, subclasses try not to overwrite
    • Combined with the Richter replacement principle
    • Interfaces are responsible for defining public properties and methods, and declaring dependencies on other objects, and abstract classes are responsible for the implementation of common construction parts, implementing classes to implement business logic accurately
4. Interface Isolation principle (Interface segregation Principle)

Let's first look at the definition of the interface:

    • Instance interface: Declare a class in Java, and then use the new keyword to produce an instance, which is a description of a class of things, which can be seen as an interface
    • Class interfaces: interfaces defined with interface
the understanding of isolation:
    • The client should not rely on interfaces it does not need
    • Dependencies between classes should be based on the smallest interface
    • Summary: The establishment of a single interface, do not build a bloated interface, that is, the interface as thin as possible, the interface of the method as little as possible

This is the basis of the open and close principle, the specific content: programming for the interface, dependent on the abstract and not dependent on the specific.

Constraints of the interface isolation principle:

    • interface to high cohesion, meaning is to improve the interface, class, module processing capacity, reduce external interaction, and then the specific point is to minimize the external public method in the interface, through the business logic compression interface Public method
    • Customized services, is to provide a single individual for an excellent service, such as when we write user modules, we need to provide users with query information, change passwords, registered users and other information, when the administrator performs the same operation, the General people will reuse these methods,
then on this basis to add the administrator's own method, this design method must be problematic, so design, when you modify the ordinary user call interface implementation, the administrator implementation will also occur unpredictable changes, we should for the management Individual to write an interface
    • Interface design is limited, the interface design granularity is smaller, the system more flexible, this is certain, but the flexibility of the problem is the complexity of the structure, development difficulty increased, maintainability reduced
    • An interface serves only one submodule or business logic
    • has been contaminated interface, as far as possible to modify, if the risk of a large change, the use of adapter mode for conversion processing
    • Understand the environment, refuse to blindly follow, do not blindly go to set design patterns, and sometimes do not use better, do not copy other people's design method, his method to you this does not necessarily effect good, after all, business logic is not the same
5. Dimitri Law (Demeter Principle)

Definition: Dimitri Law is also called the least knowledge principle, meaning that an object should have the least understanding of other objects, which should be well understood, is to reduce the coupling between the modules

6. Opening and closing principle (Open Close Principle)

Definition: A software entity such as classes, modules, and functions should be open to extensions, closed for modification, and the opening and closing principle is also the cornerstone of the other five Principles

Six Principles of design pattern

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.