《The Object-Oriented Thought Process》读书笔记4

7 Mastering Inheritance and Composition

Inheritanceand composition play major roles in the design of object-oriented(OO) systems.

Reusing Objects

Inheritancerepresents the is-a relationship that was introduced in Chapter 1.

Composition(组合) represents a has-a relationship.

Subtle

英 [ˈsʌtl] 美 [ˈsʌtl]

adj. 微妙的;敏感的;狡猾的;巧妙的

Inheritance

The bark and pant methodsare written only once and, assuming that they were properly tested when the Dog class was written, they do not need to be heavilytested again (but it does need to be retested). Now let’s take full advantageof our inheritance structure and create

 

Whereasretrievers(猎犬) are bred for retrieving, Lhasa Apsos are bred foruse as guard dogs.

《The Object-Oriented Thought Process》读书笔记4

Testing New Code

 

So we create aclass called Birdwith a fly method.You should immediately understand theproblem.What do we do with a penguin, or an ostrich(鸵鸟)?

Penguin

英 [ˈpeŋgwɪn] 美 [ˈpɛŋɡwɪn, ˈpɛn-]

n. 企鹅

it would notmake sense to have a method called fly for a bird that does not fly but only waddles(摇摇摆摆的走).

chagrin

英 [ˈʃægrɪn] 美 [ʃəˈgrɪn]

n. (由失败等引起的)懊恼,懊丧,悔恨

 

yodel

英 [ˈjəʊdl] 美 [ˈjoʊdl]

n. 岳得尔歌(一种流行与瑞士和奥地利山民间的民歌)

v. 用真假嗓音陡然互换地唱

《The Object-Oriented Thought Process》读书笔记4

Generalization(泛化) and Specialization(特化)

We started witha single class, called Dog,and we factored out some of the commonality between various breeds of dogs. Thisconcept, sometimes called generalization-specialization.

Perhaps the bestsolution is to implement the barking and yodeling as interfaces.

Design Decisions

The more youfactor out, the more complex your system gets.

Conundrum

英 [kəˈnʌndrəm] 美 [kəˈnʌndrəm]

n. 谜语;难解的问题

Composition

containing otherobjects

interchangeable

Aggregation,Association, and Composition

In this book, Iconsider aggregation and association to be types of composition.

Representing Composition withUML

《The Object-Oriented Thought Process》读书笔记4

Figure 7.7 Representing composition in UML.

This signifies that a Car contains(has-a) SteeringWheel.

 

In fact, thenumber of nodes and branches that can be included in this tree structure ofclasses is virtually unlimited.

Virtual 几乎就是,非常接近实质上。见https://www.zhihu.com/question/19903523

granularity

英 [grænjʊ'lærɪtɪ] 美 [grænjʊ'lærɪtɪ]

n. 间隔尺寸,粒度

Why Encapsulation Is Fundamental to OO

How Inherit WeakensEncapsulatio­n

If the method giveDirections is changed in Cabbie, it will have a direct impact on PartTimeCabbie and any other classes that might later besubclasses of Cabbie. In this subtle way, changes to the implementationof Cabbieare not necessarilyencapsulated within the Cabbie class.

Impulse

英 [ˈɪmpʌls] 美 [ˈɪmˌpʌls]

n. 凭冲动行事;突如其来的念头;[电子]脉冲;[医]冲动,搏动

To reduce therisk posed by this dilemma, it is important that you stick to the strict is-a conditionwhen using inheritance.

例如,GUI设计中可能认为窗口是继承于矩形,但实际上是窗口包含多个矩形。

A Detailed Example ofPolymorphism

in fact, the Draw method in Shape contains no implementation.

Even though Shape has a Draw method, Circleoverrides this method andprovides its own Draw method. Overriding basicallymeans replacing an implementation of a parent with your own.

Object Responsibility

public abstract class Shape{

public abstractvoid draw();

}

 

To see the realpower of polymorphism, you can actually pass the shape to a method

that has absolutely no ideawhat shape is coming.

drawMe(circle);

drawMe(rectangle);

drawMe(star);

 

static void drawMe(Shape s) {

s.draw();

}

Conclusion

This chaptergives a basic overview of what inheritance and composition are and how they aredifferent.

Use bothcomposition and inheritance, but only in their proper contexts.