简体   繁体   English

按功能的文件夹和装饰器设计模式

[英]Folder-by-feature and decorator design pattern

Folder-by-feature and decorator pattern 按功能的文件夹和装饰器模式

If I have a Parser that has an associated Stream , when I progress through the Tokens , I am mutating the state of the object as the Stream is moving forward. 如果我有一个具有关联的StreamParser ,则当我逐步处理Tokens ,我将在Stream向前移动时改变对象的状态。 I want to add app-specific functionality to this parser to maintain the principle of single responsibility, so I thought the Decorator design pattern was an appropriate choice. 我想向该解析器添加特定于应用程序的功能,以维持单一职责的原则,因此我认为Decorator设计模式是一个适当的选择。 I now wrap the Parser in a ParserDecorator(Parser) and add specific functions, such as readPerson() or readItem() that maps the data to an Object and returns that Object . 现在,我将Parser包装在ParserDecorator(Parser)并添加特定的功能,例如将数据映射到Object并返回该Object readPerson()readItem()

First, I considered making a static function within Person that takes the Parser as an argument; 首先,我考虑在Person中创建一个静态函数,以Parser作为参数。 however, the function progresses the stream and I decided that would not be thread safe in all cases. 但是,该函数将继续处理流,因此我决定并非在所有情况下都是线程安全的。 As the ParserDecorator , though, return objects of different types, there is no way to keep it logically grouped with Person only. 但是,由于ParserDecorator返回不同类型的对象,因此无法将其仅与Person逻辑分组。 So if I change the Person class later, I have to remember to change how it is read by the ParserDecorator , which is located in a different part of the program and a new developer on the project may not check that location. 因此,如果以后更改Person类,我必须记住要更改ParserDecorator读取它的方式,该ParserDecorator位于程序的不同部分,项目中的新开发人员可能不会检查该位置。 Typically I deploy folder-by-feature so all the changes that would need to be made are located together, but it does not work in this case. 通常,我按功能部署文件夹,因此所有需要进行的更改都放在一起,但是在这种情况下不起作用。

Is there a better solution to this case, working with a purely object-oriented approach? 使用纯面向对象的方法,是否有更好的解决方案? Right now I have a separate feature-folder called util that has this class. 现在,我有一个名为util的独立功能文件夹,其中包含此类。

EDIT: Example code: 编辑:示例代码:

public class JsonParserDecorator {
    private final JsonParser jsonParser;

    public JsonParserDecorator(JsonParser jsonParser) {
        this.jsonParser = jsonParser;
    }

    public Item readItem() throws IOException {
        Item item = new Item();

        if (jsonParser.currentToken() != JsonToken.START_OBJECT) {
            throw new IOException("JSON object expected");
        }

        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jsonParser.currentName();
            jsonParser.nextToken();

            switch (fieldName) {
                case "itemId":
                    item.setId(jsonParser.getLongValue());
                    break;
                case "description":
                    item.setDescription(jsonParser.getText());
                    break;
                default:
                    break;
            }
        }

        return item;
    }
}

I have come to a solution after thinking about it overnight. 经过一整夜的思考,我得出了一个解决方案。 I created a new class called ItemJson and added read(JsonParser jsonParser) and write(JsonGenerator jsonGenerator, Item item) methods. 我创建了一个名为ItemJson的新类,并添加了read(JsonParser jsonParser)write(JsonGenerator jsonGenerator, Item item)方法。 This keeps all of the similar classes/features together and handles the reformatting of data in a single, separate class. 这样可以将所有类似的类/功能组合在一起,并在一个单独的类中处理数据的重新格式化。

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

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