设计模式之 Designing for Change 变更设计(二)
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 examp