带协议缓冲区的Java序列化 [英] Java Serialization with Protocol Buffer

查看:133
本文介绍了带协议缓冲区的Java序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java应用程序中使用protobuff来促进序列化,我对Google网站上的这句话有疑问

I want to use protobuff in a Java application to facilitate serialization and I have a question about this quote from the Google web site


协议缓冲区和OO设计
协议缓冲区类基本上是
哑数据持有者(比如
C ++中的结构);他们没有在对象模型中成为一流的
公民。如果
想要为
生成的类添加更丰富的行为,那么执行
的最佳方法是将生成的协议
缓冲类包装在
应用程序中 - 具体课程。如果你无法控制.proto文件的
设计,那么包装
协议缓冲区也是一个好主意
(如果你想重新使用
另一个
项目)。在这种情况下,您可以使用
包装类来创建更适合应用程序的唯一
环境的
接口:
隐藏一些数据和方法,暴露
便利函数等。你应该
永远不会通过继承它们来为生成的
类添加行为。这个
将破坏内部机制,并且
不是很好的面向对象的做法
无论如何。

Protocol Buffers and O-O Design Protocol buffer classes are basically dumb data holders (like structs in C++); they don't make good first class citizens in an object model. If you want to add richer behaviour to a generated class, the best way to do this is to wrap the generated protocol buffer class in an application-specific class. Wrapping protocol buffers is also a good idea if you don't have control over the design of the .proto file (if, say, you're reusing one from another project). In that case, you can use the wrapper class to craft an interface better suited to the unique environment of your application: hiding some data and methods, exposing convenience functions, etc. You should never add behaviour to the generated classes by inheriting from them. This will break internal mechanisms and is not good object-oriented practice anyway.

来自: http://code.google.com/apis/protocolbuffers/docs/javatutorial .html

它说要包装创建的类是什么意思?

What does it mean when it says to wrap the created class?

推荐答案

透视1

你编写.proto文件并将其提供给生成Builder代码的protoc。他们建议不要在生成的代码中添加任何方法。如果你想要将一些自定义行为添加到生成的代码中,那么写下你自己的CLASSPING生成的代码。

You write a .proto file and give it to protoc that generates the Builder code. They are suggesting not to add any methods to the generated code. If at all you want some custom behavior to be added to the generated code then WRITE YOUR OWN CLASS WRAPPING the generated code.

例如,让我们说protoc生成的类是MyMessageBuilder。并且您想要添加一个方法,该方法可以接受XML输入并将特定于protobuff的消息吐出。你可以编写一个XmlToMyMessageBuilder,如下所示。在这里XmlToMyMessageBuilder,你的类包装生成的代码并从XML()添加自定义行为。

For e.g let us say the protoc generated class is MyMessageBuilder. And you wanted to add a method that can take XML input and spitout the protobuff specific message out. You would write a XmlToMyMessageBuilder as below. Here XmlToMyMessageBuilder, your class is wrapping the generated code and adding custom behavior fromXml().

public class XmlToMyMessageBuilder
{
    private final MyMessageBuilder protoBuilder;

    public MyMessage fromXml(byte[] input()
    {
        protoBuilder.setXXX();
    }
}

这是一般的良好编程原则。

This is a general good programming principle.

观点2

通过提供中介,您还可以从底层序列化机制中解除代码。这允许您切换序列化器实现(假设您要序列化一个有效载荷,其中所有数据都是字符串格式...其中JSON序列化与压缩是一个更好的选择),影响很小。你可以做这样的事情

By providing a intermediary you can also DECOUPLE your code from the underlying serialization mechanism. This allows you to switch the serializer implementations (say you want to serialize a payload where all the data is in string format...where JSON seriazation with compression is a better alternative) with low impact. You could do something like this

public interface MySerializer
{
    boolean serialize(MyDomainObject input);
}

public PBBasedSerializer implements MySerializer
{
    private final MyMessageBuilder protoBuilder;
    ...
}

public JsonBasedSerializer implements MySerializer
{
    private final JSONSerializer jsonSerializer;
    ...
}

这篇关于带协议缓冲区的Java序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