Reading [The Object-Oriented Thought Process] Chapter 1

来源:互联网 发布:查看linux下tomcat版本 编辑:程序博客网 时间:2024/04/25 08:42

// 第一章 TimeStamp: 2005-3-28

Chapter 1.  Introduction to Object-Oriented Concepts

Procedural Versus OO Programming

Moving from Procedural to Object-Oriented Development

What Exactly Is an Object?

What Exactly Is a Class?

Using UML to Model a Class Diagram

Encapsulation

Inheritance

Polymorphism

Composition

Conclusion

 

Chapter 1.  Introduction to Object-Oriented Concepts

第一章.   面向对象概念介绍

老大先介绍了一下整个OO的发展过程和大局势,我就略过了。不过其中有这样一段:

Even when there are legacy concerns, there is a trend to wrap the legacy systems in object wrappers.

Object Wrappers

Object wrappers are object-oriented code that includes structured code inside. For example, you can take a structured module and wrap it inside an object to make it look like an object.

Object wrappers就是将结构化代码封装在面向对象的代码中。举例来说,你可以将一个结构化模块封装在一个对象中让它看起来像一个对象。(言下之意就是说这个不是对象?!怎么理解?)

        

         在这一章中老大将带我们掌握OO基本概念。

 

Procedural Versus OO Programming

过程 VS OO

Before we delve deeper into the advantages of OO development, let's consider a more fundamental question: What exactly is an object? This is both a complex and a simple question. It is complex because shifting gears to learn a totally new way of thinking is not an easy task. It is simple in the sense that most people already think in terms of objects.

再进一步深入研究OO开发的优势,让我们先考虑一个问题:到底什么是对象?这是一个即复杂又简单的问题。说它复杂是因为完全改变一种思考方式不是一件简单的事,说它简单是因为大多数人本来就是按照对象的概念去思考的。

 

For example, when you look at a person, you see the person as an object. And an object is defined by two terms: attributes and behaviors. A person has attributes, such as eye color, age, height, and so on. A person also has behaviors, such as walking, talking, breathing, and so on. In its basic definition, an object is an entity that contains both data and behavior. The word both is the key difference between the more traditional programming methodology, procedural programming, and OO programming. In procedural programming, code is placed into totally distinct functions or procedures. Ideally, as shown in Figure 1.1, these procedures then become "black boxes," where inputs go in and outputs come out. Data is placed into separate structures, and is manipulated by these functions or procedures.

举例来说,当你看到一个人的时候,你就把人看成一个对象。一个对象被定义为两块:属性和行为。

……

一个对象是同时包含数据和行为的实体。同时是区别于传统面向过程和OO的关键。在面向过程中,代码全部在各种明显不同的方法和过程中。理想的情况就像Figure 1.1所示,这些过程应该是一个黑盒子’,数据从入口进入,而被加工后的数据从出口导出。数据被放入单独的结构中,通过这些函数和过程来操作这些数据。

Difference Between OO and Procedural

 

This is the key difference between OO and procedural programming. In OO design, the attributes and behavior are contained within a single object, whereas in procedural, or structured design, the attributes and behavior are normally separated.

这是区别OO和面向过程的关键区别。在OO设计中,属性和行为被同时放在一个单独的对象中。反之,面向对象则将属性和方法独立分离开存放。

这小节后半部分的就不翻了,因为还是再重复加深解释OOData Hiding,而不OO则比较会引起混乱。这个道理大家都懂的,直接跳过。最后稍微提了一下对象和对象直接的通信要靠发消息,也是点到为止,反正到后面还会细说。

 

Moving from Procedural to Object-Oriented Development

从面向过程转向面向对象

 

Procedural Programming

OO Programming

我们不难从上面两个图得出:

如果需要通过网络传输信息时,面向过程传输的是具体的相关数据,而面向对象则传送的是具体的对象,包括属性和方法。

 

A good example of this concept is a Web object, such as a Java applet. The browser has no idea of what the Web object will do. When the object is loaded, the browser executes the code within the object and uses the data contained within the object.

一个好的例子就是Java applet。浏览器本身不关心Web对象做什么。当那些对象被载入后,浏览器执行对象自己内部的方法。

 

老大的图片还是很直观的,很不错。这是值得很多同志们学习的。这一点jjhou做的也很不错。But,太多的书都是满满的字,简直是受罪。有图心情也好。

 

 

 

What Exactly Is an Object?

老大说的也是老套,属性和方法,也没什么特别的,过了。

 

What Exactly Is a Class?

