简体   繁体   English

带协议缓冲区的Java序列化

[英]Java Serialization with Protocol Buffer

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 我想在Java应用程序中使用protobuff来促进序列化,我对Google网站上的这句话有疑问

Protocol Buffers and OO Design Protocol buffer classes are basically dumb data holders (like structs in C++); 协议缓冲区和OO设计协议缓冲区类基本上是哑数据持有者(如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). 如果您无法控制.proto文件的设计(例如,如果您正在重用另一个项目中的一个),那么包装协议缓冲区也是一个好主意。 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. 这将破坏内部机制,无论如何都不是良好的面向对象实践。

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

What does it mean when it says to wrap the created class? 它说要包装创建的类是什么意思?

Perspective 1 观点1

You write a .proto file and give it to protoc that generates the Builder code. 您编写.proto文件并将其提供给生成Builder代码的protoc。 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. 如果你想要将一些自定义行为添加到生成的代码中,那么写下你自己的CLASSPING生成的代码。

For eg let us say the protoc generated class is MyMessageBuilder. 例如,让我们说protoc生成的类是MyMessageBuilder。 And you wanted to add a method that can take XML input and spitout the protobuff specific message out. 并且您想要添加一个方法,该方法可以将XML输入和spitout特定于protobuff的消息输出。 You would write a XmlToMyMessageBuilder as below. 你可以编写一个XmlToMyMessageBuilder,如下所示。 Here XmlToMyMessageBuilder, your class is wrapping the generated code and adding custom behavior fromXml(). 在这里使用XmlToMyMessageBuilder,您的类将包装生成的代码并从Xml()添加自定义行为。

public class XmlToMyMessageBuilder
{
    private final MyMessageBuilder protoBuilder;

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

This is a general good programming principle. 这是一个很好的编程原则。

Perspective 2 观点2

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. 这允许您切换序列化程序实现(假设您要序列化有效负载,其中所有数据都是字符串格式...其中JSON序列化与压缩是一个更好的选择),影响很小。 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;
    ...
}

It means that you would implement your own class that contains a protocol buffer object as a private field. 这意味着您将实现自己的包含协议缓冲区对象的类作为私有字段。

Protocol buffer classes are generated from .proto files. 协议缓冲区类是从.proto文件生成的。 These generated classes have all methods to directly manipulate the fields they contain. 这些生成的类具有直接操作它们包含的字段的所有方法。 But they don't have methods that serve higher level operations than just modifying a field. 但是他们没有比仅修改字段更高级别操作的方法。

Your wrapper class can then provide a richer or more restricted interface to users of your API. 然后,您的包装器类可以为API用户提供更丰富或更受限制的界面。 As any modification of the protocol buffer needs to go through the wrapping object, you have full control about what operations you want to support. 由于协议缓冲区的任何修改都需要通过包装对象,因此您可以完全控制要支持的操作。

What does it mean when it says to wrap the created class? 它说要包装创建的类是什么意思?

They're handing you a class, wrap it with a child class purpose built for what you're doing. 他们正在给你上课,把它包装成为你正在做的事情而建的儿童课。 Don't interact with a raw class instance from the library. 不要与库中的原始类实例进行交互。

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

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