Six principles of design patterns

  There is a lot of information on the Internet about the six design principles of design patterns, but the explanations in many places are too general. I also looked for a lot of information and found that there are several articles about the six principles of design patterns on CSDN. Easy to understand, so reproduced.

  Original author blog link: http://blog.csdn.net/LoveLion/article/category/738450/7

1. Single Responsibility Principle

  Original link: http://blog.csdn.net/lovelion/article/details/7536542

  The Single Responsibility Principle is the simplest object-oriented design principle, and it is used to control the granularity of classes. The Single Responsibility Principle is defined as follows:

Single Responsibility Principle (SRP): A class is only responsible for the corresponding responsibilities in one functional area, or it can be defined as: For a class, there should be only one reason for it to change.

      The Single Responsibility Principle tells us that a class can't be too "tired"! In a software system, the more responsibilities a class (from a module to a method) undertakes, the less likely it is to be reused, and a class has too many responsibilities, which is equivalent to coupling these responsibilities in At the same time, when one of the responsibilities changes, it may affect the operation of other responsibilities. Therefore, these responsibilities should be separated, and different responsibilities should be encapsulated in different classes, that is, different reasons for changes should be encapsulated in different classes. Responsibilities that always change at the same time can be encapsulated in the same class.

      The Single Responsibility Principle is a guideline for achieving high cohesion and low coupling . It is the simplest but most difficult principle to apply. It requires the designer to discover the different responsibilities of a class and separate them, and to discover the multiple responsibilities of a class requires the designer to have Strong analysis and design skills and relevant practical experience.

      Let’s further analyze the Single Responsibility Principle with a simple example:

The developers of Sunny software company proposed the initial design scheme as shown in Figure 1 for the customer information graphic statistics module in a CRM (Customer Relationship Management, customer relationship management) system:

Figure 1 Structure diagram of initial design scheme

In Figure 1, the methods in the CustomerDataChart class are described as follows: getConnection() method is used to connect to the database, findCustomers() is used to query all customer information, createChart() is used to create charts, and displayChart() is used to display charts.

It is now refactored to use the single responsibility principle.

      In Figure 1, the CustomerDataChart class takes on too many responsibilities, including both database-related methods and chart generation and display-related methods. If other classes also need to connect to the database or use the findCustomers() method to query customer information, it is difficult to reuse the code. Whether modifying the database connection mode or modifying the chart display mode, the class needs to be modified. It has more than one reason for its change, which violates the single responsibility principle. Therefore, the class needs to be split to satisfy the single responsibility principle. The class CustomerDataChart can be split into the following three classes:

      (1) DBUtil: responsible for connecting to the database, including the database connection method getConnection();

      (2) CustomerDAO: Responsible for operating the Customer table in the database, including methods such as adding, deleting, modifying and querying the Customer table, such as findCustomers();

      (3) CustomerDataChart: Responsible for the generation and display of charts, including methods createChart() and displayChart().

      The refactored structure using the single responsibility principle is shown in Figure 2:

Figure 2 Structure diagram after reconstruction

2. The principle of opening and closing

  Original link: http://blog.csdn.net/lovelion/article/details/7537584

  The open-closed principle is the first cornerstone of object-oriented reusable design, and it is the most important object-oriented design principle. The open-closed principle was proposed by Bertrand Meyer in 1988 and is defined as follows:

Open-Closed Principle (OCP): A software entity should be open for extension and closed for modification. That is, software entities should try their best to expand without modifying the original code.

      In the definition of the open-closed principle, a software entity can refer to a software module, a local structure composed of multiple classes, or an independent class .

      An important issue that any software needs to face is that their requirements will change over time. When the software system needs to face new requirements, we should try our best to ensure that the design framework of the system is stable. If a software design conforms to the open-closed principle, the system can be easily extended without modifying the existing code, so that the software system has better stability and continuity while having adaptability and flexibility . With the increasing scale of software, longer software life, and higher software maintenance costs, it becomes more and more important to design software systems that satisfy the open-closed principle.

      In order to satisfy the open-closed principle, the system needs to be abstractly designed, and abstraction is the key to the open-closed principle . In programming languages ​​such as Java and C#, a relatively stable abstraction layer can be defined for the system, and different implementation behaviors can be moved to a specific implementation layer. In many object-oriented programming languages, mechanisms such as interfaces and abstract classes are provided, through which the abstract layer of the system can be defined and then extended through concrete classes. If you need to modify the behavior of the system, you don't need to make any changes to the abstract layer, you only need to add new concrete classes to implement new business functions, and to extend the functions of the system without modifying the existing code, to achieve the open-closed principle requirements.

      The CRM system developed by Sunny Software can display various types of charts, such as pie charts and bar charts, etc. In order to support a variety of chart display methods, the original design scheme is shown in Figure 1:

