简体   繁体   English

根据描述创建对象

[英]Creating objects from their description

I need to create objects by reading their description from a text file. 我需要通过从文本文件中读取对象的描述来创建对象。 The objects are also in a hierarchy or can be decorative objects For example 这些对象也可以是层次结构,也可以是装饰性对象。例如

For this descrition in a text file: 对于文本文件中的此说明:

Sample.txt Sample.txt


A FileReader "fileName.txt" FileReader“ fileName.txt”

A Buffered FileInput reader "FileName.txt" 缓冲的FileInput阅读器“ FileName.txt”

A Buffered StringInput reader "TestString" 缓冲的StringInput阅读器“ TestString”


The program should read this description and return a list of suitable readers 程序应阅读此说明并返回合适的读者列表

new FileReader("fileName.txt")

new BufferedReader(new FileReader("FileName.txt"))

new BufferedReader(new StringReader("TestString"))

Is there any way to achieve it? 有什么办法可以实现? Factory pattern can be used to achieve this IMHO. 可以使用工厂模式来实现此恕我直言。

Surely you can do it with a Factory, but to me it sounds more like a job for a Builder . 当然可以使用Factory来完成它,但是对我来说,这听起来更像是Builder的工作。 (You can of course use the Factory interface, and implement it as a Builder too). (您当然可以使用Factory接口,也可以将其实现为Builder)。

A Builder is more suitable for creating a complex object hierarchy based on varying input, like in your case. 就像您的情况一样,构建器更适合根据变化的输入来创建复杂的对象层次结构。 My first idea is to use a Map<String, Class> and parse the lines from the file backwards: the last parameter is the filename, then each word (group) would map to a specific class, which you instantiate by passing it the previous result. 我的第一个想法是使用Map<String, Class>并向后解析文件中的行:最后一个参数是文件名,然后每个词(组)都将映射到特定的类,您可以通过将其传递给上一个实例来实例化该类。结果。

If all your real cases are so simple as example in your question then you can use what you like (ie what you've already used before). 如果您所有的实际案例都像问题中的例子一样简单,那么您可以使用自己喜欢的内容(即以前使用过的内容)。

But if more complex cases exist then you should provide some kind of parser first. 但是,如果存在更复杂的情况,则应首先提供某种解析器。 Even for last string in your example it's not so simple to generate necessary code: 即使对于您示例中的最后一个字符串,生成必要的代码也不是那么简单:

public Object buildObject (Parser parser, Item param) {
   if (!parser.hasNext ()) {
      return param.getValue ();
   }
   Item nextItem = parser.getNextItem (); //1st call: "TestString", 2nd call: "StringReader"
   if (nextItem.isParameter ()) {         //1st call: true, 2nd call: false 
      return buildObject (parser, nextItem);
   } else if (nextItem.isClassName () {   //2nd call: true
      Class c = Class.forName (nextItem.getStringValue ());
      Constructor ctr = c.getConstructor (param.getClassName ()); //getClassName returns "String"
      Object obj = ctr.newInstance (param.getValue ());
      return buildObject (parser, new Item (obj));
   } else {
      throw new ParseException ("Illegal input");
   }   
}

This peace of code is still very buggy (there is no try/catch, there is no some necessary logic), but it demonstrates the basic idea (and this idea is about it's not so easy task). 这样的代码和平仍然很容易出错(没有try / catch,没有任何必要的逻辑),但是它演示了基本思想(并且这个思想不是一件容易的事)。

Maybe it's worth to look for some 3rd party libs (even if you have to sacrifice your file format and accept their format). 也许值得寻找一些第三方库(即使必须牺牲文件格式并接受它们的格式)。

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

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