15

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

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

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

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?

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.

Thanks

brian d foy
  • 129,424
  • 31
  • 207
  • 592
rajachan
  • 795
  • 1
  • 6
  • 20

1 Answers1

19

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

The easiest way to get roles is to use Moose.

daotoad
  • 26,689
  • 7
  • 59
  • 100
  • 1
    Is Role same as Mixin in Ruby? – rpattabi Jan 20 '11 at 18:33
  • 1
    @ragu.pattabi, roles are similar to mixins. The main difference is that roles have some controls on when they can be applied--roles can require that certain methods are available in a consuming class. There are other differences, too. Perl usage of the term role maps to 'traits' in the larger world of OO research. In Perl/Moose a trait is a role applied to a method using the meta-object protocol, which is beyond the scope of this reply. I raise the point so that I can avoid the appearance of non-sequitor as I direct you to a resource on traits in general: http://scg.unibe.ch/research/traits/ – daotoad Jan 21 '11 at 15:12
  • That means roles provide mechanism like template method pattern without the baggage of inheritance. That's pretty interesting. – rpattabi Jan 22 '11 at 03:17
  • 1
    @ragu.pattabi, yes. The papers on the traits are worth a read. They go into depth on the advantages of traits vs inheritance and mixin code reuse. Roles/traits are amazing, to find out exactly why read the papers, find an implementation, and try them out. – daotoad Jan 22 '11 at 17:49
  • I will look out for traits. Thank you. – rpattabi Jan 28 '11 at 09:14