If I have read you right then your base class has foreknowledge of your subclasses. The reason being you can either instantiate the sub class or pass a parameter to the base class and it decides what class to give you.
Assuming this is correct:
This would be my reason why you should avoid putting your base class and your factory in the same class. The purpose of a class hierarchy, in my opinion, is that if you were to line them up (single level for simplicity):
A (base)
|
B inherits
|
C inherits
Then as you go down the chain, you should be adding functionality and your classes become more *specific*.
Each class should have knowledge of its parent(s) but surely have none of its children? Your structure requires that the base class has knowledge of all classes that inherit from it (and possibly, its deepest descendants <-- though I doubt it).
I would argue that you need to think deeper regarding why you would want to merge a factory and your base class together. IMHO its better to have a separate factory that has 'knowledge' of the chain in question and can 'do the right thing'.
Of course, YMMV as it depends on your class hierarchy. I've only done horizontal class factories:
A
|
+---+--+---+---+
B C D E <---------- Factory picks b,c,d or e
Whereas yours looks like:
A (base)
|
B -+
| |
C |-- Factory can handle the chain (only B and C shown)
-+
But thats another story ;).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.