通用条目,以便任何类可以调用它并获取对象

时间:2018-05-24 00:58:38

标签: java

我设计了以下界面:

public interface applyBuisnessRule {    
    public String execute(String jobCode)throws Exception;
}

并且有实现它的类:

public class AccelImpl implements applyBuisnessRule {

    private static final String Success = null;

        @Override
        public String execute(String jobCode) throws Exception {
            // TODO Auto-generated method stub
            return Success;
        }

    }

另一个是:

public class CardvaletImpl implements applyBuisnessRule {
    private static final String Success = null;

    public String execute(String jobCode) throws Exception {
        return Success;
    }

}

请告知我如何使下面的接口和类更通用意味着输入参数将是字符串类型但返回将是对象类型所以请告诉我如何使它更通用一个它应该返回对象类型如下我们可以清楚地看到它是一个stratergy设计模式,但代码中没有泛型

我所尝试的内容如下所示,请告知它是正确的方法

界面

interface applyBuisnessRule<T> {
    public T execute(String jobCode) throws Exception;
}

班级

class CardvaletImpl<T> implements applyBuisnessRule<T> {
    private static final String Success = null;

    public T execute(String jobCode) throws Exception {
        T result = null;
         // put your logic and populate result here. I believe you are not going to return Success every time as it String
        return result;
    }
}

代码执行

CardvaletImpl<String> cardvaletImpl = new CardvaletImpl<>();
String result = cardvaletImpl.execute("JOBCODE");

伙计们请告知我们是否可以进一步改进解决方案

0 个答案:

没有答案