I have a:
class ServiceObject {
ServiceClass svcClass;
void execute(String operation, Map arguments, ...) {
svcClass.execute(this, operation, arguments, ...);
}
}
Then I have a subclass of that to add in synchronization:
class SynchronizedServiceObject extends ServiceObject {
void execute(String operation, Map arguments, ...) {
synchronized(lock) {
super.execute(operation, arguments, ...);
}
}
}
I also have a subclass to add a logging context
class LogContextServiceObject extends ServiceObject {
void execute(String operation, Map arguments, ...) {
MDC.set("context", myCtx);
super.execute(operation, arguments, ...);
MDC.remove("context");
}
}
However, if I want to use both features, I would need to write another subclass which has them both. You can imagine that with more added features (logging, request rewriting, etc.) I would have to write a lot of classes for every combination I need.
Instead, I’d like to ditch the subclasses and pass some sort of filter or component objects at the creation of ServiceObject. What’s a recommended pattern for this (In Java, or in general).
In terms of famous GoF book: Decorator.
After all you can combine them as you like: