How to tell who a class will extend at runtime?

2

I have the class

public class Conhecimento{}

This class extends from another class "CteProc"

Only this CteProc class has "versions" example.

v200.CteProc

v300.CteProc

My problem is that in creating the Knowledge class I need to tell you if it will extend from CteProc 200 or CteProc 300 according to the version I receive. I wanted to know if it's possible to do this, change the extends or leave dynamic.

    
asked by anonymous 11.09.2017 / 14:45

1 answer

3

It is not possible for a class to inherit from A and at some point inherit from B.

If I understand what you want, use one of these approaches:

  • Bridge Design Pattern

    If the classes v200.CteProc and v300.CteProc implement a same interface (CteProc), that is, the two classes have the same methods but with different implementations.

    Implement the Knowledge class in order to receive an object from a class that implements the CteProc interface.

    According to "the version I get" you pass an object v200.CteProc or v300.CteProc.

    Internally, the Knowledge class uses the interface methods whose implementation is given by the passed object.

  • Adapter Design Pattern

    If the v200.CteProc and v300.CteProc classes do not implement a same interface, that is, the two classes have different methods (names or signatures).

    Extract an interface from the Knowledge class.

    Write two classes that implement this interface: v200.Knowledge and v300.Knowledge.

    For processing, the v200.Cognition class will internally use a v200.CteProc object and the v300.Contain a v300.CteProc.

    According to the version I receive, you use an object v200.Knowledge or v300.Knowledge.

    Customers of the old Knowledge class should refer to the interface.

11.09.2017 / 16:03