I have two separate inheritance hierarchies that represent objects that can be "matched" to each other to consitute a FooBarMatch (which contains references to the matched FooBase and Bar partners):
_____________
| FooBarMatch |
|-------------|
---<>|FooBase |<>---
| |Bar | |
| |_____________| |
| |
| |
______|_______ ________|_____
| <<abstract>> | | Bar |
| FooBase | |______________|
|______________|
^
|
--------------------
_______|______ _______|______
| FooA | | FooB |
|______________| |______________|
The actual logic for determining whether a FooBase
is a match for a Bar
depends on what subtype FooBase
ends up being (if FooBase
is a FooA
we need to execute different logic than if it's a FooB
). But all FooBase
objects can be compared to a Bar
to see if they are a match.
I am currently handling this as follows: by creating an IBarMatchable
interface with an IsMatch(Bar) : bool
method, which is implemented by an abstract method in FooBase
. FooA
and FooB
then contain different implementations for IsMatch(Bar)
. It looks like this:
_____________
| FooBarMatch |
|-------------|
_______________ ---<>|FooBase |<>---
| IBarMatchable | | |Bar | |
|---------------| | |_____________| |
| IsMatch(Bar) | | |
|_______________| | |
^ ______|_______ ________|_____
| | <<abstract>> | | Bar |
---| FooBase | |______________|
|--------------|
|*IsMatch(Bar)*|
|______________|
^
|
--------------------
_______|______ _______|______
| FooA | | FooB |
|--------------| |--------------|
|IsMatch(Bar) | |IsMatch(Bar) |
|______________| |______________|
Now to determine matches we can only concern ourselves with IBarMatchable
. This seems to accomplish what I want, however I am curious if this is "good design" or not-- I'm most concerned that it now seems to
Introduces a dependency on
Bar
toFooBase
to support functionality that is ultimately outside ofFooBase
(FooBase
itself doesn't care about matching at all).FooBase
otherwise has nothing to do withBar
.Introduces a second "reason to change" (i.e. the way we define a match changes) to
FooBase
-- so potentially a SRP violation.
Does this violate SOLID? Is there a better way to design this that does not require FooBase
to depend on Bar
?
FooBase
)IsMatch
method works (at least, roughly), it is impossible to say if it is a good or bad choice to place it in theFoo
hierarchy. Could it easily be implemented outside ofFooA
andFooB
in terms of their public methods/properties, or does it need access to internals of those objects? Please clarify.