VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Objective-C编程 >
  • 工厂模式中的泛型接口

Java

 

复制代码
@@@code

 

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

public class DataBodyFormatterFactory {


public DataBodyFormatterFactory() {
}

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

}

public <DDataBodyFormatter<DgetDataBodyFormatter(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);

 

@@#

文章出处:https://www.cnblogs.com/QinQouShui/p/14310335.html

相关教程