何时使用反射是好的做法?

时间:2015-07-28 15:19:57

标签: java reflection

我有各种方法来验证对象。现在我运行这样的验证:

boolean error = false;
if(!methodOneValidade(myObject)) error = true;
if(!methodTwoValidade(myObject)) error = true;
if(!methodTreeValidade(myObject)) error = true;
.
.
.
if(!method23Validade(myObject)) error = true;
return error;

相反,如果我使用特定注释(@MyRule eg)注释方法并使用反射执行它们,这是一个好习惯吗? 对我来说将是聪明和实用的,但在java中使用反射是否可以接受?

示例1 - 没有反思:

public class ClientRules {
    private Client client;

    public ClienteNegocio(Cliente cliente) {
        this.client = cliente;
    }

    public boolean validateAge() {
        return client.getIdade()>=18;
    }

    public boolean validateInicialInput() {
        return client.getAporteInicial() > 999.99;
    }

    public boolean validateAccountBalance() {
        MyDatabase db = new MyDatabase();
        List<SomeObject> list = db.getClientHistory(client);
        double finalValue = 0;

        if(list.size()>=100) {
            double sum = 0;
            for(SomeObject someObject: list){
                sum = sum + someObject.value();
            }
            finalValue = sum/list.size();
            return finalValue>=5.5;
        }
        return false;
    }

    public boolean validate() {
        return validateEmail() && validateAge() && validateAccountBalance() && validateInicialInput();
    }

    public static void main(String[] args) {
        ClientRules clientRules = new ClientRules(client);
        if(clientRules.validate())
            System.out.println("Success!");
        else
            System.out.println("Fail!");
    }
}

示例2 - 使用反射

public class ClientRules {
    private Client client;

    public ClienteNegocio(Cliente cliente) {
        this.client = cliente;
    }

    @Rule
    public boolean validateAge() {
        return client.getIdade()>=18;
    }

    @Rule
    public boolean validateInicialInput() {
        return client.getAporteInicial() > 999.99;
    }

    @Rule
    public boolean validateAccountBalance() {
        MyDatabase db = new MyDatabase();
        List<SomeObject> list = db.getClientHistory(client);
        double finalValue = 0;
        if(list.size()>=100) {
            double sum = 0;
            for(SomeObject someObject: list){
                sum = sum + someObject.value();
            }
            finalValue = sum/list.size();
            return finalValue>=5.5;
        }
        return false;
    }

    public static void main(String[] args) {
        ClientRules clientRules = new ClientRules(client);
        for (Method method : clientRules.getClass().getDeclaredMethods()) {
            if (method.isAnnotationPresent(Rule.class)) {
                try {
                    boolean result = (boolean)method.invoke(clientRules);
                    if (!result) {
                        System.out.println("Fail!");
                    }
                } catch (Exception e) {
                    System.out.println("...");
                } 
            }
        }
    }
}

0 个答案:

没有答案