Posted on 2008-06-24
Last Modified: 2013-11-26
HI Experts

We have a .NET application (VS 2008) that uses the Factory and Command Patterns to generate & invoke commands. Commands are logged, may have or not have timer support and are classified to categories which are also hierarchical (i.e. in category A you have commannd A1 and command A11 that is inherited from command A1 and so on...)

The difficulty with this is debug, since there are about 7 levels in the hierarchy , it takes time to navigate   and find the problem origin.

Questions :

1) When I did OO courses, I remember that they strongly recommended not to pass the 3 object hierarchy depth , is that true?

2) What design alternatives can you recommend in order to make our commands hierarchy shorter ?

Question by:elimesika
  • 2
  • 2
LVL 21

Expert Comment

ID: 21866714
1) One of my favorite books:
  "Heuristic 5.4 In theroy, inheritance hierarchies should be deep - the deeper, the better.
  Heruistic 5.5 In practice, inheritance hierarchies should be no deeper than an average person can keep in his or her short-term memory.  A popular value for this depth is six."  I must be more feeble minded than most as I like 3.

2) I can't read the jpg but it looks like you might be suffering a class explosion.  The command pattern purpose is to encapsulate requests, hiding implementation from the caller.  Possibly a narrower command hierarchy using classes representing individual functionality within a command and then aggregating functionality via Mediator pattern.
LVL 19

Author Comment

ID: 21868871

Thanks for your respond.
Given that all commands are isolated and have no commnad-to-command commun ication, it seems that the mediator pattern will not help to simplify this.

Is there a way to use interfaces when applicable instead of classes in the inheritance tree to shorten the depth of the inheritance tree ???

LVL 21

Accepted Solution

mastoo earned 500 total points
ID: 21869841
I meant that you'd have a smaller number of command classes, and when they are constructed they get different components added.  I was thinking the mediator within the command would orchestrate those pieces, but on second thought that would be overkill.

For instance, during construction the factory feeds the command constructor a CLogger.  CLogger might be a base class reference to a CLogPrinter or CLogFile.  I'm not sure if this example applies to your case but it would be one less dimension for the command hierarchy and it helps with another golden rule: A class should capture one and only one key abstraction (i.e., a command shouldn't have to worry about implementation of logging).
LVL 19

Author Closing Comment

ID: 31470387

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question