Is-a vs. is-like-a relationships("是一个"与"像是一个")关系

来源:互联网 发布:2017电影票房数据库 编辑:程序博客网 时间:2024/05/12 01:12

There's a certain debate that can occure about inheritance:Should inheritance override only baseclass methods(and not add new method that aren't in the base class)?对于继承可能会引发某种争论:继承应该只覆盖基类的方法(而并不添加在基类中没有的新方法)吗?

This would mean that the derived class is exactly the same type as the base class since it has exactly the same interface.如果这样做,就意味着导出类和基类是相同的类型,因为他们具有完全相同的接口.

As a result, you can exactly substitute an object of the derived class for an object of the base class.结果就是可以用一个导出类完全代替一个基类对象.

This can be thought of as pure substitution, and it's often referred to as the substitution principle.这可以被视为纯粹替代,通常称之为替代原则.

In a sense, this is the ideal way to treat inheritance. 在某种意义上,这是一种处理继承的理想方式.

We often refer to the relationship between the base class and derived classes in this case as an is-a relationship, because you can say,"A circle is a shape." 我经常将这种情况下的基类和导出类的关系称为is-a(是一个)关系,因为可以说"一个圆形就是一个几何形状".

A test for inheritance is to determine whether you can state the is-a relationship about the classes and have it make sense.判断是否继承,就是要确定是否可以用is-a来描述类之间的关系,并使之具有实际意义.


There are times when you must add new interface elements to a derived type, thus extending the interface.有时必须在导出类型中添加新的接口元素,这样也就扩展了接口.

The new type can still be substituted for the base type, but the substitution isn't perfect because your new methods are not accessible from the base type.这个新的类型仍然可以替代基类,但是这种替代并不完美,因为基类无法访问新添加的方法.

This can be described as an islike-a relationship(my term).这种情况我们可以描述为is-like-a(像一个)关系.

The new type has the interface of the old type but it also contains other methods, so you can't really say it's exactly the same. 新类型具有旧类型的接口,但是它还包含其他方法,所以不能说它们完全相同.

For example, consider an air conditioner.以空调为例.

Suppose your house is wired with all the controls for cooling;假设房子里已经布线安装好了所有的冷气设备的控制器,

that is, it has an interface that allows you to control cooling.也就是说,房子具备了让你控制冷气设备的接口.

Imagine that the air conditioner breaks down and you replace it with a heat pump, which can both heat and cool.想象一下,如果空调坏了,你用一个既能制冷又能制热的热力泵替换了它,

The heat pump is-like-an air conditioner, but it can do more.那么这个热力泵就is-like-a空调,但是它可以做更多的事.

Because the control system of your house is designed only to control cooling, it is restricted to communication  with the cooling part of the new object.因为房子的控制系统被设计为只能控制冷气设备,所以它只能和新对象中的制冷部分进行通信.

The interface of the new object has been extended, and the existing system doesn't know about anything except the original interface.尽管新对象的接口已经被拓展了,但是现有系统除了原来接口之外,对其他东西一无所知

.


Of course, once you see this design it becomes clear that the base class "cooling system" is not genneral enough, and should be renamed to "temperature control system" so that it can also include heating -- at which point the substitution principle will work.当然,在看过这个设计之后,很显然会发现,制冷系统这个基类不够一般化,应该将其更名为"温度控制系统",使其可以包括制热功能,这样我们就可以套用替代原则了.

However, this diagram is an example of what can happen with design in the real world.这张图说明了在真实世界中进行设计时可能会发生的事情.


When you see the substitution principle it's easy to feel like this approach(pure substitution) is the only way to do things, and in fact it is nice if your design works out that way.当你看到替代原则时,很容易会认为这种方式(纯粹替代)是唯一可行的方式,而且事实上,用这种方式设计是很好的.

But you'll find that there are times when it's equally clear that you must add new methods to the interface of a derived class.但是你会时常发现,同样显然的是你必须在导出类的接口中添加新方法.

With inspection both cases should be reasonably obvious.只要仔细审视,两种方法的使用场合应该是相当明显的.




0 0