设计模式之 Designing for Change 变更设计

The key to maximizing reuse lies in anticipating new requirements and changes to existing requirements, and in designing your systems so that they can evolve accordingly.

变更设计的关键是在预测新的要求和变更已存在的要求时最大限度的重用,在你设计的系统中可以进行相应的设计。

To design the system so that it's robust tosuch changes, you must consider how the system might need to change over itslifetime. A design that doesn't take change into account risks major redesign inthe future. Those changes might involve class redefinition and reimplementation,client modification, and retesting. Redesign affects many parts of the softwaresystem, and unanticipated changes

are invariably expensive.

为了让你设计的系统在面对诸多的改变时显得健全,需要考虑在这个系统的生命周期内出现的所有变化。在未来的时候,设计不能冒重新设计的风险。这些变更可能会涉及到类的重定义和类的实现,客户端的修改,重新测试。重新设计影响了软甲系统的很多的部分,意想不到的变化花钱更多。

Design patterns help you avoid this by ensuring that a system can change in specific ways. Each design pattern lets some aspect of system structure vary independently of other aspects, thereby making a system more robust to a particular kind of change.

设计模式通过一些特殊的方法确保系统可以应对这些变更,帮助你避免这些问题。设计模式会让系统结构的某些方面和其他的方面之间最大限度的独立,因此会让系统在应对各种特殊的变化时显得更健全。

Here are some common causes of redesign along with the design pattern(s) that address them:

这里有常见的设计模式连同它们的位置

1. Creating an object by specifying a class explicitly. Specifying a class name when you create an object commits youto a particular implementation instead of a particular interface. This commitment can complicate future changes. To avoid it, create objectsindirectly.

Design patterns: Abstract Factory (99),Factory Method (121), Prototype (133).

(1)通过指定的显式类创建对象。指定类的名字,当你创建对象保证特殊的实现来代替特殊的接口。这些可以为未来复杂的变化做贡献。为了避免这个问题,间接的创建对象。

设计模式:抽象工厂,工厂模式,原型模式

2. Dependence on specific operations. When you specify a particular operation,you commit to one way of satisfying are quest. By avoiding hard-coded requests, you make it easier to change the way a request gets satisfied both at compile-time and at run-time.

Design patterns: Chain of Responsibility(251), Command (263).

(2)依赖特殊的运算。你通过指定特殊的运算的方法来满足要求。通过避免编码要求,在编译和运行时,可以很容易的变更方法来满足要求。

设计模式:职责链模式,命令模式

3. Dependence on hardware and software platform. External operating system interfaces and application programming interfaces (APIs) are different on different hardware and software platforms.Software that depends on a particular platform will be harder to port to other platforms. It may even be difficult to keep it up to date on itsnative platform. It's important therefore to design your system to limit its platform dependencies.

Design patterns: Abstract Factory (99),Bridge (171).

(3)依靠硬件或软件平台。在不同的软件或硬件平台上,外部运算系统接口和应用程序接口是不同的。软件依赖特殊的平台将会很难移植到其他的平台。甚至可能在它本身的平台上保持数据的更新都很困难。因此在设计你的系统时平台的独立性很重要。

设计模式:抽象工厂,桥接模式

4. Dependence on object representations or implementations. Clients that know how an object is represented, stored,located, or implemented might need to be changed when the object changes.Hiding this information from clients keeps changes from cascading. Design patterns: Abstract Factory (99),Bridge (171), Memento (316), Proxy (233).

(4)依靠当前对象和对象的实现。当一个对象变更时,客户知道对象如何表示、存储和它的位置以及可能需要变更的实现。从客户不断变化的级联中隐匿这些信息

设计模式:抽象工厂,桥接模式,备忘录模式,代理模式

5. Algorithmic dependencies. Algorithms are often extended, optimized, and replaced during development and reuse.Objects that depend on an algorithm will have to change when the algorithm changes. Therefore algorithms that are likely to change should be isolated.Design patterns: Builder (110), Iterator(289), Strategy (349), Template Method (360), Visitor (366).

运算独立性:在发展和重用的过程中,运算经常是可延伸,充分利用和可替代的。当运算变更时,对象所依赖的运算也会变更。因此运算引起的变更要尽可能的独立。

