zoukankan      html  css  js  c++  java
  • 工厂模式中的泛型接口

    Java

    @@@code

    public interface DataBodyFormatter<T> {
        void Serialize(ByteBuf writer, T value, Version version);
        T Deserialize(ByteBuf reader,Version version);
    }

    public class DataBodyFormatterFactory {


    public DataBodyFormatterFactory() {
    }

    /**
    *
    系统内的formatters,自动注册
    */
    @Autowired
    Map<String, DataBodyFormatter> formatters;

    }

    public <D> DataBodyFormatter<D> getDataBodyFormatter(long code, boolean isReply) {

       

    }

    # 使用,默认就是object

    dataBodyFormatter = _DataBodyFactory.getDataBodyFormatter(code, isClientMode());

       

       

    @@#

       

    而在NET中

    @@@code

    public interface IDataBodyFormatterBase

    {

         object Deserialize(IByteBuffer reader, Version version);

    }

    public interface IDataBodyFormatter<T> : IDataBodyFormatterBase

    {

    void Serialize(IByteBuffer writer, T value, Version version);

    new T Deserialize(IByteBuffer reader, Version version);

    }

    # 使用一个基类

    public abstract class TDataBody<T> : DataBody, IDataBodyFormatter<T>

    {

    public abstract T Deserialize(IByteBuffer reader, Version version);

    public abstract void Serialize(IByteBuffer writer, T value, Version version);

       

    object IDataBodyFormatterBase.Deserialize(IByteBuffer reader, Version version)

    {

    return this.Deserialize(reader, version);

    }

    }

    public class DataBodyFormatterFactory : IDataBodyFormatterFactory

    {

       

    [Autowired()]

    public Dictionary<string, IDataBodyFormatterBase> FormatterDict { get; set; }

       

    //不允许的写法

    //public Dictionary<string, IDataBodyFormatter> FormatterDict2 { get; set; }

       

    public IDataBodyFormatterBase getDataBodyFormatter(int code, bool isReply = false)

    {

    }

    }

    # 使用

    dataBodyFormatter = _DataBodyFactory.getDataBodyFormatter(code, IsClientMode);

       

    @@#

  • 相关阅读:
    使用Let`s encrypt 免费的https 证书
    iOS使用Security.framework进行RSA 加密解密签名和验证签名
    java aes_cbc_256 加密解密
    iOS CommonCrypto 对称加密 AES ecb,cbc
    openssl lhash 数据结构哈希表
    openssl stack 数据结构栈基本操作
    openssl pem 生成公钥和私钥及文件
    openssl evp RSA 加密解密
    Mac OS键盘常用快捷键
    PHP JSON 数据编码和解码
  • 原文地址:https://www.cnblogs.com/QinQouShui/p/14310335.html
Copyright © 2011-2022 走看看