Six design principles java design patterns

1. Single Responsibility

Popular simple terms: a class is only responsible for a duty.

Follow the single responsibility of the advantages of the original are:

  • Can reduce the complexity of the class, a class is only responsible for a duty, certainly better than its logic is responsible for a number of responsibilities simpler;
  • Improve the readability of the class, improve maintainability;
  • Risk reduction due to change, change is inevitable, if the single responsibility to comply with the principle of good, when modifying a function, you can significantly reduce the impact on other functions.

It should be noted that single responsibility principle is not just specific to object-oriented programming ideas, as long as modular programming, apply a single responsibility principle.

Single Responsibility seemingly simple, in fact, in the actual application process, you will find really a lot of responsibility extended phenomenon, this time using fully comply with or follow the direct violation of the Single Responsibility Principle or method will depend on the current business development personnel the skill level and the time demand, if insufficient skill levels will certainly be simple if else to solve, what principles do not want to directly realize the function just fine, which is why many small companies will find that the code is business heap up, of course, there is a good little company code is written in good, this is undeniable. But no matter what way to solve, at least hearts to know that there are several solutions.

2, Richter substitution principle (Liskov Substitution Principle)         

One of the basic principles of Richter substitution principle (Liskov Substitution Principle LSP) object-oriented design. Richter substitution principle in that place any base class can appear, sub-class will be able to appear. LSP is inherited cornerstone multiplexed only when the derived class can replace the base class, the software unit of functionality is not affected, the base class can really be reused, derived class can add new behavior on the basis of the base class on . Richter substitution principle is - to add "on-off" principle. To achieve "open - closed" principle is a key step in abstraction. The inheritance relationship with a base class is a subclass of the abstract concrete realization, so Richter substitution principle is the specification for concrete steps to achieve the abstract.

After reading the above concepts is estimated that many people like me are not too understanding, or more curious, why is it called the Richter replaced? The reason: The principle was first stated in 1988, presented by MIT surnamed Ms. Lane (Barbara Liskov) come.

Anatomy following description:

Definition 1: If the object type for each of T1 o1, o2 T2 has an object type, such that all defined P T1 program when all the generations are replaced objects o1 o2, the program does not change the behavior of P , then the type is a subtype of type T2 T1.

Definition 2: All references to base classes must be transparently used where the object whose subclasses.

Popular Simply put: subclass can extend the functionality of the parent class, but can not change the parent class of the original function.

It contains meanings:

  • Subclasses may implement the abstract parent class, but non-abstract parent class can not be covered.
  • Subclass can add their own unique way.
  • When the method of the parent class subclass overloaded, the pre-conditions of the process (i.e., process parameter) input parameters than the parent class method more relaxed. [Note distinction between overloading and rewrite]
  • When subclass of the implementation of the abstract parent class, method postcondition (i.e., return value) is more stringent than the parent.

 3. Dependency Inversion Principle (Dependence Inversion Principle)                                                       

The so-called dependency inversion principle (Dependence Inversion Principle) is to rely on the abstract, do not rely on specific. Implement the key principles of opening and closing is abstract and derives from the abstract concrete realization, if the principle of opening and closing the target object-oriented design, then the principle is the primary means of object-oriented design relies reversed.

Definition: high-level modules should not depend on low-level modules, both of which should rely on its abstract; abstract should not rely on details; details should depend on the abstract.

Popular point that: abstract requirements of programming, not to achieve programming, thus reducing the coupling between the client and the implementation module.

There are three ways to transfer the dependency, the method used in the above example is an interface transfer, there are two other delivery methods: constructor passing and setter methods passed, I believe used Spring Framework, transfer mode will not be unfamiliar to rely on .

In actual programming, we generally need to do the following three points:

  • Lower block as far as possible should have an abstract class or interface, or both. [May be used] people
  • Declare the types of variables as much as possible is an abstract class or interface.
  • Richter substitution principle to follow when using inheritance.

Dependency Inversion principle is the core of our programming to interfaces to understand the interface-oriented programming, also understand Dependency Inversion.

 4. Interface segregation principle (Interface Segregation Principle)                                                                 

The principle is literally: a plurality of interface isolation, is better than using a single interface. Intended to reduce the coupling between classes, and the design pattern is a design software, from large-scale software architecture, in order to upgrade and easy maintenance. Therefore, the above multiple times: reduce dependence, reduce coupling.

Original definition: The client should not rely on it does not interface; a dependency on another class of the class should be based on the smallest interface. 

Meaning the interface segregation principle is: the establishment of a single interface, do not create bloated interface, try to refine the interfaces, interface methods as little as possible

Here, many people will feel isolated interface very similar to the principle of single responsibility principle before, it is not. First, the Single Responsibility Principle is the primary focus of duty; and the interface segregation principle focus isolation interfaces depend. Second, the Single Responsibility Principle is mainly constrained class, followed by the interfaces and methods, it is aimed at program implementation and details; and the interface Interface Interface Segregation Principle The main constraint, mainly for abstraction, for the program to build the overall framework.

When using the interface segregation principle of interface constraints, to note the following:

  • Interface as small as possible, but there's a limit. The interfaces can be refined to improve the flexibility of the fact that programming is not earned, but if it is too small, it will cause excessive number of interfaces, complicating the design. So be sure to moderate.
  • Customized services to dependent interface, only expose the class to call the method requires it, and it does not require the method is hidden. Only focus on providing customized services for a module in order to establish the minimum dependencies.
  • Improve cohesion and reduce the external interaction. The interfaces with the least method to accomplish most things.

