OO从字符串表示创建对象的方法

时间:2023-01-27 23:54:12

I like to keep my design clean and OO: I have an abstract java class (Block) with a method to return a string representation of the object to be able to save it to a file for instance. I also have loader functions (currently external to Block and it's subclasses) to turn a string representation of a object back into the object it was. How do I do this without if, else if, else if, etc... The getIdentifier() method returns a unique string identifying the block type in a 4 letter String. I wish to keep it this way for ease of use later on. This is "srce" for a source block for instance. The string representation is then srce,1,0,1,1 for instance with the parameters in commas defining the state of this specific block.

我喜欢保持我的设计干净和OO:我有一个抽象的java类(Block),它有一个方法来返回对象的字符串表示,以便能够将它保存到文件中。我还有加载器函数(当前在Block及其子类外部)将一个对象的字符串表示形式转换回它所在的对象。如果没有if,else if else else if等,我该怎么做... getIdentifier()方法返回一个唯一的字符串,用于标识4字母String中的块类型。我希望保持这种方式以便以后使用。例如,这是源块的“srce”。然后字符串表示为srce,例如1,0,1,1,其中参数以逗号定义该特定块的状态。

public abstract class Block{
    public abstract String getIdentifier();
    public abstract String getStringRepr();
}

I want a polymorphic way of turning a String representation from above into a java object. Right now, defining a new Block type with it's behavior (in other methods) is easy, however the string to object function still needs to be declared elsewhere which bugs me.

我想要一种将上面的String表示形式转换为java对象的多态方式。现在,使用它的行为(在其他方法中)定义一个新的Block类型很容易,但是对象函数的字符串仍然需要在别处声明,这会让我感到烦恼。

Edit: I don't want a different serialisation of this object. I need to be able to produce them using a shorthand, human readable format.

编辑:我不想要这个对象的不同序列化。我需要能够使用简写的,人类可读的格式来制作它们。

2 个解决方案

#1


0  

Use JSON serialization for that, here is an example of using Jackson:

使用JSON序列化,这是使用Jackson的一个例子:

    // create object mapper
    com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();

    // write object to file
    Block block = new SomeBlockImpl();
    File outputFile = new File("output location goes here");
    objectMapper.writeValue(outputFile, block);

    // read object from file
    File inputFile = new File("input location goes here");
    Block savedBlock = objectMapper.readValue(inputFile, Block.class);

#2


-1  

I've done it: I've added yet another abstract method which registers the block's identifier with a Class higher up. This maps the identifier to the specific Block class thus allowing me to call the correct constructor without having to if else my way through all options for the identifier.

我已经完成了:我添加了另一个抽象方法,它将块的标识符注册为更高的类。这将标识符映射到特定的Block类,从而允许我调用正确的构造函数,而不必通过标识符的所有选项。

#1


0  

Use JSON serialization for that, here is an example of using Jackson:

使用JSON序列化,这是使用Jackson的一个例子:

    // create object mapper
    com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();

    // write object to file
    Block block = new SomeBlockImpl();
    File outputFile = new File("output location goes here");
    objectMapper.writeValue(outputFile, block);

    // read object from file
    File inputFile = new File("input location goes here");
    Block savedBlock = objectMapper.readValue(inputFile, Block.class);

#2


-1  

I've done it: I've added yet another abstract method which registers the block's identifier with a Class higher up. This maps the identifier to the specific Block class thus allowing me to call the correct constructor without having to if else my way through all options for the identifier.

我已经完成了:我添加了另一个抽象方法,它将块的标识符注册为更高的类。这将标识符映射到特定的Block类,从而允许我调用正确的构造函数,而不必通过标识符的所有选项。