通用生成器模式类使用反射 [英] Generic Builder Pattern class using reflection

查看:172
本文介绍了通用生成器模式类使用反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已经做了下面的代码:

  public class CursistBuilder {
private Cursist cursist = null;

私人CursistBuilder(){
cursist =新Cursist(未设定用户名,未设定电子邮件);


public static CursistBuilder aCursist(){
return new CursistBuilder();


公共CursistBuilder withNaam(字符串名称){
cursist.setGebruikersnaam(name);
返回此;
}

public CursistBuilder withEmail(String email){
cursist.setEmail(email);
返回此;
}

public Cursist build(){
return cursist;


这怎么实现?

$因为对象不是在 create 函数中创建的,但它不是合适的构建器模式,但是,你可以使用它作为改进的参考

  public static class Builder< T> {
public final T instance;
$ b $ public Builder(Class< T> clazz)抛出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 = clazz.getMethod(set
+ name.substring(0,1).toUpperCase()+ name.substring(1),
value.getClass());
method.invoke(instance,value);
返回此;
}

T create(){
return instance;


code


$ b

如何使用它:
是通过将类传递给构造函数来创建构建器实例


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



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



你做了什么?例如为你的类传递一些默认值并且不使用非参数构造函数

It would be great to make a implementation of the builder pattern with generics. In theory it would be possible to use reflection to make the following possible:

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

I already made the following code:

    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;
        }
    }

How can this be accomplished?

解决方案

it is not proper builder pattern, as object is not created in create function, but you could use it as reference for improvement

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;
        }
    }

how to use it: yo are creating instance of builder by passing class to constructor

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

and then use method setProperty(String name, Object value) to call setter on your object,

what you coul done? for example pass some default values for your class and dont use non-args constructor

这篇关于通用生成器模式类使用反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