Java中匿名内部类的替代方案

时间:2010-10-29 13:03:01

标签: java

鉴于下面的实体 - 是否有可能以某种方式避免使用/***** Get rid of this anonymous inner class ******/标记的匿名内部类。 问题是“SomeEntity”可以包含数百个字段,并且有多个类实现此接口,每个类都需要元信息。这意味着有数千个匿名内部类开始生成,导致我的构建时间非常慢,并且系统的启动时间非常慢,因为类加载器加载了所有类。

我无法使用反射,因为我的类在发布之前会被混淆

public interface SomeEntity {

    ///// boolean someField
    // getter
    void someField(boolean someField);

    // setter
    boolean someField();

    final static String someField = "someField";

    ///// boolean someField2
    // getter
    void someField2(int someField2);

    // setter
    int someField2();

    final static String someField2 = "someField2";

}


public class SomeEntityImpl implements SomeEntity {
    boolean someField = false;
    int someField2 = 0;

    @Override
    public void someField(boolean someField) {
        this.someField = someField;
    }

    @Override
    public boolean someField() {
        return this.someField;
    }

    @Override
    public void someField2(int someField2) {
        this.someField2 = someField2;
    }

    @Override
    public int someField2() {
        return someField2;
    }
}


public class SomeEntityMeta {

    public static interface Meta<T> {
        T get(SomeEntity x);
        void set(SomeEntity x, T t);
        void copy(SomeEntity from, SomeEntity to);
    }

    Meta<Boolean> metaForSomeField = new Meta<Boolean>() {

        @Override
        public Boolean get(SomeEntity x) {
            return x.someField();
        }

        @Override
        public void set(SomeEntity someEntity, Boolean newFieldValue) {
            someEntity.someField(newFieldValue);
        }

        @Override
        public void copy(SomeEntity from, SomeEntity to) {
            to.someField(from.someField());
        }

    };
    Meta<Integer> metaForSomeField2 = new Meta<Integer>() {

        @Override
        public Integer get(SomeEntity someField2) {
            return someField2.someField2();
        }

        @Override
        public void set(SomeEntity someEntity, Integer newFieldValue) {
            someEntity.someField2(newFieldValue);
        }

        @Override
        public void copy(SomeEntity from, SomeEntity to) {
            to.someField2(from.someField2());
        }

    };

    Map<String, Meta<?>> metaMapForAllFields = new HashMap<String, Meta<?>>() {
        private static final long serialVersionUID = 1L;
        {
            put(SomeEntity.someField, metaForSomeField);
            put(SomeEntity.someField2, metaForSomeField2);
        }
    };

}    
    // used as follows:


        public final static void main(String[] args) {
            SomeEntity someEntity = new SomeEntityImpl();
            SomeEntityMeta entityMeta = new SomeEntityMeta();
            /// 
            Map<String, Object> fieldNameValueMap = getFieldNameValueMapFromExternalsSystem();

            for ( Entry<String, Object> fieldNameValueEntry : fieldNameValueMap.entrySet()) {
                Meta<Object> meta = (Meta<Object>) entityMeta.metaMapForAllFields.get(fieldNameValueEntry.getKey());
                meta.set(someEntity, fieldNameValueEntry.getValue());
            }
        }   

1 个答案:

答案 0 :(得分:2)

我建议让SomeEntity成为简单对象的组合,这样就不需要你的小元类了。

(有人会很快提到反射或JavaBeans,我建议先尝试解决设计问题。)