Figure 1 Structure diagram of initial design scheme

      The following code snippet exists in the display() method of the ChartDisplay class:

1
2
3
4
5
6
7
8
9
10
......
if  (type.equals( "pie" )) {
     PieChart chart =  new  PieChart();
     chart.display();
}
else  if  (type.equals( "bar" )) {
     BarChart chart =  new  BarChart();
     chart.display();
}
......


      In this code, if you need to add a new chart class, such as the line chart LineChart, you need to modify the source code of the display() method of the ChartDisplay class to add new judgment logic, which violates the open-close principle.

      The system is now reconstructed to conform to the open-closed principle.

       In this example, since each chart class is programmed in the display() method of the ChartDisplay class, the source code has to be modified to add a new chart class. The system can be refactored in an abstract way, so that it is not necessary to modify the source code when adding a new chart class, which satisfies the open-closed principle. The specific methods are as follows:

      (1) Add an abstract chart class AbstractChart, and use various concrete chart classes as its subclasses;

      (2) The ChartDisplay class is programmed for the abstract chart class, and the client decides which specific chart to use.

      The reconstructed structure is shown in Figure 2:

Figure 2 Structure diagram after reconstruction

      In Figure 2, we have introduced the abstract chart class AbstractChart, and ChartDisplay is programmed for the abstract chart class, and the instantiated concrete chart object is set by the client through the setChart() method, and the chart is called in the display() method of ChartDisplay The object's display() method displays the chart. If you need to add a new chart, such as the line chart LineChart, you only need to use LineChart as a subclass of AbstractChart, and inject a LineChart object into ChartDisplay on the client side, without modifying the source code of the existing class library.    

       注意:因为xml和properties等格式的配置文件是纯文本文件,可以直接通过VI编辑器或记事本进行编辑,且无须编译,因此在软件开发中,一般不把对配置文件的修改认为是对系统源代码的修改。如果一个系统在扩展时只涉及到修改配置文件,而原有的Java代码或C#代码没有做任何修改,该系统即可认为是一个符合开闭原则的系统。

三.里氏替换原则

  原文链接:http://blog.csdn.net/lovelion/article/details/7540445

  里氏代换原则由2008年图灵奖得主、美国第一位计算机科学女博士Barbara Liskov教授和卡内基·梅隆大学Jeannette Wing教授于1994年提出。其严格表述如下:如果对每一个类型为S的对象o1,都有类型为T的对象o2,使得以T定义的所有程序P在所有的对象o1代换o2时,程序P的行为没有变化,那么类型S是类型T的子类型。这个定义比较拗口且难以理解,因此我们一般使用它的另一个通俗版定义:

里氏代换原则(Liskov Substitution Principle, LSP):所有引用基类(父类)的地方必须能透明地使用其子类的对象。

      里氏代换原则告诉我们,在软件中将一个基类对象替换成它的子类对象,程序将不会产生任何错误和异常,反过来则不成立,如果一个软件实体使用的是一个子类对象的话,那么它不一定能够使用基类对象。例如:我喜欢动物,那我一定喜欢狗,因为狗是动物的子类;但是我喜欢狗,不能据此断定我喜欢动物,因为我并不喜欢老鼠,虽然它也是动物。

      例如有两个类,一个类为BaseClass,另一个是SubClass类,并且SubClass类是BaseClass类的子类,那么一个方法如果可以接受一个BaseClass类型的基类对象base的话,如:method1(base),那么它必然可以接受一个BaseClass类型的子类对象sub,method1(sub)能够正常运行。反过来的代换不成立,如一个方法method2接受BaseClass类型的子类对象sub为参数:method2(sub),那么一般而言不可以有method2(base),除非是重载方法。

      里氏代换原则是实现开闭原则的重要方式之一,由于使用基类对象的地方都可以使用子类对象,因此在程序中尽量使用基类类型来对对象进行定义,而在运行时再确定其子类类型,用子类对象来替换父类对象

      在使用里氏代换原则时需要注意如下几个问题:

      (1)子类的所有方法必须在父类中声明,或子类必须实现父类中声明的所有方法。根据里氏代换原则,为了保证系统的扩展性,在程序中通常使用父类来进行定义,如果一个方法只存在子类中,在父类中不提供相应的声明,则无法在以父类定义的对象中使用该方法。

      (2) 我们在运用里氏代换原则时,尽量把父类设计为抽象类或者接口,让子类继承父类或实现父接口,并实现在父类中声明的方法,运行时,子类实例替换父类实例,我们可以很方便地扩展系统的功能,同时无须修改原有子类的代码,增加新的功能可以通过增加一个新的子类来实现。里氏代换原则是开闭原则的具体实现手段之一。

      (3) Java语言中,在编译阶段,Java编译器会检查一个程序是否符合里氏代换原则,这是一个与实现无关的、纯语法意义上的检查,但Java编译器的检查是有局限的。

      在Sunny软件公司开发的CRM系统中,客户(Customer)可以分为VIP客户(VIPCustomer)和普通客户(CommonCustomer)两类,系统需要提供一个发送Email的功能,原始设计方案如图1所示:

