mardi 23 juin 2020

Cast two interfaces that have same methods but different namespaces to one generace class/interface

I have to different logging services that implement the exact same methods, however, they have different namespaces.

package com.first.namespace
public interface loggerA
{
 void info();
 void warn();
 void debug();
 void error();
}

package com.second.namespace
public interface loggerB
{
 void info();
 void warn();
 void debug();
 void error();
}

and I have a DB class that must use a logger.

public class DB{

private MyCustomLogger logger;
public DB(MyCustomLogger log){
logger = log;

}
}

And finally I have two different services:

public class serviceA{

@inject 
private LoggerA logger;

private DB myDB;

public serviceA(){
       myDB = new myDB(??); // How can I pass/cast the logger properly here.???
}

}


public class serviceB{

@inject 
private LoggerB logger;

private DB myDB;

public serviceB(){
       myDB = new myDB(??); // How can I pass/cast the logger properly here.???
}

What is the best solution to pass/cast each of those different loggers properly to myDB class that has a unique logger parameter?

Note that I have not created yet MyCustomLogger, I am still searching the best way to do write this class or to get rid of it, if there is a better solution.

Aucun commentaire:

Enregistrer un commentaire