Objects can vary tremendously in size and number. They canrepresent everything down to the hardware or all the way up toentire applications. How do we decide what should be an object?
就尺寸和数字而言,对象是极其多样变化的。它们可以代表一切到硬件或到整个应用的程序。我们如何决定什么是对象?
Design patterns address this issue as well.The Facade (208) pattern describes how to represent complete subsystems asobjects, and the Flyweight (218) pattern describes how to support huge numbers ofobjects at the finest granularities.Other design patterns describe specificways of decomposing an object into smaller objects. Abstract Factory (99) and Builder(110) yield objects whose only
responsibilities are creating otherobjects. Visitor (366) and Command (263) yield objects whose only responsibilities are toimplement a request on another objector group of objects.
设计模式也解决这个问题。外观模式描述如何将完整的子系统作为对象。享元模式模式描述了如何在最小的粒度上来支持含有大量数字的对象。其他的设计模式描述了如何用特殊的方法将对象分解成为更小的对象。抽象工厂模式和建造者模式让出那些责任仅仅是创建其他对象的对象访问者模式和命令模式让出那些在其他的对象或一组对象上责任是实现某种要求的对象。
Specifying Object Interfaces 指定对象接口
Every operation declared by an objectspecifies the operation's name, the objects it takes as parameters, and the operation'sreturn value. This is known as the operation's signature. The set of allsignatures defined by an object's operations is called the interface to the object. Anobject's interface characterizes the complete set of requests that can be sentto the object. Any request that matches
a signature in the object's interface maybe sent to the object.
每一种运算的声明是通过一个叫运算名字的接口。对象作为参数,并且运算返回一个值。这些体现了运算的特征。通过指向对象接口这所有被定义的特征被调用。
对象接口的特征是一套完整的可以发送给对象的命令。任何命令匹配对象的接口可能被发送给对象的特征
A type is a name used to denote aparticular interface. We speak of an object as having the type "Window" if itaccepts all requests for the operations defined in the interface named "Window."An object may have many types, and widely different
objects can share a type. Part of anobject's interface may be characterized by one type, and other parts by other types.Two objects of the same type need only share parts of their interfaces. Interfacescan contain other interfaces as subsets.We say that a type is a subtype of anotherif its interface contains the interfaceof its supertype. Often we speak of asubtype inheriting the interface of its
supertype.
类型是用于标识特定的接口的名称。我们谈到某个对象有一个类型为”窗口”的接口,如果我们获得所有的命令通过该接口接口定义的运算。对象可能有很多的类型,大量不同的对象可以共用一个类型,而其他的部分通过使用其他的类型。两个相同类型的对象可以共享它们之间部分接口。接口可以成为其他接口的子集。如果一个接口包含子接口,我们就说该类型是另外的类型子类的父类。我们经常说子类继承超类的接口。
Interfaces are fundamental inobject-oriented systems. Objects are known only through their interfaces. There is no wayto know anything about an object or to ask it to do anything without goingthrough its interface. An object's interface says nothing about itsimplementation—different objects are free to implement requests differently. That means twoobjects having completely different implementations can have identicalinterfaces.
接口是面向对象系统的基础。了解对象只有通过它们的接口。如果没有通过这个接口,我们就没有办法来了解这个对象和要求这个对象去做任何事。不同的对象实现不同的命令时自由的。对象的接口不会对对象的实现说些什么,这也就意味着两个对象可以用完全相同的接口来完成完全不同的实现。
When a request is sent to an object, theparticular operation that's performed depends on both the request and thereceiving object. Different objects that support identical requests may havedifferent implementations of the operations
that fulfill these requests. The run-timeassociation of a request to an object and one of its operations is known asdynamic binding.
当命令被发送给对象的是,特定的运算依靠发出命令和接受命令的对象进行。不同的对象满足这个要求,支持对完全相同的命令做出不同的实现。在执行时协同命令给对象,这种运算被称为动态绑定.
Dynamic binding means that issuing arequest doesn't commit you to a particular implementation until run-time.Consequently, you can write programs that expect an object with a particular interface,knowing that any object that has the correctinterface will accept the request.Moreover, dynamic binding lets you substitute objects that have identical interfaces foreach other at run-time. This
substitutability is known as polymorphism,and it's a key concept in object-oriented systems. It lets a clientobject make few assumptions about other objects beyond supporting a particularinterface. Polymorphism simplifies the definitions of clients, decouples objectsfrom each other, and lets them varytheir relationships to each other atrun-time.
动态绑定意味着发送命令不能保证特定的实现直到程序执行时。因此,你可以写一个对象调用特定接口的程序,了解对象会用特定的接口来实现获得的命令。另外,在程序执行时,动态绑定会让完全相同的接口来实现对象的替代。这种替代被称为多态。这是在面向对象系统中一个关键的概念。它让客户对象除了支持特定的接口之外还对其他对象做出假设。在程序执行时,多态简化了定义的客户,减弱了对象之间的关系,使它们之间的关系有一定的差异。
Design patterns help you define interfacesby identifying their key elements and the kinds of data that get sent across aninterface. A design pattern might also tell you what not to put in the interface.The Memento (316) pattern is a good example. It describes how to encapsulateand save the internal state of an object so that the object can be restored to thatstate later. The pattern stipulates
that Memento objects must define twointerfaces: a restricted one that lets clients hold and copy mementos, and a privilegedone that only the original object can use to store and retrieve state in thememento.
设计模式帮助你通过确认关键元素和通过接口发送各种数据来定义接口。设计模式也会告诉你什么不应该放入接口。备忘录模式是一个很好的例子。他描述了如何封装对象和保存对象的内部状态。以至于对象之后的状态可以被重新存储。这种模式要求备忘录对象定义两个接口。在备忘录内,一个内部让客户可以捕捉和拷贝备忘,一个保密的只有原始对象可以用于储存和恢复状态。
Design patterns also specify relationshipsbetween interfaces. In particular, they often require some classes to havesimilar interfaces, or they place constraints on the interfaces of someclasses. For example, both Decorator (196)and Proxy (233) require the interfaces ofDecorator and Proxy objects to be identical to the decorated and proxiedobjects. In Visitor (366), the Visitor interface must reflect all classes ofobjects that visitors can visit.
设计模式也明确说明接口之间的关系,尤其是它们要求一些类有相似的接口,或者放置限制在一些类的接口中。例如装饰模式和代理模式要求装饰或代理对象接口被装饰和代理对象确认。在访问者模式中,访问接口一定折射访问者可以访问到的类的所有对象。