图1原始结构图

      在对系统进行进一步分析后发现,无论是普通客户还是VIP客户,发送邮件的过程都是相同的,也就是说两个send()方法中的代码重复,而且在本系统中还将增加新类型的客户。为了让系统具有更好的扩展性,同时减少代码重复,使用里氏代换原则对其进行重构。

      在本实例中,可以考虑增加一个新的抽象客户类Customer,而将CommonCustomer和VIPCustomer类作为其子类,邮件发送类EmailSender类针对抽象客户类Customer编程,根据里氏代换原则,能够接受基类对象的地方必然能够接受子类对象,因此将EmailSender中的send()方法的参数类型改为Customer,如果需要增加新类型的客户,只需将其作为Customer类的子类即可。重构后的结构如图2所示:

图2  重构后的结构图

      里氏代换原则是实现开闭原则的重要方式之一。在本实例中,在传递参数时使用基类对象,除此以外,在定义成员变量、定义局部变量、确定方法返回类型时都可使用里氏代换原则。针对基类编程,在程序运行时再确定具体子类。

  另外补充一篇关于里氏替换原则的一篇博文:

  http://blog.csdn.net/zhengzhb/article/details/7281833

四.依赖倒置原则

  原文链接:http://blog.csdn.net/lovelion/article/details/7562783

  如果说开闭原则是面向对象设计的目标的话,那么依赖倒转原则就是面向对象设计的主要实现机制之一,它是系统抽象化的具体实现。依赖倒转原则是Robert C. Martin在1996年为“C++Reporter”所写的专栏Engineering Notebook的第三篇,后来加入到他在2002年出版的经典著作“Agile Software Development, Principles, Patterns, and Practices”一书中。依赖倒转原则定义如下:

依赖倒转原则(Dependency Inversion  Principle, DIP):抽象不应该依赖于细节,细节应当依赖于抽象。换言之,要针对接口编程,而不是针对实现编程。

      依赖倒转原则要求我们在程序代码中传递参数时或在关联关系中,尽量引用层次高的抽象层类,即使用接口和抽象类进行变量类型声明、参数类型声明、方法返回类型声明,以及数据类型的转换等,而不要用具体类来做这些事情。为了确保该原则的应用,一个具体类应当只实现接口或抽象类中声明过的方法,而不要给出多余的方法,否则将无法调用到在子类中增加的新方法。

      在引入抽象层后,系统将具有很好的灵活性,在程序中尽量使用抽象层进行编程,而将具体类写在配置文件中,这样一来,如果系统行为发生变化,只需要对抽象层进行扩展,并修改配置文件,而无须修改原有系统的源代码,在不修改的情况下来扩展系统的功能,满足开闭原则的要求。

      在实现依赖倒转原则时,我们需要针对抽象层编程,而将具体类的对象通过依赖注入(DependencyInjection, DI)的方式注入到其他对象中,依赖注入是指当一个对象要与其他对象发生依赖关系时,通过抽象来注入所依赖的对象。常用的注入方式有三种,分别是:构造注入,设值注入(Setter注入)和接口注入。构造注入是指通过构造函数来传入具体类的对象,设值注入是指通过Setter方法来传入具体类的对象,而接口注入是指通过在接口中声明的业务方法来传入具体类的对象。这些方法在定义时使用的是抽象类型,在运行时再传入具体类型的对象,由子类对象来覆盖父类对象。

      下面通过一个简单实例来加深对依赖倒转原则的理解:

      Sunny软件公司开发人员在开发某CRM系统时发现:该系统经常需要将存储在TXT或Excel文件中的客户信息转存到数据库中,因此需要进行数据格式转换。在客户数据操作类中将调用数据格式转换类的方法实现格式转换和数据库插入操作,初始设计方案结构如图1所示:

