Reputation: 4565
My Java library provides an interface SomethingClient
with one implementation class SomethingClientImpl
. The interface contains methods that would be called by the app, as you'd expect.
But there is a "mirrored" interface SomethingHandler
, which contains methods that are provided by the app - application callbacks. You can imagine the app provides to the library, an object of this interface - perhaps to the Factory method from which SomethingClient
is gotten.
As an unexperienced Java designer, I'm interested to know whether there is a name for, and whether/to what extent is recommended, to also provide an interface and class that combines both concepts:
public interface SomethingClient { /*..*/ }
public interface SomethingHandler { /*..*/ }
public interface ClientAndHandler extends SomethingClient,
SomethingHandler { }
public abstract class ClientAndHandler_Impl implements ClientAndHandler {
final SomethingClient clientImpl_;
ClientAndHandler_Impl(SomethingClient clientImpl) {
this.clientImpl_ = clientImpl;
}
// TODO now all SomethingClient methods are implemented in terms of clientImpl_
// AND, SomethingHandler methods are left abstract so they are implemented by the application
}
The intent is, the application writer might prefer to extend from the ClientAndHandler_Impl
abstract class and implement the callback methods, perhaps in terms of the client (outgoing) methods. This can be done with relative ease. Assuming you'd do it, what name would you give to the ClientAndHandler
concept?
Upvotes: 1
Views: 128
Reputation: 44250
Firstly, this is not a design pattern. It does sort of contain one through: the delegation pattern.
As for combining the interfaces, I might call that a "composite marker interface" or something. I'm not aware of any accepted name for that.
This is bad for a couple of reasons. Firstly, my class could implement A and B separately but if it doesn't implement AAndB then it's not going to match the type of those methods. Generic type intersection is therefore preferable.
Secondly, I believe that requiring a composite type in the first place is indicative of bad design. Classes should do one thing. If you're implementing two distinct interfaces, you're by definition not doing one thing.
Upvotes: 0
Reputation: 5230
Your current code looks a little bit like the skeletal implementation. You can read more about it on massachusetts institute of technology or on dzon.
The idea behind it is to provide a client with a default implementation. You can find some examples in the Java-Collections-API like AbstractCollection
, AbstractSet
and AbstractMap
.
The interface ClientAndHandler
is redundant. The class ClientAndHandler_Impl
should implement SomethingClient
and SomethingHandler
.
I would name the abstract class ClientAndHandler_Impl
to AbstractClientInteraction
or ClientInteractionSkeleton
if you want to make clear that you use the skeletal implementation.
Upvotes: 1