G'day tusker,

Welcome to the Monastery.

Using class names like A, B, C and D, doesn't make it easy to visualise your overall class structure.

In general terms, A::B::C doesn't imply that B inherits from A nor that C inherits from A::B; it does, however, imply that B is somehow related to A and C is somehow related to A::B.

Having said that, it often makes sense to use naming to logically separate concerns. For instance, A might compose B::C or B::D while both B::C and B::D inherit from B.

Consider these contrived classes:

Building Building::House Building::Shed Building::Garage Vehicle Vehicle::Car Vehicle::Train Vehicle::Motorcycle Door Door::Hinged Door::Sliding Door::Roller

The various Building::* and Vehicle::* classes could compose zero, one or more Door::* classes.

By sticking with your current class structure, you could end up with a huge number of classes such as this:

Building Building::House Building::House::Door Building::House::Door::Hinged Building::House::Door::Sliding Building::Shed Building::Shed::Door Building::Shed::Door::Hinged ... Vehicle Vehicle::Train Vehicle::Train::Door Vehicle::Train::Door::Hinged Vehicle::Train::Door::Sliding ...

This quickly starts to become unmanageable. Building::House::Door, Building::Shed::Door and Vehicle::Train::Door will probably have duplicated code; as will Building::House::Door::Hinged, Building::Shed::Door::Hinged, Vehicle::Train::Door::Hinged, and so on. Throw in a variety of Lock, Handle, etc. subclasses and you're facing a maintenance nightmare.

I'd recommend spending a bit more time on your class design before writing any code. Class names with duplicated portions will probably equate to duplicated code which you should endeavour to avoid.

[A note on nomenclature. You used the term "composition" in your question and I continued its use; however, in the strictest sense, that may be inappropriate in some cases. There are other types of relationships (e.g. "aggregation", "association", "delegation"): at times, it's important to be specific about which you're referring to. You may already know about these; if not, most introductory OO texts will describe them.]

— Ken


In reply to Re: Perl modules hierarchy and composition by kcott
in thread Perl modules hierarchy and composition by tusker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.