图1 初始设计方案结构图

      在编码实现图1所示结构时,Sunny软件公司开发人员发现该设计方案存在一个非常严重的问题,由于每次转换数据时数据来源不一定相同,因此需要更换数据转换类,如有时候需要将TXTDataConvertor改为ExcelDataConvertor,此时,需要修改CustomerDAO的源代码,而且在引入并使用新的数据转换类时也不得不修改CustomerDAO的源代码,系统扩展性较差,违反了开闭原则,现需要对该方案进行重构。

      在本实例中,由于CustomerDAO针对具体数据转换类编程,因此在增加新的数据转换类或者更换数据转换类时都不得不修改CustomerDAO的源代码。我们可以通过引入抽象数据转换类解决该问题,在引入抽象数据转换类DataConvertor之后,CustomerDAO针对抽象类DataConvertor编程,而将具体数据转换类名存储在配置文件中,符合依赖倒转原则。根据里氏代换原则,程序运行时,具体数据转换类对象将替换DataConvertor类型的对象,程序不会出现任何问题。更换具体数据转换类时无须修改源代码,只需要修改配置文件;如果需要增加新的具体数据转换类,只要将新增数据转换类作为DataConvertor的子类并修改配置文件即可,原有代码无须做任何修改,满足开闭原则。重构后的结构如图2所示:

图2重构后的结构图

    

      在上述重构过程中,我们使用了开闭原则、里氏代换原则和依赖倒转原则,在大多数情况下,这三个设计原则会同时出现,开闭原则是目标,里氏代换原则是基础,依赖倒转原则是手段,它们相辅相成,相互补充,目标一致,只是分析问题时所站角度不同而已。

五.接口隔离原则

  原文链接:http://blog.csdn.net/lovelion/article/details/7562842

  接口隔离原则定义如下:

接口隔离原则(Interface  Segregation Principle, ISP):使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口。

      根据接口隔离原则,当一个接口太大时,我们需要将它分割成一些更细小的接口,使用该接口的客户端仅需知道与之相关的方法即可。每一个接口应该承担一种相对独立的角色,不干不该干的事,该干的事都要干。这里的“接口”往往有两种不同的含义:一种是指一个类型所具有的方法特征的集合,仅仅是一种逻辑上的抽象;另外一种是指某种语言具体的“接口”定义,有严格的定义和结构,比如Java语言中的interface。对于这两种不同的含义,ISP的表达方式以及含义都有所不同:

      (1) 当把“接口”理解成一个类型所提供的所有方法特征的集合的时候,这就是一种逻辑上的概念,接口的划分将直接带来类型的划分。可以把接口理解成角色,一个接口只能代表一个角色,每个角色都有它特定的一个接口,此时,这个原则可以叫做“角色隔离原则”。

      (2) 如果把“接口”理解成狭义的特定语言的接口,那么ISP表达的意思是指接口仅仅提供客户端需要的行为,客户端不需要的行为则隐藏起来,应当为客户端提供尽可能小的单独的接口,而不要提供大的总接口。在面向对象编程语言中,实现一个接口就需要实现该接口中定义的所有方法,因此大的总接口使用起来不一定很方便,为了使接口的职责单一,需要将大接口中的方法根据其职责不同分别放在不同的小接口中,以确保每个接口使用起来都较为方便,并都承担某一单一角色。接口应该尽量细化,同时接口中的方法应该尽量少,每个接口中只包含一个客户端(如子模块或业务逻辑类)所需的方法即可,这种机制也称为“定制服务”,即为不同的客户端提供宽窄不同的接口。

      下面通过一个简单实例来加深对接口隔离原则的理解:

      Sunny软件公司开发人员针对某CRM系统的客户数据显示模块设计了如图1所示接口,其中方法dataRead()用于从文件中读取数据,方法transformToXML()用于将数据转换成XML格式,方法createChart()用于创建图表,方法displayChart()用于显示图表,方法createReport()用于创建文字报表,方法displayReport()用于显示文字报表。

图1 初始设计方案结构图

      在实际使用过程中发现该接口很不灵活,例如如果一个具体的数据显示类无须进行数据转换(源文件本身就是XML格式),但由于实现了该接口,将不得不实现其中声明的transformToXML()方法(至少需要提供一个空实现);如果需要创建和显示图表,除了需实现与图表相关的方法外,还需要实现创建和显示文字报表的方法,否则程序编译时将报错。

      现使用接口隔离原则对其进行重构。

      在图1中,由于在接口CustomerDataDisplay中定义了太多方法,即该接口承担了太多职责,一方面导致该接口的实现类很庞大,在不同的实现类中都不得不实现接口中定义的所有方法,灵活性较差,如果出现大量的空方法,将导致系统中产生大量的无用代码,影响代码质量;另一方面由于客户端针对大接口编程,将在一定程序上破坏程序的封装性,客户端看到了不应该看到的方法,没有为客户端定制接口。因此需要将该接口按照接口隔离原则和单一职责原则进行重构,将其中的一些方法封装在不同的小接口中,确保每一个接口使用起来都较为方便,并都承担某一单一角色,每个接口中只包含一个客户端(如模块或类)所需的方法即可。

      通过使用接口隔离原则,本实例重构后的结构如图2所示:

