我可以构建一个知道该子类的某些属性值的子类实例吗
我有一个所谓的属性类的层次结构,它代表我的域模型的单个属性。每个 Attribute 类都有一个唯一的 String id 属性。 我还有一个 AttributeFactory,它有一个根据 id 检索 Attribute 实例的方法。
public class Attribute {
private String id;
private AttributeType type;
.......
.......
public Attribute(String id){
this.id=id;
}
public void setID(String id){
this.id=id;
}
public String getID(){
return this.id;
}
.......
.......
}
Factory 的接口看起来像
public interface IAttributeFactory {
Attribute getAttributeByID(String id);
}
该接口的一个实现可以具有例如 HashMap 作为所有已定义属性的存储库。
属性子类的一个例子是:
public class ClientCode extends Attribute {
public ClientCode(){
super("clientCode");
this.setType(AttributeType.CHAR_TYPE);
}
........
........
}
问题是,我们是否可以使用反射或任何其他东西来构建属性子类的新实例,只知道该子类的 id,因此实现上面定义的接口,而不需要定义 HashMap 并添加每个新定义的 HashMap 子类?
先感谢您 丹尼尔
I have a hierarchy of so named Attribute classes, that represents single properties of my Domain Model. Each of this Attribute classes has a unique String id property.
I Also have an AttributeFactory that has a method to retrieve instances of Attribute based on id.
public class Attribute {
private String id;
private AttributeType type;
.......
.......
public Attribute(String id){
this.id=id;
}
public void setID(String id){
this.id=id;
}
public String getID(){
return this.id;
}
.......
.......
}
The interface of the Factory looks like
public interface IAttributeFactory {
Attribute getAttributeByID(String id);
}
One implementation of this interface could have for example a HashMap as a repository of all the defined Attributes.
An example of Attribute subclass is:
public class ClientCode extends Attribute {
public ClientCode(){
super("clientCode");
this.setType(AttributeType.CHAR_TYPE);
}
........
........
}
The question is, can we using reflection or any other thing build new instances of subclasses of attributes only knowing the id of that subclasses, so implement the interface defined above, without the need to define the HashMap an add each new defined subclass to the HashMap?
Thank you in advance
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 ID 是由客户端代码任意选择的,那么就不可能知道哪个 ID 属于哪个 Attribute 子类。然而,如果你能以某种方式强制执行一个模式,那么只需要知道 ID 就可以实例化子类。你可以通过反思来做到这一点。
下面是如何通过反射创建新实例的示例代码:
[这只是示例代码;例如,可以在实际实现中改进错误处理。]
基本上,您必须编写convertIDToClassName(String) 方法来将ID 转换为类名。如果没有这种关联,就不可能将两者联系起来。
另一个想法是,属性的ID是由你计算的;这意味着 Attribute 类设置自己的 ID 字段。结果,您直接将子类名称和 ID 字段链接在一起。因此,可以通过 ID 实例化子类。
无论如何,个人评论是避免反射并坚持在代码中创建类(可能在工厂类中的某个地方取出实例化代码,以便您的主代码得到 因修改而关闭)。
If IDs are arbitrarily chosen by the client code, then it wouldn't be possible to know which ID belongs to which Attribute subclass. However, if you can somehow enforce a pattern, then yes it would be possible to instantiate subclasses by only knowing the ID. You can do that through reflection.
The sample code below how to create a new instance via reflection:
[It is just sample code; for example the error handling can be improved in the actual implementation.]
Basically, you will have to write the convertIDToClassName(String) method to convert an ID to the class name. Without such association, it wouldn't be possible to link the two.
Another idea is that the ID of an attribute is calculated by you; meaning that the Attribute class sets its own ID field. As a result, you directly link the subclass name and the ID field together. Therefore, it would be possible instantiate subclasses via the ID.
In any case, a personal comment would be avoid reflection and stick to creating the classes in the code (probably take out the instantiation code somewhere like in a factory class, so that your main code gets closed for modification).
您需要一个工厂来创建 Attribute 的特定子类的实例,对吗?让我们使用典型的工厂模式来完成此操作。
You need a factory to create an instance of specific subclass of Attribute, right? Let's do this using a typical factory pattern.