简体   繁体   English

Java反射模式不好?

[英]Java Reflection bad pattern?

Let's say I have a lot of similar classes (Units in a RTS in this example), that is, the class Unit , and the subclasses UnitA , UnitB , UnitC etc. 假设我有很多类似的类(在此示例中为RTS中的Units),即Unit类,以及子类UnitAUnitBUnitC等。

All Unit classes have the following constructor (including Unit) 所有Unit类都具有以下构造函数(包括Unit)

public class UnitX {
    public UnitX(FileReader fr) {
         ...read parameters for constructing the unit...
    }
}

My file containing the parameters have the form 我的包含参数的文件具有以下形式

UnitX params
UnitY params
....

and creating a list of all units in a file would be a while-loop like 并在文件中创建所有单位的列表将是一个while循环,例如

Class[] params = {FileReader.class};
while(fr has more to read) {
    String unitType = fr.getString();
    Unit u = (Unit)
java.lang.reflect.Constructor constr = Class.forName(unitType).getConstructor(params);
    Unit u = (Unit)constr.newInstance(new Object[]{fr});
    list.add(u);
}

I realized that I use this pattern very often when I create objects from files. 我意识到从文件创建对象时经常使用这种模式。 My question is, is this a bad pattern? 我的问题是,这是一个不好的模式吗? Is there a better way to do this? 有一个更好的方法吗?

That a case for the factory pattern : 这是工厂模式的一种情况:

java.lang.reflect.Constructor constr = Class.forName(unitType).getConstructor(params);
Unit u = (Unit)constr.newInstance(new Object[]{fr});

could be changed into 可以变成

Unit u = UnitFactory.create( unitType, fr );

The factory is then a list of if/else though. 然后,工厂是if / else的列表。

The code itself is fine. 代码本身很好。 Since constructors cannot be part of a traditional codified interface, the next best thing is the consistent reflective interface. 由于构造函数不能成为传统的编码接口的一部分,因此,下一个最好的事情就是一致的反射接口。

However, if you are repeating this code in many places, then that's not so good. 但是,如果您在许多地方重复此代码,那不是很好。 You might try to centralize that into some kind of factory or builder that provides Unit names from the file, along with the parameters defined for that unit, and pair this with a handler implementation that instantiates the unit with the parameters provided via a UnitFactory. 您可以尝试将其集中到某种工厂或生成器中,该工厂或生成器提供文件中的单元名称以及为该单元定义的参数,并将其与处理程序实现配对,该处理程序实现将通过UnitFactory提供的参数实例化该单元。 The UnitFactory uses reflection to instantiate the named Unit and provide it with the parameters. UnitFactory使用反射实例化命名的Unit并为其提供参数。

This allows reuse, and decouples reading the file from instantiation, and the method of instantiation. 这允许重用,并使读取文件与实例化以及实例化方法脱钩。

I think your implementation is ok. 我认为您的实施还可以。 Another approach: the textfile is a simple form of a DSL (Domain Specific Language) 另一种方法:文本文件是DSL (特定于域的语言)的一种简单形式

You could switch to a more dynamic jvm compatible language. 您可以切换到更具动态性的jvm兼容语言。 Dynamic languages like groovy (my favourite ;-) ), javascript (Rhino,...), BeanShell, jython, ... can more easily be used to implement Domain specific languages (DSL). 诸如groovy(我最喜欢的;-),javascript(Rhino,...),BeanShell,jython等动态语言可以更轻松地用于实现特定于域的语言(DSL)。 For more complex DSL's, you could take a look at the eclipse XText project. 对于更复杂的DSL,可以看一下Eclipse XText项目。

This is a simple, cut down serialization. 这是一个简单的简化序列化。 I would say that this is fine if it fits your purposes. 我会说,如果适合您的目的,这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM