开发者

Is it good to return a Abstract class from a Interface

I have a interface called ABC. I also have an abstract class PQR and there are 3 classes which extend this abstract class.

For example:

abstract class PQR {
    //...
}

interface ABC {
    //other methods....
    PQR returnRightClass();
}

class X extends PQR {
}

class Y extends PQR {
}

class Z extends PQR {
}

Now there are classes which implement the ABC interface开发者_JAVA技巧. So what should I do to get the right class from X, Y, and Z?


There is nothing wrong with returning an abstract class, though it is recommended to use interfaces as much as possible. If you can make PQR an interface, then do so. But if not, no worry.

Now there are classes which implements ABC interface. So what should I do to get the right class from x ,y ,z?

Just make sure each implementation of returnRightClass() in each class implementing ABC returns a proper instance of PQR (x,y or z).

EDIT

To answer your question:

public MyClassX implements ABC {

    public PQR returnRightClass() {
        return new x();
    }

}

Or

public MyClassY implements ABC {

    public PQR returnRightClass() {
        return new y();
    }

}

Or

public MyClassZ implements ABC {

    public PQR returnRightClass() {
        return new z();
    }

}


You can return an abstract class. There's no law about returning interfaces or abstract classes, it depends on your software design:

One way to to return your x, y, z is to use the Factory Pattern.

Example (pseudocode):

public class PQRFactory {

    public PQR getPQR(condition) {

        switch (condition) {
            case condition_x :
                return new x();
            break;

            case condition_y :
                return new y();
            break;

            case condition_z :
                return new z();
            break;

            default : return null;
        }
    }
}

If you don't want to use condition then you can use a Builder pattern to do what @JVerstry posted.

When your classes implement ABC interface, you can use the Strategy pattern to pass the factory to be used by the ABC implementors.

Example (pseudocode):

public abstract class AbstractABC implements ABC {

    protected PQRFactory factory;

    protected AbstractABC() {
        factory = new PQRFactory();
    }

    protected AbstractABC(PQRFactory factory) {
        this.factory = factory;
    }

}

public class ABCImpl1 extends AbstractABC() {

    //Override constructors needed to construct this class
}

Hope this help.


Its good to use abstract or interface for return value.

you can use instanceof operator for type matching.

like in some cases

PQR myObj = new z();

(myObj instanceof z) // returns true
(myObj instanceof x) // returns false
(myObj instanceof y) // returns false


I am not sure what is your design idea behind that, but wouldn't it just be easier to have your X,Y,Z, classes implement the interface itself? You still could put any common code in your abstract class that the three can derive from. In general I think is it better to reference interfaces then Abstract classes, in case you want to adjust it in the future. Just a thought.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