Use the interface segregation principle, must be appropriate, interface design is too large or too small is not good. Interface design time, only more time to think and plan in order to practice this principle accurately.

5. Demeter (minimum principle known) (Demeter Principle)                                                                                  

Why call least known principles, that is to say: an entity should minimize the interaction between the entity and the other, such that the independent function modules. That is a software entity should be as little interaction with other entities. Thus, when a module is modified, will minimize the impact of other modules, expansion will be relatively easy, which is to limit communications between software entities, it requires a communication between the width and depth limitations software entities.

Definition: An object should be kept to a minimum understanding of other objects.

The origin of the problem: the closer the relationship between class and class, the greater the degree of coupling, when a class is changed, the influence of another class is also greater.

Solution: minimize coupling between the class and class.

Since the program began contacting us, we know the general principles of software programming: low coupling, high cohesion. Whether or process-oriented programming object-oriented programming, only the coupling between the individual modules as low as possible, in order to improve the rate of code reuse. The advantages of coupling low self-evident, but how can we do the programming and low coupling it? That is what the Law of Demeter to be done.

Demeter also known as the principle of the least known, was first proposed by Ian Holland America Northeastern University in 1987. Popular speaking, is a class of their dependent classes know better. That is, for the dependent classes, no matter how complex logic, as much as possible to the internal logic is encapsulated in the class, the external addition method provided by the public, without leaking any information outside. Demeter there is a more simple definition: only direct communication with friends. First, let's explain what is direct friends: Each object has a coupling relationships to other objects, as long as the coupling relationship between two objects, we say that friendship between the two objects. Many coupling mode, dependent, associated, combined, polymerization. Among them, we call appearance member variables, method arguments, method return values directly in the class friends, now out of the local variables of the class are not direct friends. In other words, unfamiliar class best not to appear as a form of local variables within the class.

 

6. The opening and closing principle (Open Close Principle)                                                                                               

That is the principle of opening and closing open for extension, but closed for modification. When the program needs to expand, not to modify the existing code to achieve the effect of a hot-swappable. So summarized in one sentence: In order to make the program better scalability, ease of maintenance and upgrades. We want to achieve this effect, need oriented programming interface.

Is defined: a software entity, such as classes, modules and functions should be open for extension but closed for modification.

The origin of the problem: in the software life cycle because of changes, upgrades, and maintenance or other reasons need to modify existing software code, the old code could have introduced errors or may cause us to have to reconstruct the whole function, and requires the original code has been re-tested.

Solution: When software needs to change, try to achieve changes in behavior by extending the software entity, rather than to achieve change by modifying existing code.

Opening and closing the principles of object-oriented design is the most basic design principles that guide us in how to build a stable and flexible system. Opening and closing the principle of the six principles of design patterns may be defined in one of the most obscure, it only tells us open for extension, but closed for modification, but in the end how can we open to extension but closed for modification, and did not explicitly tell us . Previously, if someone told me, "When you make design must comply with the principle of opening and closing," I feel he did not say anything, but it looks like and what the say. Since the open principles really Void.

If after careful thought and read many articles design patterns, you will find in fact, we follow the five principles of the previous design patterns, and the use of 23 kinds of design patterns aim is to follow the principle of opening and closing. In other words, as long as we abide by the principles of good for front five, a software design is naturally in line with the principle of opening and closing, opening and closing of this principle is more like the previous level of compliance with the five principles of "average score", in front of 5 compliance with the principle of good, average naturally high, indicating that the software designed to comply with the principle of opening and closing of the good; if the previous five principles comply with the bad, then good compliance with the principle of opening and closing.

Closing principle this is nothing more than one meaning to express: a framework for building an abstract, by extension detail achieved. Because the abstract good flexibility and adaptability, as long as the abstract rational, can remain stable software architecture. The details of the software variable, we used to extend from the abstract implementation class students, when software needs to change, we just need to implement a class derived according to the demand to extend it. Of course, only if we abstract should be reasonable, forward-looking and predictability to the job of changing needs.

Here, then recall earlier said five principles, it is precisely tell us build the framework of abstract, with precautions only achieve expansion details: Single Responsibility Principle tells us to implement a single class duties; Richter substitution principle tells us not damage inheritance system; Dependency inversion principle tells us to be oriented programming interface; Interface Segregation principle tells us to streamline the design of the interface when single; Demeter tells us to reduce the coupling. The opening and closing principle is master, he told us to open for extension, but closed for modification.

Finally, explain how to comply with these six principles. Follow these six principles and is not no problem, but the more and less of a problem, that is to say, we generally do not say there is no compliance, but to say how much the degree of compliance. Anything is too far, six design principles of design patterns is the same, the development of the six principles of our purpose is not to be rigid compliance with them, need flexibility in the use of the actual situation. As long as their level of compliance within a reasonable range, even if it is good design. We use a diagram to explain.

Each dimension in the figure each represent a principle, we are in accordance with the degree of compliance with this principle in the dimension drawing a point, if you follow this principle of reasonable, then, this point should fall inside the red concentric circles; If compliance is poor, the point will be inside the small round; if excessive compliance, the point will fall outside the great circle. A good design is reflected in the figures, it should be six vertex of the hexagon in concentric circles.

In the figure, 1 design, design 2 belongs to a well-designed, to the extent that they are in compliance with the six principles of a reasonable range; 3 design, design 4 design although some deficiencies, but basically acceptable; design 5 serious lack of principles are not strictly followed; the transition is designed to comply with the six, design 5 and 6 are designed urgent need to reconstruct the design.

Guess you like

Origin www.cnblogs.com/szz1113/p/11498765.html