Six principles of [design pattern Zen]

Source: Internet
Author: User

I. Single principle of responsibility

The English name of the single duty principle is responsibility principle, abbreviated as the SRP. The single principle of responsibility is the single of functions within the class or interface, reducing the coupling degree and enhancing the robustness of the program.

The book summarizes the benefits of a single responsibility principle:

    • The complexity of classes is reduced and what responsibilities are clearly defined
    • Increased readability due to reduced complexity
    • Improved maintainability due to improved readability
    • Changes caused by the risk of reduction, change is essential, if the interface of a single responsibility to do a good job, an interface modification only to the implementation of the class has an impact, no impact on other interfaces, which has a great help to the scalability and maintainability of the system
It is true to implement a single function of a class, which causes problems such as overloading between classes and increasing the number of classes. So the single responsibility of the class is really constrained by many factors, pure theory, the principle is very good, but the reality has a realistic difficulty, to fully abide by the difficult to do. Book authors suggest:interface must be a single principle, the design of the class as far as possible only one cause of change. The author of this chapter refers to the single function of the method, enumerates the user information modification instance: the User information modification function to break down to the user name modification, the password modification, the address modifies three functions, thus manifests the method the single responsibility principle. But in the actual project, there is the situation that can modify the user name, password and address at the same time, how to design the user information modification function?
second, the principle of the Richter replacementDefinition: English abbreviation for LSP. All references to base classes must be able to transparently use objects of their subclasses. In layman's terms, as long as the parent class can appear where subclasses can appear, and the substitution of subclasses will not produce any errors or exceptions, the consumer may not need to know whether it is a parent class or a subclass. However, the reverse is not possible, where there are subclasses, the parent class may not be able to adapt. Benefits of Inheritance:
    • Code sharing reduces the effort to create classes
    • Improve the reusability of code
    • Subclasses can resemble a parent class, but are different from the parent class
    • Increase the scalability of your code
    • Improve the openness of products or projects
Disadvantages of Inheritance:
    • Inheritance is intrusive. As long as you inherit, you must have all the properties and methods of the parent class
    • Reduce the flexibility of your code
    • Increased coupling. When the constants, variables, and methods of the parent class are modified, you need to consider the modifications of the subclasses.
The Richter substitution principle defines a specification for good inheritance, and a simple definition contains 4 levels.
    • Subclasses must fully implement the methods of the parent class

      Note: It is important to use the parent class or interface when calling other classes in the class, and if the parent class or interface cannot be used, the design of the class has violated the LSP principle


      Note: If a subclass cannot fully implement a method of the parent class, or if some of the methods of the parent class have distorted in the subclass, it is recommended that you break the parent-child inheritance relationship, using dependencies, aggregates, and combinations to replace the inheritance

    • subclasses can have their own personalities (in the first premise, you can extend other functions ...) )

    • Input parameters can be magnified when overriding or implementing a method of a parent class
    • Output can be scaled down when overriding or implementing a method of the parent class
Book authors suggest: In the project, the use of the Richter replacement principle, as far as possible to avoid sub-class personality, once the subclass has a personality, the relationship between this class and the parent class is difficult to reconcile, the sub-class as the parent class, the subclass of the personality is obliterated; The handle class is used alone to do a business, Makes the coupling between the code confusing--the lack of a standard for class substitution Iii. The principle of dependency inversionThe dependency inversion principle, dependence inversion Principle, DIP, contains three layers of meaning:
    • High-level modules should not rely on the underlying modules, both should be dependent on the abstract
    • Abstraction should not depend on details
    • Detail should be dependent on abstraction
Dependencies result in the performance in Java:
    • Dependencies between modules occur through abstraction, implementing no direct dependencies between classes, and their dependencies are implemented 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
The more streamlined definition isinterface-oriented programming (ood-object-oriented Design)Three ways to rely:
    • Constructors Pass dependent objects

    • Setter methods Pass dependent objects

    • On an interface that declares a dependent object, this method is called interface injection
Book authors suggest:
    • Each class should have an interface or abstract class as much as possible, or abstract classes and interfaces have both
    • The surface type of the variable to make the interface or abstract class as
    • No class should ever be born from a specific class.
    • Try not to overwrite the method of the base class
    • Combined with the Richter replacement principle
noun: Open and close principle, to expand the development, the modification closes. iv. principle of interface IsolationDefinition: The establishment of a single interface, do not build a bloated and huge interface, popular: interface as far as possible, the interface in the method of measuring less. What is the difference between the interface isolation principle and the single responsibility principle? Answer: The principle of interface isolation and the perspective of a single duty is different, the single responsibility requirement is the class and interface responsibilities of a single, focus on responsibility, this is the division of Business logic. The interface isolation principle requires that the interface method be as few as possible. The interface isolation principle is a canonical constraint on an interface that contains the following 4-layer meanings:
    • The interface should be as small as possible (the single responsibility principle must be met first when splitting the interface based on the interface isolation principle)
    • interface to high cohesion
      What's the matter, high cohesion? High cohesion is to improve the interface, class, module processing capacity, reduce external interaction.
    • Customized Services
    • Interface designers have limits.
      The smaller the design granularity of the interface, the more flexible the system, which is an indisputable fact. However, the flexibility has also brought about the complexity of the structure, the development difficulty increases, the maintainability is reduced, so the interface must be moderate, this degree must be tested by experience and try to judge .......
Book authors suggest:
    • An interface serves only one submodule or business logic
    • The public method in the compressed interface through the business logic
    • has been contaminated interface, as far as possible to modify, if the risk of change is too large, then the adapter mode conversion processing
    • Understand the environment and refuse to follow blindly.
v. The law of DimitriDefinition: Law of Demeter, LoD is also known as the least knowledge principle. The Dimitri Law provides a clear requirement for the low-coupling of classes, including the following 4 meanings:
    • Communicate with friends only
      What do you call a direct friend? The coupling between two objects is called a friend relationship.
      Note that a class communicates only with friends, not with unfamiliar classes, and does not appear geta (). GETB (). GetC (). GETD () In this case, the relationship between classes and classes is built between classes , not between methods, Therefore, a method tries not to introduce objects that do not exist in a class .
    • There's a distance between friends.
      Dimitri law requires the class "shy" point, try not to publish too many public methods and non-static variables, as far as possible, more introverted, use private, package-private, protected and other access rights
    • It's your own, it's yours.
      In the actual application of such a method often appears: Put in this class can also, put in other classes also not wrong, how to measure it? You can stick to the principle that if a method is placed in this class, it does not increase the relationship between the classes and does not negatively affect the class, it is placed in this class.
    • Careful use of serializable
The author suggests that the core point of the Dimitri Law is the decoupling between classes, weak coupling, and only weakly coupled, the reuse rate of the class can be improved. vi. principle of opening and closingDefinition: A software entity such as classes, modules, and functions should be open to extensions and closed to modifications. Software entities include the following sections:
    • A module in a project or software product that is broken down by certain logical rules
    • Abstractions and classes
    • Method

Six principles of [design pattern Zen]

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.