Say for example you have a base abstract class
public abstract Foo
{
IFlyable _fly;
ISwimmable _swim;
void performSwim()
{
_swim.swim();
}
void performFly()
{
_fly.fly();
}
}
And have behaviors/algorithm that you will have in your system
interface IFlyable { void fly(); }
interface ISwimmable { void swim(); }
interface IVoteable { void vote(); }
etc etc
Now you have multiple class that implement it, with concrete of IFlyable, ISwimmable etc
class Bar: Foo { _swim = new ConcerteSwim(); }
class Baz: Foo { _fly = new ConcreteFly(); }
etc etc
One is using the strategy pattern in the base class of Foo to swap in the behaviours.
We could also use the decorator pattern to wrap it with certain behavior but since the decorator wraps it with base class, how do we actually allow the open close principle to work without touching the base class if we later on add more behaviors to objects. Since these behavior might have different signatures as we add more, rather than just for example like calling in decorator
void performSwim()
{
swimWithMoreSpeed() + foo.performSwim()
}
I guess my question is if I add more behavior, how can I not modify the base class and still be able to say add IWeaponBehavior, ISomeBehaviour to a class.
for example I want to have a class
public class XYF: Foo
{
}
But I want to give it some behavior of ISomeBehaviour, is there a way I could say wrap it with these kinds of behaviour, or more like here is an ConcreteFoo wrap it with these behaviours and now do stuff rather than implementing the interface on concrete xyz although that makes you implementing so many kinds of concretebehaviour class like swimbehaviour, nullbehaviour etc but there is no way out of it.
Is there a way to do this in design patterns? It almost seems like a hybrid of patterns.
I know it almost seems like if it walks like a duck and quacks like a duck but requires battery then you have something wrong with abstraction.
Hope this make sense.