jeudi 30 avril 2020

How do I get instance of class based on inheritance in Java/Groovy

I have the following interface inheritance scheme:

Category -> PortalCorp | Portal365

And the following interfaces:

Oracle | MsSql | PostgreSql

And finally, concrete singleton classes (only PortalCorp for simplicity):

class OracleQueryGetterPortalCorp extends DefaultQueryGetterPortalCorp implements PortalCorp, Oracle

class MssqlQueryGetterPortalCorp extends DefaultQueryGetterPortalCorp implements PortalCorp, MsSql

class PostgresQueryGetterPortalCorp extends DefaultQueryGetterPortalCorp implements PortalCorp, PostgreSql

I'm writing a function to get a concrete instance of a class. As a parameter we pass PortalCorp or Portal365 class. I guess it can be accomplished based on inheritance criteria

def <T extends Category> T getImpl(Class<T> category)
{
        Category queryGetter

        if (DDLTool.isOracle())
        {
            queryGetter = //here we need to return first instance of class which implements category (parameter either PortalCorp or Portal365) and Oracle
        }
        else if (DDLTool.isMSSQL())
        {
            queryGetter = //here we need to return first instance of class which implements category (parameter either PortalCorp or Portal365) and MsSql

        }
        else if (DDLTool.isPostgres())
        {
            queryGetter = //here we need to return first instance of class which implements category (parameter either PortalCorp or Portal365) and PostgreSql
        }

        return queryGetter

}

What's the best way to do that? Thanks in advance





Aucun commentaire:

Enregistrer un commentaire