In short, a class is a blueprint for an object. When you instantiate an object, you use a class as the basis for how the object is built. In fact, trying to explain classes and objects is really a chicken-and-egg dilemma. It is difficult to describe a class without using the term object, and to describe an object without using the term class. For example, a specific bike is an object. However, someone had to have the blueprints (that is, the class) to build the bike. In OO software, unlike the chicken-and-egg dilemma, we do know what comes first—the class. An object cannot be instantiated without a class. Thus, many of the concepts in this section are similar to those presented earlier in the chapter, especially when we talk about attributes and methods.

Classes Are Object Templates

Classes can be thought of as the templates, or cookie cutters, for objects as seen in Figure 1.10. A class is used to create an object.

Messages

倒是Message需要了解一下Smalltalk就是这样滴,大家对C++/JAVA大概比较熟,Smalltalk可能接触比较少。有机会学习一下,还是对Think in OO很有帮助的。

Messages are the communication mechanism between objects. For example, when Object A invokes a method of Object B, Object A is sending a message to Object B. Object B's response is defined by its return value. Only the public methods, not the private methods, of an object can be invoked by another object. The following code illustrates this concept:

Message是对象间通讯的机制。举例来说,当对象A调用对象B的一个方法,我们就认为对象A发了一条消息给对象B。对象B要对这个消息作出相应并返回相应的返回值。只有对象中的public方法才能被其它对象所调用。下面用一个示例来说明一下这个概念:

 

public class Payroll{

    String name;

    Person p = new Person();

    String = p.setName("Joe");

    ... code

    String = p.getName();

}

In this example (assuming that a Payroll object is instantiated), the Payroll object is sending a message to a Person object, with the purpose of retrieving the name via the getName method.

在这个例子中(假设一个Payroll对象被实例化了),这个Payroll为了获得Name通过getName这个方法向Person对象发送了条消息。

其实就是这回事,平时都是这样用的,但说法的不同表明大家的思维方式可能会有出入,还是从现在开始正规起来,使用消息这个词语。

 

Using UML to Model a Class Diagram

老大很简单的提了一下,后面会有单独的一章来说UML,表急。

 

Encapsulation

封装

Hiding as Much Data as Possible这个道理大家都懂的吧?不多说了。

 

A Real-World Example of the Interface/Implementation Paradigm

老大举了个例子我们顺便看一下

Figure 1.12 illustrates the interface/implementation paradigm using real-world objects rather than code. The toaster obviously requires electricity. To get this electricity, the cord from the toaster must be plugged into the electrical outlet, which is the interface. All the toaster needs to do to get the required electricity is to use a cord that complies with the electrical outlet specifications; this is the interface between the toaster and the electricity. The fact that the actual implementation is a coal-powered electric plant is not the concern of the toaster. In fact, for all the toaster cares, the implementation could be a nuclear power plant or a local power generator. With this model, any appliance can get electricity, as long as it conforms to the interface specification as seen in Figure 1.12.

 

 


Inheritance

继承

老大通过一个Dog,CatMammal(哺乳动物)三者关系来说明继承,这个大家都没问题的。

继承是is-a关系,这个要知道。

 

Polymorphism

多态

这个在C++/JAVA里面都会以Sharp为例来说明多态,这里也差不多这个意思,我就过了。

 

Composition

组合

It is natural to think of objects as containing other objects.

In this way, objects are often built, or composed, from other objects—this is composition.

Has-a Relationships

Although an inheritance relationship is considered an is-a relationship for reasons already discussed, a composition relationship is termed a has-a relationship. Using the example in the previous section, a television has-a tuner and has-a video display. A television is obviously not a tuner, so there is no inheritance relationship. In the same vein, a computer has-a video card, has-a keyboard, and has-a disk drive. The topics of inheritance, composition, and how they relate to each other is covered in great detail in Chapter 7, "Mastering Inheritance and Composition."

组合就是has-a的关系。

 

 


Conclusion

小结

There is a lot to cover when discussing OO technologies. However, you should leave this chapter with a good understanding of the following topics:

 

Encapsulation: Encapsulating the data and behavior into a single object is of primary importance in OO development. A single object contains both its data and behaviors and can hide what it wants from other objects.

 

Inheritance: A class can inherit from another class and take advantage of the attributes and methods defined by the superclass.

 

Polymorphism:  Polymorphism means that similar objects can respond to the same message in different manners. For example, you might have a system with many shapes. However, a circle, a square, and a star are each drawn differently. Using polymorphism, you can send each of these shapes the same message (for example, Draw), and each shape is responsible for drawing itself.

 

Composition: Composition means that an object is built from other objects.

 

This chapter covers the fundamental OO concepts. By now you should have a good grasp of what OO concepts are all about.

 

通过这一章的学习加上我们过去对OO的积累我们应该对封装,继承,多态,组合这四块有比较好的理解。如果由于我的翻译或风格原因使您还对上述问题还比较confusing,那你可以给我留言,提出意见,我尽可能帮助您解决问题,大家共同进步。