funky-eyes commented on code in PR #6208:
URL: https://github.com/apache/incubator-seata/pull/6208#discussion_r1436778416


##########
serializer/seata-serializer-seata/src/main/java/io/seata/serializer/seata/SeataSerializer.java:
##########
@@ -20,38 +20,58 @@
 
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
+import io.seata.common.exception.NotSupportYetException;
 import io.seata.common.loader.LoadLevel;
+import io.seata.common.loader.Scope;
 import io.seata.common.util.BufferUtils;
 import io.seata.core.protocol.AbstractMessage;
+import io.seata.core.protocol.ProtocolConstants;
 import io.seata.core.serializer.Serializer;
+import io.seata.serializer.seata.protocol.v1.MessageCodecFactoryV1;
 
 /**
  * The Seata codec.
  *
  */
-@LoadLevel(name = "SEATA")
+@LoadLevel(name = "SEATA", scope = Scope.PROTOTYPE)
 public class SeataSerializer implements Serializer {
 
+    MessageCodecFactory factory;
+    byte protocolVersion ;
+
+    public SeataSerializer(Byte version){
+        if (version == ProtocolConstants.VERSION_1) {
+            factory =  new MessageCodecFactoryV1();
+        }else {
+            throw new NotSupportYetException("not support version" + version);
+        }
+        protocolVersion = version;
+    }
     @Override
     public <T> byte[] serialize(T t) {
         if (!(t instanceof AbstractMessage)) {
             throw new IllegalArgumentException("AbstractMessage isn't 
available.");
         }
         AbstractMessage abstractMessage = (AbstractMessage)t;
-        //typecode
+        //type code
         short typecode = abstractMessage.getTypeCode();
         //msg codec
-        MessageSeataCodec messageCodec = 
MessageCodecFactory.getMessageCodec(typecode);
+        MessageSeataCodec messageCodec = factory.getMessageCodec(typecode);
         //get empty ByteBuffer
         ByteBuf out = Unpooled.buffer(1024);
         //msg encode
         messageCodec.encode(t, out);
         byte[] body = new byte[out.readableBytes()];
         out.readBytes(body);
 
-        //typecode + body
-        ByteBuffer byteBuffer = ByteBuffer.allocate(2 + body.length);
-        byteBuffer.putShort(typecode);
+        ByteBuffer byteBuffer;
+        if (protocolVersion == ProtocolConstants.VERSION_0) {

Review Comment:
   为什么不通过策略模式来实现不同版本的逻辑处理?
   Why not implement different versions of logic processing through the policy 
pattern?



##########
core/src/main/java/io/seata/core/serializer/SerializerServiceLoader.java:
##########
@@ -48,6 +51,24 @@ public static Serializer load(SerializerType type) throws 
EnhancedServiceNotFoun
                         "Please manually reference 
'io.seata:seata-serializer-protobuf' dependency ", e);
             }
         }
-        return EnhancedServiceLoader.load(Serializer.class, type.name());
+
+        String key = serialzerKey(type, version);
+        Serializer serializer = serializerMap.get(key);
+        if (serializer == null) {
+            if (type == SerializerType.SEATA) {
+                serializer = EnhancedServiceLoader.load(Serializer.class, 
type.name(), new Object[]{version});

Review Comment:
   为何不是通过version加载到对应的实现类,而是共用一个实现类只是在内部对不同版本做不同的处理
   Why not load it into the corresponding implementation class through version, 
but share an implementation class that only handles different versions 
internally?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to