Imagine I have an abstract class Node
which has several methods and attributes. (Join a network, send a message, broadcast ...).
I want to be able to add/remove functionality to/from that Node
class (Routing functionality, mining functionality, ...)
I was thinking about using a Decorator pattern since that let's me change the behaviour of that class dynamically on runtime.
return new RoutingNode(new BaseNode(name));
But now, I' starting to think that this is not the right choice since I'm using an abstract base class over an interface.
Basically I want to know if it's possible to add functionality to an existing object without subclassing the base class. For example, I want to add Routing functionality to let a node know he's able to route incoming requests or Mining functionality to let the node perform mining tasks. But this should be interoperatable meaning I can add add or remove functionality on runtime.
What would be the most elegant and best way to handle my use case scenario.
invoke($commandName, $params)
method on your object. You can create a method to check if certain object has certain command likehasCommand($commandName)
, but this is something you should think twice about before you start implementing such things.