简体   繁体   中英

In Java, what do you call this pattern/idiom?

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?

Skeletal Implementation

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 .

Some Suggestions

Reduntant Interface

The interface ClientAndHandler is redundant. The class ClientAndHandler_Impl should implement SomethingClient and SomethingHandler .

Naming

I would name the abstract class ClientAndHandler_Impl to AbstractClientInteraction or ClientInteractionSkeleton if you want to make clear that you use the skeletal implementation.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM