英文:
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
你可能需要考虑是否应该将这两者绑定在一起,因为有一些内聚性和耦合性的准则需要考虑。例如,如果您需要在其他用途和广泛程度上使用writer
或outputStream
,则除非这种广泛使用在两者之间均得到体现,否则应将它们分开。也许更好的做法是将它们都隐藏在两个独立的数据访问对象后面,以便可以对它们的使用进行分区,根据访问目的以一种描述性的方式公开它们的操作 - 这更适合于您的业务。
无论是有限的使用还是广泛的使用,对于对这对输出的平等使用,我可能会应用一个外观模式,如下所示:
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
会在新实例中引发数据耦合 - id
是SimultaneousFeed
不应知道的数据。这使得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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论