1.对象简介 Introduction to Objects
The genesis of the computer revolution was in a machine. The genesis of our programming languages thus tends to look like that machine.
But computers are not so much machine as they are mind amplification tools (bicycles for the mind, as Steve Jobs is fond of saying) and a different kind of expressive medium. Object-oriented programming (OOP) is part of the movement toward using the computer as an expressive medium.
面向对象编程(OOP)–是为了让计算机使用起来更像一种表达媒介。
2.对象有接口An object has an interface
Although what we really do in object-oriented programming is to create new data types, virtually (事实上) all object-oriented languages use the “class” keyword. When you see the word “type” think “class” and vice versa.
There needs to be a way to make a request of the object so that it will do something , and each object can satisfy only certain requests. The requests you can make of an object are defined by its interface, and the type is what determines the interface.
The interface determines the requests that you can make for a particular object. However, there must be code somewhere to satisfy the request. This, along with the hidden data, comprises the implementation.
- 面向对象语言其实是创建了一种新的数据结构,以’class’关键字标识。
- 对于每个对象而言,都有一种方式请求这个对象做些什么,而且每个对象只能对某一些请求回应。
- 所能对对象做的请求就是该对象的接口interface.
3. 对象作为服务提供者An object provides services
While you’re trying to develop or understand a program design, one of the best ways to think about the 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 the 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.
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 coherence (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.
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 may be purchased, but it also produces new objects that might be reused somewhere else.
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.
- 当我们进行程序设计时,我们倾向于把对象作为一种“服务提供者”。目标是制造(后者更好的是在现存代码库中找到)一组能提供服务的对象来解决问题。
- 把对象作为服务提供者的一个附加好处是:能提高对象的内聚程度(coherence)。