简体   繁体   中英

Can i use reference of implementing class into method declared in Interface in java

I have seen some classes desinged in a way where interface having the declaration of method which having a parameter of its child class type (implementing class) like below:

Interface A defined as has the method setData which has parameter of type AAbstract which is subclass of concrete class AImpl which implements interface A:

 public interface A{    
        void setData(AAbstract ref);
    }

where we have implementing class AImpl:

 public class AImpl implements A {
    private AAbstract instance; 
        public void setData(AAbstract ref) {
            this.instance= ref;
        }
    }

and again abstract implementaion like:

 public abstract class AAbstract extends AImpl {
       public abstract void dummy(String msg);
    }

personally I do not see it as correct design want to know the expert comments from design perspective.

The big problem with this kind of pattern is coupling - class AImpl and abstract class AAbstract are tightly coupled and circularly dependent. Each level in the inheritance hierarchy is aware of the other levels so the potential for a change in one to affect the others is quite high. Normally, you would try to design classes which are not dependent on, or aware of, their children. There are exceptions, where the number of implementations of a class are known in advance and don't often change, but they're rare. In this case, the the circular dependency between AAbstract and AImpl suggests that they might as well be combined into a single class.

To be honest, I don't see what value the pattern would give. AImpl contains an instance of AAbstract , which extends AImpl and so also contains an instance of AAbstract ... so is it AAbstract s all the way down? My guess is that there's a null at some level, and nobody likes null.

The answer would be simply use more descriptive implementation names.


AImpl and AAbstract suggests that you are creating an interface for a single implementation. In that case, YAGNI applies and the abstraction is not needed. Abstraction allows for inversion of control, allowing you to specify different types of A . If there is only one implementation, the abstraction is pretty much useless. Of course that's simply in theory, since people create abstractions for easy mocking in tests.

Abstractions allow for different implementations. If you use AImpl , what would you name the other implementations?

You could instead use ADefault , which suggests it is a default implementation (if other implementations dont fit the trick, use a default), but this is still the same problem as you specified, just a bit watered down.

Ask yourself: " If I had multiple implementations, what would be the differences between them? Why would I need multiple implementations? ". That'll help you find a more descriptive name for each implementation, allowing you to name the abstraction A , then definine different types of A based on the reason for their existance.

Given an example of the use of A , I'm sure I could easily find better names for the implementation. For example, Graphics is an abstraction, OpenGL and DirectX are implementations. Both implementations exist for different purposes, and it's described in their names.

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