Lying design pattern notes (three) の six principles

Single Responsibility Principle

English: Single Responsibility Principle, referred to as the SRP .

definition

On a category, it should only be one the cause of change.

description

  • If a class too many responsibilities to bear, it means these functions coupled together, a change in responsibilities may weaken or inhibit the ability of this class perform other duties. This coupling will lead to fragile designs, when a change occurs, the design will suffer unexpected damage.
  • Much of the software design really need to do is find those duties and responsibilities separated from each other.
  • If you can think of more than a motivation to change a class, then the class will have more than one role.

Open - Closed Principle

English: Open Closed Principle, referred to as OCP .

definition

It is said software entities (classes, modules, functions, etc.) should be extended, but can not be modified.

description

  • That is extended is open for change are closed .
  • No matter how the module is "closed", there will be a number of closed can not change. Since it can not be completely closed, the designer must he designed the module what changes should be closed to make a choice. He must first guess the most likely types of changes, and then construct abstract to isolate those changes .
  • In our initial write code, assuming no change . When changes occur, we will create an abstract to isolate similar changes in the future.
  • The face of demand, when changes to the program by adding new code is carried out, rather than changing existing code.
  • We hope that the development work launched soon know the changes that may occur. The longer identify possible changes to the wait, the more difficult it is to create the right abstractions.
  • Open - closed principle is object-oriented design of the core . Following this principle can bring enormous benefits of object-oriented technology, he claimed, that is maintainable, scalable, reusable, good flexibility . Developers should only program showing those parts of the frequent changes made by the abstract, however, for each part of the application are deliberately abstract it is not a good idea. As important as immature denial of abstraction and abstraction itself .

Dependency Inversion principle

English: Dependence Inversion Principle, referred to as DIP .

definition

High-level modules should not depend on low-level modules, both should depend abstract; abstract should not rely on the details, the details should depend on the abstract.

description

  • It means, for interface programming, not to implement programming .
  • Rely on the reverse in fact can be said for the logo design of the object , which language to write programs is not important, consider if writing is abstract for programming if not for the details of programming that all dependencies program is terminated in the abstract class or interface that is designed objects, on the contrary it is the process of design of.

Richter substitution principle

English: Liskov Substitution Principle, referred to as LSP .

definition

Subtype must be able to replace their parent type.

description

  • If you are using a software entity is a parent, then the child must apply to their class, and it is imperceptible difference between parent and child class object. In other words, the software inside, the parent class are replaced its subclasses, program behavior has not changed .
  • Only when the subclass can replace the parent class, the software unit of function is not affected, the parent can truly be reused, and the subclass can add new behavior on the basis of the parent class.
  • Since the sub-type of an alternative type that makes use of the parent class without modification module would be extended.

Demeter

English: Law of Demeter, referred to LoD .

definition

Also known as the principle of least knowledge. If the two classes do not have to communicate directly with each other, then the two classes can not be direct interaction. If a class needs to call a particular method of another class, you can forward the call through a third party.

description

  • Demeter is the fundamental ideological emphasis on loose coupling between classes .
  • Weaker coupling between classes, the better reuse , in a weakly coupled classes are modified, will not cause spread of a class are related.

Interface Segregation Principle

While this principle in "Westward design mode" is not explicitly stated, but implied in fact or in principle in the other modes.

English: Interface Segregation Principle, referred to as the ISP .

definition

Dependence on a class of another class should be established at a minimum interface. The client program should not rely on it does not interface methods.

description

  • Interface segregation principle and the principle of single responsibility has a certain cross-over concept. In order to both improve the cohesion and the like, to reduce the coupling between them , it embodies the idea of the package. But they are different.
  • Interface segregation principle to emphasize is the interface commitment to the client, the better, and do it single-minded . When the possibility of a request made by a client program to force a change in the interface change occurs, affects the client program as small as possible.
  • Interface segregation principle focus is isolation interface dependence for the interface constraints, mainly for the construction of the abstract and the overall framework .
  • Single responsibility principle focus is duty bound for the class, the main achievement for the program and details .

to sum up

In fact many design mode on the concept of a certain cross, if only according to a code which determines design patterns belong, are difficult to distinguish, further comprising mixing even more. Therefore, do not always entangled in it belongs to the idea in the end what kind of, understand the meaning and can be applied to the actual development which is the most important .

Guess you like

Origin www.cnblogs.com/call-me-devil/p/11031468.html
Recommended