0

what design pattern can I use to create an object that implements a specific interface or multiple or any combination among the available interfaces? for Example say I have the following

interface A {
   void foo();
}

interface B {
   void bar();
}

interface C {
   void car();
}

class Agent {

}

I want to be able to say give me object of class Agent that implements interface A?

or

I want to be able to say give me object of class Agent that implements interfaces B?

or

I want to be able to say give me object of class Agent that implements interfaces C?

or

I want to be able to say give me object of class Agent that implements interfaces A,B?

or

I want to be able to say give me object of class Agent that implements interfaces B,C?

or

I want to be able to say give me object of class Agent that implements interfaces A,C?

or

I want to be able to say give me object of class Agent that implements interfaces A,B,C?

or

In general what I am looking for is to create an object which can have the functionality from any combination of interfaces.

user1870400
  • 6,028
  • 13
  • 54
  • 115
  • 2
    I don't think what you're asking for makes sense in Java. A class either implements an interface or it doesn't. The only way to come close is to have `Agent` implement `A`, `B`, and `C`, and then have your overriding methods throw exceptions if you have an object that isn't supposed to "implement" one of those interfaces – ajb Nov 10 '16 at 06:02
  • Do you have multiple subclasses of `Agent`? If that's the case I think you can use reflection to find what you want. Otherwise your question makes no sense. – plalx Nov 10 '16 at 06:04

2 Answers2

3

Java is a statically typed language (see this question for details). This means that a variable you describe has a type which includes a set of interfaces that are implemented. So it does not make a lot of sense to say that you want an object of a certain class to vary the interfaces it implements.

Having said that, I suspect you are trying to use interface for something for which it is not intended. For example if you want a class whose objects can have various capabilities that are determined when the object is created then there are design patterns that might work for you.

Without knowing your needs it's hard to suggest specifics but here's a possible model:

interface A {
    void foo();
}

interface B {
    void bar();
}

class Agent {
    private Optional<A> delegateA = Optional.empty();
    private Optional<B> delegateB = Optional.empty();

    public void addA(A delegate) {
        delegateA = Optional.of(delegate);
    }

    public boolean implementsA() {
        return delegateA.isPresent();
    }

    public void foo() {
        delegateA.ifPresent(A::foo);
    }
}

Agent agent = new Agent();
agent.addA(() -> System.out.println("foo"));
agent.implementsA();
agent.foo();
Community
  • 1
  • 1
sprinter
  • 27,148
  • 6
  • 47
  • 78
1

The best solution I can think of would be to have Agent implement all of your interfaces, then to use it as needed with your desired interface, such as:

A agentUsingA = new Agent();

You would then only be able to call the methods implementedfrom A.

However:

This is a really bad way of using interfaces. One of the main ideas of an interface in java is to act as a sort of "contract". For example:

Say you have two classes: AEncryptor and BEncryptor. Both of these classes implement the interface EncryptService, which has the method .encrypt(String text).

So, say you have a method somewhere called encryptString(EncryptService service, String textToBeEncrypted). This method takes an encrypting service and uses it to encrypt the string textToBeEncrypted. All this method will do is call service.encrypt(textToBeEncrypted);

Now, encryptString() dosen't care how service will encrypt the string, all it knows is that when it calls .encrypt(textToBeEncrypted) on it, it will encrypt the text.

So AEncryptor and BEncryptor could encrypt the text completely differently, but because they implement EncryptService, we know that it's encrypt method will be there, and that by calling it, it will encrypt the text.

In you're example, we won't really know what we can call on Agent, we wont have any assurance that a method (Say, .encrypt()) will actually be there.

For more information on interfaces, have a look into Oracle tutorials here and here. ;)

Anatoly Shamov
  • 2,608
  • 1
  • 17
  • 27
Gulllie
  • 523
  • 6
  • 21