Основной класс, где вы можете работать как Java для целей тестирования
package stack;
public class ServiceInit
{
public static void main(String[] args)
{
new ServiceInit();
}
public ServiceInit()
{
ServiceBeanInterface xbean = ServiceFactory.getInstance().getServiceBean("X");
xbean.callService();
ServiceBeanInterface ybean = ServiceFactory.getInstance().getServiceBean("Y");
ybean.callService();
}
}
Service Factory, который возвращает компонент, который вы хотите позвонить
package stack;
public class ServiceFactory
{
/*
* you can do it with factory and class reflection if the input is always the prefix for the service bean.
*/
private static ServiceFactory instance;
// the package name where your service beans are
private final String serviceBeanPackage = "stack.";
private ServiceFactory()
{
}
public static ServiceFactory getInstance()
{
if (instance == null)
{
instance = new ServiceFactory();
}
return instance;
}
@SuppressWarnings("unchecked")
public ServiceBeanInterface getServiceBean(String prefix)
{
ServiceBeanInterface serviceBean = null;
try
{
Class<ServiceBeanInterface> bean = (Class<ServiceBeanInterface>) Class
.forName(serviceBeanPackage + prefix + "ServiceBean");
serviceBean = bean.newInstance();
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return serviceBean;
}
}
Интерфейс, который осуществляется вашими классами обслуживания
package stack;
public interface ServiceBeanInterface
{
void callService();
}
XServiceBean класс
package stack;
public class XServiceBean implements ServiceBeanInterface
{
@Override
public void callService()
{
System.out.println("I am X");
}
}
YServiceBean класса
package stack;
public class YServiceBean implements ServiceBeanInterface
{
@Override
public void callService()
{
System.out.println("I am Y");
}
}
Большое спасибо за Ur предложение. – Sudersan