Java – Parallel Class/Interface Hierarchy with the Facade Design Pattern

designdesign-patternsjavaobject-orientedprogramming practices

About a third of my code is wrapped inside a Facade class. Note that this isn't a "God" class, but actually represents a single thing (called a Line). Naturally, it delegates responsibilities to the subsystem behind it.

What ends up happening is that two of the subsystem classes (Output and Timeline) have all of their methods duplicated in the Line class, which effectively makes Line both an Output and a Timeline. It seems to make sense to make Output and Timeline interfaces, so that the Line class can implement them both. At the same time, I'm worried about creating parallel class and interface structures.

You see, there are different types of lines AudioLine, VideoLine, which all use the same type of Timeline, but different types of Output (AudioOutput and VideoOutput, respectively). So that would mean that I'd have to create an AudioOutputInterface and VideoOutputInterface as well. So not only would I have to have parallel class hierarchy, but there would be a parallel interface hierarchy as well.

Is there any solution to this design flaw?

Here's an image of the basic structure (minus the Timeline class, though know that each Line has-a Timeline):

enter image description here

NOTE: I just realized that the word 'line' in Timeline might make is sound like is does a similar function as the Line class. They don't, just to clarify.

Best Answer

What ends up happening is that two of the subsystem classes (Output and Timeline) have all of their methods duplicated in the Line class, which effectively makes Line both an Output and a Timeline.

The above sentence sounds like bad design.

I would expect that most of the methods in a facade class invoke at multiple subsystem classes or at least multiple methods in a single subsystem class.

If you facade is doing lots of simple delegation (example, facade method doSomething simply calls another class' doSomething), you probably should refactor.

Specifically, if a delegate class does not interact with the rest of the subsystem, the client should probably should probably interact with the delegate directly, instead of through the facade.

Related Topic