使用反射的泛型生成器模式类



用泛型实现构建器模式会很好。理论上,可以使用反射来实现以下目标:

    MyClass myClass = GenericBuilder<MyClass>.aObject()
            .withThisProperty("foo")
            .withThatProperty(4)
            .build();

我已经做了以下代码:

    public class CursistBuilder {
        private Cursist cursist = null;
        private CursistBuilder() {
            cursist = new Cursist("username not set", "email not set");
        }
        public static CursistBuilder aCursist() {
            return new CursistBuilder();
        }
        public CursistBuilder withNaam(String name) {
            cursist.setGebruikersnaam(name);
            return this;
        }
        public CursistBuilder withEmail(String email) {
            cursist.setEmail(email);
            return this;
        }
        public Cursist build() {
            return cursist;
        }
    }

如何才能做到这一点?

这不是合适的构建器模式,因为对象不是在create函数中创建的,但您可以将其用作改进的参考

public static class Builder<T> {
        public final T instance;
        public Builder(Class<T> clazz) throws InstantiationException,
                IllegalAccessException {
            super();
            this.clazz = clazz;
            this.instance = clazz.newInstance();
        }
        private final Class<?> clazz;
        Builder<T> setProperty(String name, Object value)
                throws IllegalAccessException, IllegalArgumentException,
                InvocationTargetException, NoSuchMethodException,
                SecurityException {
            Method method = clazz.getMethod("set"
                    + name.substring(0, 1).toUpperCase() + name.substring(1),
                    value.getClass());
            method.invoke(instance, value);
            return this;
        }
        T create() {
            return instance;
        }
    }

如何使用:yo正在通过将类传递给构造函数来创建生成器的实例

Builder<MyClass> builder = new Builder<>(MyClass.class);

然后使用方法setProperty(String name, Object value)调用对象上的setter

你能做什么?例如,为类传递一些默认值,不要使用非args构造函数

我不知道这是你想要的,但如果我是你,我会实现如下//

主>>

public class Main {
    public static void main(String[] args) {
        GenericBuilder gb = GenericBuilder.getInstance();
        gb=
        gb.withThisProperty("String", "AAAAAA").
        withThisProperty(4, 9).
        withThisProperty(5.8f, 6.7f).
        withThisProperty("Array", new ArrayList<String>()).build();
        System.out.println(gb.toString());
    }
};

VO>>

public class GenericBuilder {
    private Map<Object, Object> map;
    // Implementation of Singleton 
    private GenericBuilder (){
        map = new HashMap<Object, Object>();
    }
    private static class LazyLoader{
        private static final GenericBuilder INSTANCE = new GenericBuilder(); 
    }
    public static GenericBuilder getInstance(){
        return LazyLoader.INSTANCE;
    }
    //End Singleton
    public GenericBuilder withThisProperty(Object key, Object param){
        map.put(key, param);
        return this;
    }
    public Object get(Object key){
        return map.get(key);
    }
    public GenericBuilder build(){
        return this;
    }
    @Override
    public String toString() {
        StringBuffer returnVal = new StringBuffer();
        Iterator<Object> iter = map.keySet().iterator();
        while(iter.hasNext()){
            Object key = iter.next();
            returnVal.append("key >> " + key  + " value >> " + map.get(key) + "n");
        }
        return returnVal.toString();
    }
}

我重写了toString()来显示其中的内容,并声明了一个Map来将值分配给某个地方,如果你愿意的话,你可以用其他Collections来更改它。谢谢/

相关内容

  • 没有找到相关文章

最新更新