Reading [The Object-Oriented Thought Process] Chapter 1

// 第一章 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,那你可以给我留言,提出意见,我尽可能帮助您解决问题,大家共同进步。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Paperback: 360 pages Data: September 4, 2008 Description: An introduction to object-oriented concepts for developers looking to master modern application practices. Object-oriented programming (OOP) is the foundation of modern programming languages, including C++, Java, C#, and Visual Basic .NET. By designing with objects rather than treating the code and data as separate entities, OOP allows objects to fully utilize other objects’ services as well as inherit their functionality. OOP promotes code portability and reuse, but requires a shift in thinking to be fully understood. Before jumping into the world of object-oriented programming languages, you must first master The Object-Oriented Thought Process. Written by a developer for developers who want to make the leap to object-oriented technologies as well as managers who simply want to understand what they are managing, The Object-Oriented Thought Process provides a solution-oriented approach to object-oriented programming. Readers will learn to understand object-oriented design with inheritance or composition, object aggregation and association, and the difference between interfaces and implementations. Readers will also become more efficient and better thinkers in terms of object-oriented development. This revised edition focuses on interoperability across various technologies, primarily using XML as the communication mechanism. A more detailed focus is placed on how business objects operate over networks, including client/server architectures and web services. “Programmers who aim to create high quality software–as all programmers should–must learn the varied subtleties of the familiar yet not so familiar beasts called objects and classes. Doing so entails careful study of books such as Matt Weisfeld’s The Object-Oriented Thought Process.” –Bill McCarty, author of Java Distributed Objects, and Object-Oriented Design in Java Matt Weisfeld is an associate professor in business and technology at Cuyahoga Comm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值