欢迎光临
我的个人博客网站

工厂模式中的泛型接口

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);

 

@@#

赞(0) 打赏
未经允许不得转载:张拓的天空 » 工厂模式中的泛型接口
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