工廠模式中的泛型接口

Java this

@@@code

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

public class DataBodyFormatterFactory { code


public DataBodyFormatterFactory() {
}

/**
*
系統內的formatters,自動註冊
*/
@Autowired
Map<String, DataBodyFormatter> formatters;
orm

} blog

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

 

} string

# 使用,默認就是object it

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

 

 

@@#form

 

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

 

@@#

相關文章
相關標籤/搜索