我知道我的问题并不是完全被问到但是它很难,所以我将展示一个例子: 所以我有一个AbstractHandler类。我用另一个类的方法创建它:
class AbstractManager
{
BinarySerializator binarySerializator; //just for the example
TextSerializator textSerializator;
public AbstractManager(BinarySerializator binarySerializator) {
this.binarySerializator = binarySerializator;
this.textSerializator = null;
}
public AbstractManager(TextSerializator textSerializator) {
this.textSerializator = textSerializator;
this.binarySerializator = null;
}
public AbstractHandler createHandler() {
if (this.binarySerializator != null) {
return new AbstractHandler<byte[]>(this.binarySerializator);
} else {
return new AbstractHandler<String>(this.textSerializator);
}
}
}
所以这就是我创建AbstractHandler的方法,它又有两个构造函数(一个带有BinarySerializator,另一个带有TextSerializator)。现在这里是有趣的部分:两个Serializators都有一个方法,它是getSerializedType(Object对象),二进制返回byte [],文本返回String。 我希望AbstractHandler中的方法返回byte []或String,具体取决于我所拥有的序列号的类型:
public T createSerializable(Object object) {
if (binarySerializer != null) {
return (T) binarySerializer.getSerializedType(object);
}
if (textSerializer != null) {
return (T) textSerializer.getSerializedType(object);
}
}
我的问题是 - 这是这样做的方法吗?还有两个构造函数的解决方法以及我应该如何处理两个不同的AbstractHandler,我知道可以使用泛型而不是两个类(一个用于String,一个用于byte [])。感谢您花时间阅读这篇长篇文章。
答案 0 :(得分:1)
我有两个解决方案:
interface ISerializatorStategy
{
object GetSerializedType(object obj);
}
// If you want generic classes
interface ISerializatorStategy<T> : ISerializatorStategy
{
new T GetSerializedType(object obj);
}
// If you don't want generic classes
// you can inherit from the base interface and implement only its method
class TextSerializator : ISerializatorStategy<string>
{
public string GetSerializedType(object obj)
{
return "Hello World";
}
object ISerializatorStategy.GetSerializedType(object obj)
{
return GetSerializedType(obj);
}
}
class IntSerializator : ISerializatorStategy<int>
{
public int GetSerializedType(object obj)
{
return 42;
}
object ISerializatorStategy.GetSerializedType(object obj)
{
return GetSerializedType(obj);
}
}
class AbstractManager
{
protected ISerializatorStategy SerializatorStrategy { get; set; }
public AbstractManager(ISerializatorStategy serializatorStrategy)
{
SerializatorStrategy = serializatorStrategy;
}
public AbstractHandler CreateHandler()
{
return new AbstractHandler(SerializatorStrategy);
}
}
// If you want generic classes
class AbstractManager<T> : AbstractManager
{
// Note, if you allow this constructor,
// you could encounter runtime errors
public AbstractManager(ISerializatorStategy serializatorStrategy)
: base(serializatorStrategy)
{
}
public AbstractManager(ISerializatorStategy<T> serializatorStrategy)
: base(serializatorStrategy)
{
}
public new AbstractHandler<T> CreateHandler()
{
return new AbstractHandler<T>(SerializatorStrategy);
}
}
class AbstractHandler
{
private readonly ISerializatorStategy _serializatorStrategy;
public AbstractHandler(ISerializatorStategy serializatorStrategy)
{
_serializatorStrategy = serializatorStrategy;
}
public object CreateSerializable(object obj)
{
return _serializatorStrategy.GetSerializedType(obj);
}
public void GetSerializable<T>(object obj, out T serializebleObject)
{
serializebleObject = (T)_serializatorStrategy.GetSerializedType(obj);
}
}
// If you want generic classes
class AbstractHandler<T> : AbstractHandler
{
// Note, if you allow this constructor,
// you could encounter runtime errors
public AbstractHandler(ISerializatorStategy serializatorStrategy)
: base(serializatorStrategy)
{
}
public AbstractHandler(ISerializatorStategy<T> serializatorStrategy)
: base(serializatorStrategy)
{
}
public new T CreateSerializable(object obj)
{
return (T)base.CreateSerializable(obj);
}
}
class Program
{
static void Main(string[] args)
{
var textManager = new AbstractManager<string>(new TextSerializator());
var textHandler = textManager.CreateHandler();
string textResult = textHandler.CreateSerializable(null); // or AbstractHandler<string> as type
Console.WriteLine(textResult);
var intManager = new AbstractManager<int>(new IntSerializator());
var intHandler = intManager.CreateHandler(); // or AbstractHandler<int> as type
int intResult = intHandler.CreateSerializable(null);
Console.WriteLine(intResult);
Console.ReadKey();
}
}
class Program
{
static void Main(string[] args)
{
AbstractManager manager = new AbstractManager(new TextSerializator());
AbstractHandler handler = manager.createHandler();
string textResult;
handler.GetSerializable(null, out textResult);
Console.WriteLine(textResult);
manager = new AbstractManager(new IntSerializator());
handler = manager.createHandler();
int intResult;
handler.GetSerializable(null, out intResult);
Console.WriteLine(intResult);
Console.ReadKey();
}
}
现在,你不应该在你的问题上应用这个麻烦。