本文介绍了我应该使用继承还是组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想保持这一点。我建造了一个有两个房间的HouseA,比如 BedRoom StudyRoom ,两者都来自一个名为<$ c的基类$ C>间。
BedRoom StudyRoom 有一个名为 House 。此外,房屋内的任何房间只能通过父母进入任何其他房间。如果 BedRoom 必须访问 StudyRoom 的任何属性,则必须通过 House (即父母),反之亦然。

I would like to keep this one short. I build a HouseA that has two rooms, say BedRoom and StudyRoom, both deriving from a base class called Room.BedRoom and StudyRoom have a same parent called House. Also, any room in a house can access any other rooms only through the parent. If BedRoom has to access any attribute of StudyRoom, it has to go only via House (i.e. parent) and vice-versa.

HouseA ISA House
HouseA HAS BedRoom and StudyRoom.
BedRoom ISA Room
StudyRoom ISA Room

现在问题:我们说,我建立了另一个家(比如说 HouseB ),这与上面完全相同,但只做了一次改动。我不想要两个单独的房间(即 BedRoom StudyRoom ),而是一个房间(<$ c) $ c> MasterRoom )具有这两种设施。
为了代码可重用性,我可以考虑以下设计选项:

Now the Problem: Let's say, I build another home (say HouseB), which is a exactly the same as the above, but with one change. I don't want two separate rooms (i.e. BedRoom and StudyRoom), but instead a single room (MasterRoom) which has both these facilities.For the sake of code reusability, I could think of the following design options:

Option-1:
HouseB ISA House
HouseB HAS MasterRoom
MasterRoom ISA Room

这里我失去重用我为 HouseA创建的 BedRoom StudyRoom 属性的能力。请注意, BedRoom StudyRoom 的大部分属性需要在 MasterRoom中重新实现无论如何,从而导致代码重复。

Here I lose the ability to reuse the attributes of BedRoom and StudyRoom that I created for HouseA. Note that most of the attributes of BedRoom and StudyRoom need to be reimplemented in MasterRoom anyway, thereby resulting in code duplication.

Option-2:
HouseB ISA House
HouseB HAS MasterRoom
MasterRoom ISA Room
MasterRoom HAS LogicalBedroom
MasterRoom HAS LogicalStudyRoom
LogicalBedroom ISA BedRoom
LogicalStudyRoom ISA StudyRoom

这样,我使用组合使我可以重用我的大部分代码(我有几千行代码可以重复使用),但是问题是 BedRoom 是一个具体的类, logicalBedRoom 可能会发现某些属性不合适,可能会被强制覆盖方法他们什么都不做例如, Bedroom-> noOfSides()= 4 logicalBedRoom-> noOfSides()= ?? 。这是继承的好用吗?

This way, I use composition so that I could reuse most of my code (I have several thousand lines of code that I could reuse), but the problem is that BedRoom is a concrete class and logicalBedRoom may find certain attributes not suitable and may be forced to override methods so that they do nothing. For example, Bedroom->noOfSides() = 4 and logicalBedRoom->noOfSides() = ??. Is this a good use of inheritance?

我的实际设计是针对一个复杂的芯片,结合了两个芯片的功能(我使用了House(主板)和Room(芯片) ) 比喻)。我在Object Oriented Perl中编码,我非常感谢任何其他设计建议。

My actual design is for a complex chip that combines the functionality of two individual chips (I used House (motherboard) and Room (chip) analogy). I code in Object Oriented Perl and I would really appreciate any alternate design suggestions.

谢谢

推荐答案

为什么不使用角色来实现这个目标:

Why not use roles to achieve this:

House A has a Bedroom
Bedroom does SleepingArea
House has a Studyroom
Studyroom does ComfyArea

House B has a MasterRoom
MasterRoom does SleepingArea and ComfyArea

获得是使用。

这篇关于我应该使用继承还是组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-09 00:11