图2 重构后的结构图

     在使用接口隔离原则时,我们需要注意控制接口的粒度,接口不能太小,如果太小会导致系统中接口泛滥,不利于维护;接口也不能太大,太大的接口将违背接口隔离原则,灵活性较差,使用起来很不方便。一般而言,接口中仅包含为某一类用户定制的方法即可,不应该强迫客户依赖于那些它们不用的方法。

六.迪米特法则

  原文链接:http://blog.csdn.net/lovelion/article/details/7563445

  迪米特法则来自于1987年美国东北大学(Northeastern University)一个名为“Demeter”的研究项目。迪米特法则又称为最少知识原则(LeastKnowledge Principle, LKP),其定义如下:

迪米特法则(Law of  Demeter, LoD):一个软件实体应当尽可能少地与其他实体发生相互作用。

      如果一个系统符合迪米特法则,那么当其中某一个模块发生修改时,就会尽量少地影响其他模块,扩展会相对容易,这是对软件实体之间通信的限制,迪米特法则要求限制软件实体之间通信的宽度和深度。迪米特法则可降低系统的耦合度,使类与类之间保持松散的耦合关系。

      迪米特法则还有几种定义形式,包括不要和“陌生人”说话只与你的直接朋友通信等,在迪米特法则中,对于一个对象,其朋友包括以下几类:

      (1) 当前对象本身(this);

     (2) 以参数形式传入到当前对象方法中的对象;

      (3) 当前对象的成员对象;

      (4) 如果当前对象的成员对象是一个集合,那么集合中的元素也都是朋友;

      (5) 当前对象所创建的对象。

      任何一个对象,如果满足上面的条件之一,就是当前对象的“朋友”,否则就是“陌生人”。在应用迪米特法则时,一个对象只能与直接朋友发生交互,不要与“陌生人”发生直接交互,这样做可以降低系统的耦合度,一个对象的改变不会给太多其他对象带来影响。

      迪米特法则要求我们在设计系统时,应该尽量减少对象之间的交互,如果两个对象之间不必彼此直接通信,那么这两个对象就不应当发生任何直接的相互作用,如果其中的一个对象需要调用另一个对象的某一个方法的话,可以通过第三者转发这个调用。简言之,就是通过引入一个合理的第三者来降低现有对象之间的耦合度

      在将迪米特法则运用到系统设计中时,要注意下面的几点:在类的划分上,应当尽量创建松耦合的类,类之间的耦合度越低,就越有利于复用,一个处在松耦合中的类一旦被修改,不会对关联的类造成太大波及在类的结构设计上,每一个类都应当尽量降低其成员变量和成员函数的访问权限在类的设计上,只要有可能,一个类型应当设计成不变类在对其他类的引用上,一个对象对其他对象的引用应当降到最低

      下面通过一个简单实例来加深对迪米特法则的理解:

      Sunny软件公司所开发CRM系统包含很多业务操作窗口,在这些窗口中,某些界面控件之间存在复杂的交互关系,一个控件事件的触发将导致多个其他界面控件产生响应,例如,当一个按钮(Button)被单击时,对应的列表框(List)、组合框(ComboBox)、文本框(TextBox)、文本标签(Label)等都将发生改变,在初始设计方案中,界面控件之间的交互关系可简化为如图1所示结构:

图1 初始设计方案结构图

      在图1中,由于界面控件之间的交互关系复杂,导致在该窗口中增加新的界面控件时需要修改与之交互的其他控件的源代码,系统扩展性较差,也不便于增加和删除新控件。

      现使用迪米特对其进行重构。

      在本实例中,可以通过引入一个专门用于控制界面控件交互的中间类(Mediator)来降低界面控件之间的耦合度。引入中间类之后,界面控件之间不再发生直接引用,而是将请求先转发给中间类,再由中间类来完成对其他控件的调用。当需要增加或删除新的控件时,只需修改中间类即可,无须修改新增控件或已有控件的源代码,重构后结构如图2所示:

图2  重构后的结构图

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325229808&siteId=291194637