While working on a project I've come up with some design solution. I am having a hard time relating it to any general design pattern or analyzing this situation in details. This is also prohibiting me to find any better design for this.
This is a simple analogy of my solution -
Both Kangaroo
and Tiger
can walk
but only Kangaroo
can jump
and only Tiger
can run
.
Animal<TLeg> where TLegs: ILegs
{
TLegs Legs;
void Walk(){ Legs.Walk(); }
}
Kangaroo: Animal<IJumpingLegs>
{
void Jump() { Legs.Jump(); }
}
Tiger: Animal<IRunningLegs>
{
void Run() { Legs.Run(); }
}
ILegs
{
void Walk();
}
IRunningLegs : ILegs
{
void Run();
}
IJumpingLegs : Ilegs
{
void Jump();
}
[Edit]
Problem Description:
Here, Kangaroo and Tiger share common functions and a property Legs. Therefore, I have a base class for common functions. Base class needs to have access to this property Legs
.
Functions in subclasses too rely on Legs and based on type of Legs, subclass will need to call different functions of this property.
Basically, two concrete animals need some common methods and their uncommon methods(Jump and Run) require different TYPE of a common property.
For this problem, I have come up with the solution described above. Hoping that I am able to describe my problem, I would like to know if there could be any alternate solution.
Does this problem/solution sound familiar? Is there anything that can be improved?