An object provides services(每个对象都提供服务)

来源:互联网 发布:制作地图软件 编辑:程序博客网 时间:2024/05/29 17:12

While you're trying to develop or understand a program design, one of the best ways to think about objects is as "service providers." 当正在试图开发或理解一个程序设计时,最好的方法之一就是将对象想象为“服务提供者”。

Your program itself will provide services to the user, and it will accomplish this by using the services offered by other objects. 程序本身将向用户提供服务,它将通过调用其他对象提供的服务来实现这一目的。

Your goal is to produce(or even better, locate in existing code libraries) a set of objects that provide the ideal services to solve your problem. 你的目标就是去创建(或者最好是在现有代码库中寻找)能够提供理想的服务来解决问题的一系列对象。


A way to start doing this is to ask, "If I could magically pull them out of a hat, what objects would solve my problem right away?" 着手从事这件事的一种方式就是问一下自己:“如果我可以将问题从表象中抽取出来,那么什么样的对象可以马上解决我的问题呢?”

For example, suppose you are creating a bookkeeping program. 例如,假设你正在创建一个簿记系统。

You might imagine some objects that contain pre-defined bookkeeping input screens, another set of objects that perform bookkeeping calculations, and an object that handles printing of checks and invoices on all different kinds of printers. 那么可以想象,系统应该具有某些包括了预定义的簿记输入屏幕的对象,一个执行簿记计算的对象集合,以及处理在不同的打印机上打印支票和开发票的对象。

Maybe some of these objects already exist, and for the ones that don't, what would they look like? 也许上述对象中的某些已经存在了,但是对于那些并不存在的对象,它们看起来像什么样子?

What services would those object provide, and what objects would they need to fulfill their obligations? 它们能够提供哪些服务,它们需要哪些对象才能履行它们的义务?

If you keep doing this, you will eventually reach a point where you can say either, "That object seems simple enough to sit down and write" or "I'm sure that object must exist already." 如果持续这样做,那么最终你会说“那个对象看起来很简单,可以坐下来写代码了”,或者会说“我肯定那个对象已经存在了。”

This is a reasonable way to decompose a problem into a set of objects. 这是将问题分解为对象集合的一种合理方式。


Thinking of an object as a service provider has an additional benefit: It helps to improve the cohesiveness of the object. 将对象看作是服务提供者还有一个附带的好处:它有助于提高对象的内聚性。

High cohesion is a fundamental quality of software design: It means that the various aspects of a software component(such as an object, although this could also apply to a method or a library of objects) "fit together" well. 高内聚是软件设计的基本质量要求之一:这意味着一个软件构件(例如一个对象,当然它也有可能是指一个方法或一个对象库)的各个方面“组合”得很好。

One problem people have when designing objects is cramming too much functionality into one object. 人们在设计对象时所面临的一个问题是,将过多的功能都塞在一个对象中。

For example, in your check printing module, you may decide you need an object that knows all about formatting and printing. 例如,在检查打印模式的模块中,你可以这样设计一个 对象,让它了解所有的格式和打印技术。

You'll probably discover that this is too much for one object, and that what you need is three or more objects. 你可能会发现,这些功能对于一个对象来说太多了,你需要的是三个甚至更多个对象。

One object might be a catalog of all the possible check printing module, you may decide you need an object that knows all about formating and printing.其中,一个对象(或对象集合)可以是一个通用的打印接口,它知道有关所有不同类型的打印机的信息;

One object or set of objects can be a generic printing interface that knows all about different kinds of printers(but nothing about bookkeeping--this one is a candidate for buying rather than writing yourself). 另一个对象或对象集合可以是一个通用的打印接口,它知道有关所有不同类型的打印机的信息(但是不包含任何有关簿记的内容,它更应该是一个需要购买而不是自己编写的对象);

And a third object could use the services of the other two to accomplish the task. 第三个对象通过调用另外两个对象的服务来完成打印任务。

Thus, each object has a cohesive set of services of services it offers. 这样,每个对象都有一个它所能提供服务的内聚的集合。

In  a good object-oriented design, each object does one thing well, but doesn't try to do too much. 在良好的面向对象设计中,每个对象都可以很好地完成一项任务,但是它并不试图做更多的事。

This not only allows the discovery of objects that might be purchased(the printer interface object), but it also produces new objects that might be reused somewhere else (the catalog of check layouts).  就像在这里看到的,不仅允许通过购买获得某些对象(打印机接口对象),而且还可以创建能够在别处复用的新对象(支票排版目录对象)。


Treating objects as service providers is a great simplifying tool. 将对象作为服务提供者看待是一件伟大的简化工具。

This is useful not only during the design process, but also when someone else is trying to understand your code or reuse an object. 这不仅在设计过程中非常有用,而且当其他人试图理解你的代码或重用某个对象时。

If they can see the value of the object based on what service it provides, it makes it much easier to fit it into the design. 如果他们看出了这个对象所能提供的服务的价值,它会使调整对象以适应其设计的过程变得简单得多。


0 0