"Zen design pattern" of the six design principles novella

This paper stresses the Dependency Inversion Principle and Interface Segregation Principle.

First, the Dependency Inversion Principle

1. Definitions

  • High-level modules should not depend on low-level modules, both of which should rely on its abstract;
  • Abstract should not rely on the details;
  • Details should depend on abstractions;

Lower layer module and a module readily appreciated, is implemented by the logic of each logic composed of atoms, indivisible atomic logic block is low level, the logic of atomic layer module is reassembled.

So what is abstract detail what is it??
In the Java language, abstract refers to the interface or abstract class, both of which can not be directly instantiated; implementation class is the class details, or inherit an abstract class that implements the interface is generated details which features can be instantiated directly, that is, a keyword can be added to produce a new object.

Performance in the Java language is:

  • Abstract dependencies between modules occurs, no direct dependencies between classes, which dependency is generated by the interface or abstract;
  • Interface or abstract class does not depend on the implementation class;
  • Implementation class dependent interface or abstract class;

Dependency Inversion Principle can be reduced using the coupling between classes, and improve system stability and reduce the risk due to parallel development, improve the readability and maintainability of the code.

Note: the availability of design stability, as long as appropriately "loose soil" to see if "design blueprint" also robust growth can be concluded that the higher stability of the design, when the surrounding environment changes frequently, still can be done, "and I shall stays."

2. Depending on the wording of three

(1) dependent objects passing constructor

By objects in the class constructor dependency declaration statement according dependency injection, injection in this way is called a constructor.

(2) Setter method dependent objects passing

Set etter abstract method declared dependency, dependency injection in accordance with the statement that it is Setter Dependency Injection.

(3) interface declaration dependent objects

In the method of dependent objects declared in the interface.

3. Best Practices

Dependency Inversion principle of nature is through the abstract (interface or abstract class) so that each class or module to achieve independence from one another, do not affect each other to achieve loose coupling between modules, how we use this rule in the project do?
Just follow a few below rules can:

  • Each class has two possible interfaces or abstract class or abstract classes and interfaces are provided (which is dependent on the basic requirements of an inverted, abstract classes and interfaces are of abstract, it may have a dependency inversion abstract);
  • Surface type or a variable of the interface is an abstract class as possible;
  • Any class should not derive from specific classes;
  • Try not to overwrite the base class (if class is an abstract base class, and this method has been achieved, so as not to overwrite the subclass. Dependency between abstract class, an abstract method override, will depend on the stability have some impact);
  • Richter substitution principle combined use;

Second, the interface segregation principle

1. Definitions

First clear protagonist - interface, the interface is divided into two types:

  • Examples of interfaces, declared in Java in a class, and then generate a new instance of a keyword, it is a description of a type of things, which is an interface. For example, you define the Person class, and then use the Person zhangSan = new Person () creates an instance, this instance to comply with this standard is the Person class Person class is zhangSan interface. Java is also a class interfaces;
  • Class interface, interface keyword defines a Java interface often used;

What is that isolation?
Two definitions as follows:

  • The client should not rely on it does not need an interface;
  • Dependencies between classes should be based on a minimum of interfaces;

Let me start first definition: "The client should not rely on it does not interface" that depend on what it needs to rely on the interface, the interface does not need to weed out, it needs to interface refinement to ensure its purity? sex;
look at the second definition: "dependencies between classes should be based on the smallest Interface", which requires a minimal interface, the interface is required to refine the interface and pure, with the first definition is exactly the same, just a thing two different descriptions.

Summed up in one sentence: to create a single interface, do not create bloated interface (the popular talk: try to refine the interface, while the interface methods as little as possible).

One might wonder, which is the single responsibility principle is not the same as you?
Interface isolation and the principle of single responsibility new perspective is different, requiring a single duty of a single class and interface responsibilities, focusing on the responsibility, it is the business logic division, two interfaces principle requires isolation interface method as little as possible.

For example a duty interface 10 may comprise a method, this method are placed in a 10 interface, and providing a plurality of access modules "method does not use access" constraint outside of the system through the document, the principle is to allow single responsibility because it requires "to make use of more specialized interfaces." What special interface refers to? It refers to each module should be a single interface, to provide several modules should have several interfaces, rather than creating a huge bloated interface to accommodate all client access.

2. To ensure the purity of the interface

Interface segregation principle is constrained interface specifications, comprising the following four layers meanings:

  • Interface to be as small as possible;
  • Interface to highly cohesive (high cohesion is improved interfaces, classes, the processing capability of the module, to reduce external interaction);
  • Customized services (customized services alone is to provide excellent service for an individual);
  • Interface design is limited (the interface design of the particle size is smaller, the flexible system, which is an indisputable fact);

3. Best Practices

Interface segregation principle is the definition of the interface, but also the definition of classes, interfaces and classes make use of atom or atomic class interfaces assembled. But this division is how to design atomic model of a major problem, in practice, be measured by the Chinese Academy of Sciences following the rules?

  • Only an interface to a service or business logic submodule;
  • public interface methods by compressing the business logic, interfaces often to look back, try to make the interface to achieve "bones covered with flesh," rather than "plump" in a bunch of ways;
  • Has been polluted interface, possible to modify, if a large change risk, the use of the adapter mode conversion process;
  • Understanding of the environment, refused to follow blindly. Each project has a specific product or environmental factors, we do not see the master is doing so you can copy. Do not, different environments, split the standard interface is different. In-depth understanding of the business logic, the best design is coming out of your hands;

So the interface segregation principle how to use it correctly?
The answer is to determine the particle size of the interface based on experience and common sense, the interface size is too small, resulting in dramatic increase in data interfaces, developers choked to death in the ocean in the interface; the interface size is too large, flexible reduced unable to provide customized services with unpredictable risks to the overall project.

How accurately practice Interface Segregation Principle?
Practice, experience and fields.

Guess you like

Origin www.cnblogs.com/youcong/p/11797718.html