设计模式六大原则(一)开闭原则

本文详细解读设计模式的六大原则,包括开闭原则、里氏代换原则、合成复用原则、接口隔离原则、迪米特法则和依赖倒转原则。通过实例阐述每条原则的具体含义及其在软件设计中的应用,旨在帮助开发者构建具有良好扩展性的系统。

10.3  设计模式六大原则

我们已经了解到,设计模式体现的是软件设计的思想,而不是软件技术,它重在使用接口与抽象类来解决各种问题。在使用这些设计模式时,应该首先遵守如表10-1所示的六大原则。

表10-1  设计模式六大原则

 

   

   

开闭原则

对扩展开放,对修改关闭

多使用抽象类和接口

里氏代换原则

基类可以被子类替换

使用抽象类继承,不使用具体类继承

合成复用原则

要依赖于抽象,不要依赖于具体

针对接口编程,不针对实现编程

接口隔离原则

使用多个隔离的接口,比使用单个接口好

建立最小的接口

迪米特法则

一个软件实体应当尽可能少地与其他实体发生相互作用

通过中间类建立联系

依赖倒转原则

尽量使用合成/聚合,而不是使用继承

尽量使用合成/聚合,而不是使用继承

 

下面我们通过简单的实例来讲解这六大原则的具体含义。

10.3.1  开闭原则(Open Closed Principle)

通常,对于开发完的代码都需要多种测试才能够投入使用,这包括:

首先要经过开发人员的单元测试、集成测试。

然后再到测试人员的白盒测试、黑盒测试。

最后还要由用户进行一定的测试。

经过漫长的测试,代码才能够投入使用。但是软件产品的维护和升级又是一个永恒的话题,在维护的过程中,你可能要不断地增加一些小功能;在升级的过程中,你要增加一些较大的功能。

因此,软件产品随时都有扩展功能的要求。这种功能的扩展,就要求我们改变原有的代码。但是,对原代码的修改就会深刻地影响到原来的功能的方方面面:

可能对旧代码引入了新的错误,使你不得不对旧代码进行大规模的修改。

可能引起你不得不重新构造系统的架构。

即使新增的代码对旧代码没有影响,你也不得不对原来的系统做一个全面的测试。

所有上述列出来的问题,都是对系统功能进行扩展所不能承受的代价。换句话说,我们设计出来的系统,一定要是扩展性良好的系统。如何才能够设计出扩展性良好的系统呢?这就需要在软件系统设计时遵守开闭原则:

软件系统必须对扩展开放,对修改关闭。

换句话说,我们的系统必须是可扩展的系统,而不是可修改的系统。

做到开闭原则,就注意以下两点。

1)多使用抽象类

在设计类时,对于拥有共同功能的相似类进行抽象化处理,将公用的功能部分放到抽象类中,所有的操作都调用子类。这样,在需要对系统进行功能扩展时,只需要依据抽象类实现新的子类即可。如图10-1所示,在扩展子类时,不仅可以拥有抽象类的共有属性和共有函数,还可以拥有自定义的属性和函数。

 

2)多使用接口

与抽象类不同,接口只定义子类应该实现的接口函数,而不实现公有的功能。在现在大多数的软件开发中,都会为实现类定义接口,这样在扩展子类时实现该接口。如果要改换原有的实现,只需要改换一个实现类即可。如图10-2所示,各子类由接口类定义了接口函数,只需要在不同的子类中编写不同的实现即可,当然也可以实现自有的函数。

 

以上两点将会在后续的各个设计模式中得到充分的体现。

With this second volume, we enter the intriguing world of complex analysis. From the first theorems on, the elegance and sweep of the results is evident. The starting point is the simple idea of extending a function initially given for real values of the argument to one that is defined when the argument is complex. From there, one proceeds to the main properties of holomorphic functions, whose proofs are generally short and quite illuminating: the Cauchy theorems, residues, analytic continuation, the argument principle. With this background, the reader is ready to learn a wealth of additional material connecting the subject with other areas of mathematics: the Fourier transform treated by contour integration, the zeta function and the prime number theorem, and an introduction to elliptic functions culminating in their application to combinatorics and number theory. Thoroughly developing a subject with many ramifications, while striking a careful balance between conceptual insights and the technical underpinnings of rigorous analysis, "Complex Analysis" will be welcomed by students of mathematics, physics, engineering and other sciences. "The Princeton Lectures in Analysis" represents a sustained effort to introduce the core areas of mathematical analysis while also illustrating the organic unity between them. Numerous examples and applications throughout its four planned volumes, of which "Complex Analysis" is the second, highlight the far-reaching consequences of certain ideas in analysis to other fields of mathematics and a variety of sciences. Stein and Shakarchi move from an introduction addressing "Fourier" series and integrals to in-depth considerations of complex analysis; measure and integration theory, and Hilbert spaces; and, finally, further topics such as functional analysis, distributions and elements of probability theory.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值