通用生成器模式类使用反射

编程入门 行业动态 更新时间:2024-10-25 12:18:39
本文介绍了通用生成器模式类使用反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

通用生成器模式类使用反射

本文发布于:2023-11-26 21:12:08,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:生成器   反射   模式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!