Home > Enterprise >  A function that are existing only in one concrete implementation of an interface
A function that are existing only in one concrete implementation of an interface

Time:07-31

I have a interface with one method:

public interface MyInterface {
    public void doSomething();
}

And multiple concrete implementations of 'MyInterface':

Implementation1:

public class Implementation1 implements MyInterface {
    @Override
    public void doSomething() {
        // DO something for implementation1
        
    }
}

Implementation2:

public class Implementation2 implements MyInterface {
    @Override
    public void doSomething() {
        // DO something for implementation2
        
    }
}

and implementation3:

public class Implementation3 implements MyInterface {

    @Override
    public void doSomething() {
        // DO something for implementation3
        
    }
    
    public void doSomething(int something) {
        // DO something for implementation3
    }
}

if I want to access 'doSomething(10)' with 'MyInterface' type I need to add this function to 'MyInterface' and other implementation ('Implementation1' and 'Implementation2' in my example) must to implement this function but do nothing because I don't need this function in 'Implementation1' and 'Implementation2'.

My question is: How to proceed in this case? implement 'doSomething(int something)' and let them to do nothing in 'Implementation1' and 'Implementation2' only for 'Implementation3' or to cast instance variable to 'Implementation3' and in this way create a dependency to concrete type 'Implementation3'? I want to specify that I don't want to create dependencies on concrete implementations because I want to let the interfaces communicate with each other.

Thanks!

CodePudding user response:

One solution is to have 2 interfaces, the first one, which you already have:

public interface MyInterface {
    void doSomething();
}

and the second which extends from the first and so it already has the parameter-less method as well as the second method overload which takes an int parameter:

// already has the default method
public interface MyInterface2 extends MyInterface {
    void doSomething(int value);
}

Then if a concrete class needs both methods, it can implement the second interface:

public class Implementation3 implements MyInterface2 {

    @Override
    public void doSomething() {
        // DO something for implementation3
        
    }

    @Override
    public void doSomething(int something) {
        // DO something for implementation3
    }
}

Note that instances of the above class can still be used in places where MyInterface type is expected.

CodePudding user response:

I want to clarify the the situation. You have a interface, it represents for a behavior, in your case, the MyInterface is represents for all object can doSomething with nothing input parameter. After that, you want some your objects have another behavior: doSomething with int input parameter. You can create new interface having doSomething(int value) and only Implementation3 implements it.

public interface MyInterface2 {
    void doSomething(int something);
}
public class Implementation3 implements MyInterface, MyInterface2 {

    @Override
    public void doSomething() {
        // DO something for implementation3
        
    }

    @Override
    public void doSomething(int something) {
        // DO something for implementation3
    }
}

You can use MyInterface2.doSomething(1) with all class implemented MyInterface2. I hope it help.

  • Related