设计模式:建造者模式,迭代器模式,策略模式,模板方法模式,访问者模式

6. Tight coupling. Classes that are tightlycoupled are hard to reuse in isolation, since they depend on each other.Tight coupling leads to

monolithic systems, where you can't change or remove a class without understanding and changing many other classes. The system becomes a dense mass that's hard to learn, port, and maintain. Loose coupling increases the probability that a class can be reused by itself and that a system can be learned, ported,modified, and extended more easily.

Design patterns use techniques such as abstract coupling and layering to promote loosely coupled systems.

Design patterns: Abstract Factory (99),Bridge (171), Chain of Responsibility (251), Command (263), Facade(208), Mediator (305),

Observer (326).

(6)紧密结合:自从它们相互依靠之后,类独立时是很难紧密结合实现重用的。紧密结合使类显得庞大,在那个时候如果你没有理解和变更很多的类,你就不能变更或者移除类。这个系统变得很密实,很难去理解,移植和维护

失去了来的紧密性,类可以被它自己重用,并且系统可以被理解,移植和维护、并且更容易延展。设计模式用诸如抽象结合和分层促进减少结合的系统

设计模式:抽象工厂模式,桥接模式,职责链模式,命令模式,外观模式,观测者模式,中介模式

7. Extending functionality by subclassing.Customizing an object by subclassing often isn't easy. Every new class has a fixed implementation overhead (initialization, finalization,etc.). Defining a subclass also requires an in-depth understanding of the parent class. For example,overriding one operation might require overriding another. An overridden operation might be required to call an inherited operation. And subclassing can lead to an explosion of classes,because you might have to introduce many new subclasses for even a simple extension.

(7)通过子类延伸功能:通过子类定制一个对象通常是不容易的。类通常有固定的实现管理(初始化,终止等等)。定义一个子类也需要彻底的了解父类。例如,覆盖运算可能需要覆盖另一个。覆盖的运算可能被命令去调用被继承的运算。子类可以导致类激增。为了一个简单的延伸你可能不得不借用新类。

Object composition in general and delegation in particular provide flexible alternatives to inheritance for combining behavior. New functionality can be added to an application by composing existing objects in new ways rather than by defining new subclasses of existing classes. On the other hand,heavy use of object composition can make designs harder to understand. Many design patterns produce designs in which you can introduce customized functionality just by defining one subclassand composing its instances with existing ones.

Design patterns: Bridge (171), Chain of Responsibility (251), Composite(183), Decorator (196), Observer (326), Strategy(349).

对象的组合通常来说是授权为继承提供灵活的可选择的联合行为。通过用新的方法组合已有的对象来增加新的功能到应用上,而不是通过用已有的类定义新的子类。另一方面,大量用对象的组合可能会使设计很难被理解。通过用已存在的类定义的子类或对象的组合的实例化可以实现定制的功能,许多设计模式就是通过这种方式来实现设计的。

设计模式:桥接模式,职责链模式,组合模式,装饰模式,观测者模式和策略模式

8. Inability to alter classes conveniently.Sometimes you have to modify a class that can't be modified conveniently.Perhaps you need the source code and don't have it (as may be the case with a commercial class library).Or maybe any change would require modifying lots of existing subclasses. Design patterns offer ways to modify classes in such circumstances.

Design patterns: Adapter (157), Decorator(196), Visitor (366).

(8)无法方便的修改类。有时候你不得不去修改那些不能很方便的修改的类。可能需要源代码,而你却不能得到它或者任何的改变需要改变很多存在的子类。在这样的情形下,设计模式提供了修改类的方法。

设计模式:适配器模式,装饰模式和访问者模式

These examples reflect the flexibility that design patterns can help you build into your software. How crucial such flexibility is depends on the kind of software you're building. Let's look at the role design patterns play in the development of three broad classes of software:application programs, toolkits, and frameworks.

这些例子涉及到用设计模式灵活的设计软件。灵活性取决于你为软件构建的各种类。

让我们看看发展中的软件三大类在设计模式中扮演的角色:应用程序,工具包,框架。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值