一个 JsonGenerator 能否在同一次调用中向 OutputStream 和 Writer 中写入?

huangapple go评论64阅读模式
英文:

Can a JsonGenerator write to an OutputStream and Writer in the same call?

问题

有没有一种优雅的解决方案或模式,可以将writeStartObject()的调用同时应用于Writer和OutputStream?

try (
    JsonGenerator output = jfactory.createGenerator(outputWriter); // writer
    JsonGenerator cachingOutput = jfactory.createGenerator(cachingService.getCachingOutputStream(id));//outputstream
) {
    output.writeStartObject();
    cachingOutput.writeStartObject();
    ...
英文:

Is there an elegant solution or pattern so that one invoke of writeStartObject() can be applied to both the Writer and the OutputStream?

        try (
        JsonGenerator output = jfactory.createGenerator(outputWriter); // writer
        JsonGenerator cachingOutput = jfactory.createGenerator(cachingService.getCachingOutputStream(id));//outputstream
    ) {
        output.writeStartObject();
        cachingOutput.writeStartObject();
        ...

答案1

得分: 1

你可能需要考虑是否应该将这两者绑定在一起,因为有一些内聚性和耦合性的准则需要考虑。例如,如果您需要在其他用途和广泛程度上使用writeroutputStream,则除非这种广泛使用在两者之间均得到体现,否则应将它们分开。也许更好的做法是将它们都隐藏在两个独立的数据访问对象后面,以便可以对它们的使用进行分区,根据访问目的以一种描述性的方式公开它们的操作 - 这更适合于您的业务。

无论是有限的使用还是广泛的使用,对于对这对输出的平等使用,我可能会应用一个外观模式,如下所示:

class SimultaneousFeed {
    private JFactory jFactory;
    private JsonGenerator out;
    private JsonGenerator writer;
    SimultaneousFeed(JFactory jFactory, OutputStream outputStream, Writer outputWriter) {
        this.jFactory = jFactory;
        this.out = jfactory.createGenerator(outputStream);
        this.writer = jfactory.createGenerator(outputWriter);
    }
    void writeStartObject() {
        out.writeStartObject();
        writer.writeStartObject();
    }
    ...
    // 对这对输出共同的其他操作
}
...
SimultaneousFeed feed = new SimultaneousFeed(jFactory, cachingService.getCachingOutputStream(id),
        outputWriter);
feed.writeStartObject();

请注意,我将OutputStream传递给了新的SimultaneousFeed实例,而不是传递id。传递id会在新实例中引发数据耦合 - idSimultaneousFeed不应知道的数据。这使得SimultaneousFeed只需关注输出数据,提高了可重用性和可维护性。

另一种要考虑的替代模式是装饰者模式,前提是您能够对jfactory实例和JsonGenerator类进行子类化。这将允许您创建一个自定义的JsonGenerator,在writeStartObject()方法的覆盖实现中同时向这两个输出进行写入。在这种情况下,您可以在jfactory类中提供一个方法:

public JsonGenerator createSimultaneousGenerator(OutputStream outputStream, Writer outputWriter) {
    return new SimulJsonGenerator(outputStream, outputWriter);
}
英文:

You may want to consider whether these two really should be bound together as there are cohesion and coupling guidelines to consider. For example, if you need to use the writer or the outputStream for other uses and to an extensive degree then you should keep them separate unless this extensive use is reflected across both of them equally. It may be better to hide both of these behind two independent Data Access Objects where their use can be compartmentalized, exposing their operations in a descriptive manner based on the purpose for accessing them - a little more suited to your business.

Whether it's limited use or extensive use, for equal use across the pair I might apply a façade as such:

class SimultaneousFeed {
    private JFactory jFactory;
    private JsonGenerator out;
    private JsonGenerator writer;
    SimultaneousFeed(JFactory jFactory, OutputStream outputStream, Writer outputWriter) {
        this.jFactory = jFactory;
        this.out = jfactory.createGenerator(outputStream);
        this.writer = jfactory.createGenerator(outputWriter);
    }
    void writeStartObject() {
        out.writeStartObject();
        writer.writeStartObject();
    }
    ...
    // other operations common to the pair of these outputs
}

...

SimultaneousFeed feed = new SimultaneousFeed(jFactory, cachingService.getCachingOutputStream(id),
        outputWriter);
feed.writeStartObject();

Note that I'm passing the OutputStream to the new SimultaneousFeed instance rather than passing the id. Passing the id raises the data coupling in the new instance - the id is a piece of data the SimultaneousFeed should know nothing about. This allows the SimultaneousFeed to concern itself with only output data, improving reusability and maintainability.

An alternative pattern to consider would be the Decorator pattern, provided you're able to subclass the class of the jfactory instance and the JsonGenerator class. This would allow you to create a custom JsonGenerator that simultaneously writes to the two outputs in an overridden implementation of the writeStartObject() method. In that case, you could provide a method in the jfactory class:

public JsonGenerator createSimultaneousGenerator(OutputStream outputStream, Writer outputWriter) {
    return new SimulJsonGenerator(outputStream, outputWriter);
}

huangapple
  • 本文由 发表于 2020年9月25日 01:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64051122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定