Seven principles of design patterns

Open Closed Principle

Software entities should be open for extension, modification closed.

Here software entity includes the following sections:

  1. Project divided into modules
  2. Classes and interfaces
  3. method

 

Richter substitution principle

Inheritance must ensure that the superclass owned properties are still set up in a subclass.

Richter replace originally inherited base reuse, which reflects the relationship between the base class and subclasses, complement the principle of opening and closing, it is the specification for concrete steps to achieve the abstract.

Popular terms Richter substitution principle is this: subclass can extend the functionality of the parent class, but can not change the parent class of the original function.

In other words: When a subclass inherits the parent class, in addition to adding new methods to complete the new features, but try not to override the parent class method.

Richter replace the role of the principle of

  1. Richter substitution principle is one important way to achieve the principle of opening and closing.
  2. It overcomes the shortcomings of inheritance override the parent class due to deterioration of reusability.
  3. It is to ensure the correctness of the action. That kind of expansion will not introduce new errors to existing systems, it reduces the possibility of error codes.

 

Dependency Inversion Principle

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, the details should depend on the abstract.

The core idea is: to be oriented programming interface, do not realize oriented programming.

Dependency Inversion principle is one important way to realize the principle of opening and closing, it reduces the coupling between the client and the implementation module.

 

Dependence, the role of Inversion Principle:

Dependency Inversion Principle can reduce the coupling between classes.

Dependency Inversion Principle can improve system stability.

Dependency Inversion principle can reduce the risk caused by the parallel development.

Dependency Inversion principle can improve the readability and maintainability of the code.

 

Dependent on implementation of the principle of inversion

Dependency Inversion principle aim is to reduce the coupling between classes via the interface to be oriented programming, so long as we follow the following four points in the actual programming, will be able to satisfy this rule in the project.

  1. Each class try to provide an interface or an abstract class, or have both.
  2. Declare the types of variables as much as possible is an interface or an abstract class.
  3. Any class should not derive from specific classes.
  4. Try to follow the Richter substitution principle when using inheritance.

Single Responsibility Principle

Single Responsibility principles set forth in a class should have one and only one cause why it changes, otherwise the class should be split.

The principles set forth object should not assume too much responsibility, if an object took on too much responsibility, there are at least two disadvantages:

  1. A change in responsibilities may weaken or inhibit the ability of this class implements other responsibilities;
  2. When a client needs a certain responsibility to the object had to be other unwanted responsibilities include it all, resulting in a waste of redundant code or code.

 

单一职责原则的优点:

单一职责原则的核心就是控制类的粒度大小、将对象解耦、提高其内聚性。如果遵循单一职责原则将有以下优点:

  1. 降低类的复杂度。一个类只负责一项职责,其逻辑肯定要比负责多项职责简单得多。
  2. 提高类的可读性。复杂性降低,自然其可读性会提高。
  3. 提高系统的可维护性。可读性提高,那自然更容易维护了。
  4. 变更引起的风险降低。变更是必然的,如果单一职责原则遵守得好,当修改一个功能时,可以显著降低对其他功能的影响。

 

单一职责原则是最简单但又最难运用的原则,需要设计人员发现类的不同职责并将其分离,再封装到不同的类或模块中。而发现类的多重职责需要设计人员具有较强的分析设计能力和相关重构经验

 

接口隔离原则

定义:客户端不应该被迫依赖于它不使用的方法。

该原则还有另外一个定义:一个类对另一个类的依赖应该建立在最小的接口上。

以上两个定义的含义是:要为各个类建立它们需要的专用接口,而不要试图去建立一个很庞大的接口供所有依赖它的类去调用。

 

接口隔离原则和单一职责都是为了提高类的内聚性、降低它们之间的耦合性,体现了封装的思想,但两者是不同的:

单一职责原则注重的是职责,而接口隔离原则注重的是对接口依赖的隔离。

单一职责原则主要是约束类,它针对的是程序中的实现和细节;接口隔离原则主要约束接口,主要针对抽象和程序整体框架的构建。

 

接口隔离原则的优点:

接口隔离原则是为了约束接口、降低类对接口的依赖性,遵循接口隔离原则有以下 5 个优点:

  1. 将臃肿庞大的接口分解为多个粒度小的接口,可以预防外来变更的扩散,提高系统的灵活性和可维护性。
  2. 接口隔离提高了系统的内聚性,减少了对外交互,降低了系统的耦合性。
  3. 如果接口的粒度大小定义合理,能够保证系统的稳定性;但是,如果定义过小,则会造成接口数量过多,使设计复杂化;如果定义太大,灵活性降低,无法提供定制服务,给整体项目带来无法预料的风险。
  4. 使用多个专门的接口还能够体现对象的层次,因为可以通过接口的继承,实现对总接口的定义。
  5. 能减少项目工程中的代码冗余。过大的大接口里面通常放置许多不用的方法,当实现这个接口的时候,被迫设计冗余的代码。

 

迪米特法则

迪米特法则的定义是:只与你的直接朋友交谈,不跟“陌生人”说话。

其含义是:如果两个软件实体无须直接通信,那么就不应当发生直接的相互调用,可以通过第三方转发该调用。

其目的是降低类之间的耦合度,提高模块的相对独立性。

 

迪米特法则的优点:

迪米特法则要求限制软件实体之间通信的宽度和深度,正确使用迪米特法则将有以下两个优点:

  1. 降低了类之间的耦合度,提高了模块的相对独立性。
  2. 由于亲合度降低,从而提高了类的可复用率和系统的扩展性。

 

但是,过度使用迪米特法则会使系统产生大量的中介类,从而增加系统的复杂性,使模块之间的通信效率降低。所以,在釆用迪米特法则时需要反复权衡,确保高内聚和低耦合的同时,保证系统的结构清晰。

 

合成复用原则

合成复用原则(Composite Reuse Principle,CRP)又叫组合/聚合复用原则(Composition/Aggregate Reuse Principle,CARP)。

它要求在软件复用时,要尽量先使用组合或者聚合等关联关系来实现,其次才考虑使用继承关系来实现。

如果要使用继承关系,则必须严格遵循里氏替换原则。合成复用原则同里氏替换原则相辅相成的,两者都是开闭原则的具体实现规范。

 

合成复用原则的重要性:

通常类的复用分为继承复用和合成复用两种,继承复用虽然有简单和易实现的优点,但它也存在以下缺点:

  1. 继承复用破坏了类的封装性。因为继承会将父类的实现细节暴露给子类,父类对子类是透明的,所以这种复用又称为“白箱”复用。
  2. 子类与父类的耦合度高。父类的实现的任何改变都会导致子类的实现发生变化,这不利于类的扩展与维护。
  3. 它限制了复用的灵活性。从父类继承而来的实现是静态的,在编译时已经定义,所以在运行时不可能发生变化。

采用组合或聚合复用时,可以将已有对象纳入新对象中,使之成为新对象的一部分,新对象可以调用已有对象的功能,它有以下优点:

  1. 它维持了类的封装性。因为成分对象的内部细节是新对象看不见的,所以这种复用又称为“黑箱”复用。
  2. 新旧类之间的耦合度低。这种复用所需的依赖较少,新对象存取成分对象的唯一方法是通过成分对象的接口。

复用的灵活性高。这种复用可以在运行时动态进行,新对象可以动态地引用与成分对象类型相同的对象。

Guess you like

Origin www.cnblogs.com/SilenceRui/p/11347406.html