我有哪些解决以下并行继承层次结构问题的选项?我想让Serializer类保持无状态。
public abstract class BaseSerializerOptions
{
public Boolean SortComponents { get; set; }
}
public class TextSerializerOptions : BaseSerializerOptions
{
public Int32 TrimStrings { get; set; }
}
public class BinarySerializerOptions : BaseSerializerOptions
{
public Boolean SkipNulls { get; set; }
}
public abstract class BaseSerializer
{
public abstract void Serialize(Object obj, Stream s, BaseSerializerOptions opt);
}
public class TextSerializer : BaseSerializer
{
public override void Serialize(Object obj, Stream s, TextSerializerOptions opt)
{
}
}
public class BinarySerializer : BaseSerializer
{
public override void Serialize(Object obj, Stream s, BinarySerializerOptions opt)
{
}
}
我希望能够编写如下方法:
static void Test(BaseSerializer s, BaseSerializerOptions o)
{
var obj = new MyObject { ... };
using (var f = File.OpenWrite("..."))
{
s.Serialize(obj, f, o);
}
// do more things here to check the serializer result;
// the serializers might return some kind of operation status, which I want to check
}
static void TestSerializers()
{
Test(new TextSerializer(), new TextSerializerOptions {...});
Test(new BinarySerializer(), new BinarySerializerOptions {...});
}
答案 0 :(得分:0)
扩大评论中的第一个建议:
public abstract class BaseSerializerOptions
{
public Boolean SortComponents { get; set; }
}
public class TextSerializerOptions : BaseSerializerOptions
{
public Int32 TrimStrings { get; set; }
}
public class BinarySerializerOptions : BaseSerializerOptions
{
public Boolean SkipNulls { get; set; }
}
public abstract class BaseSerializer<T> where T : BaseSerializerOptions
{
public abstract void Serialize(Object obj, Stream s, T opt);
}
public class TextSerializer : BaseSerializer<TextSerializerOptions>
{
public override void Serialize(Object obj, Stream s, TextSerializerOptions opt)
{
}
}
public class BinarySerializer : BaseSerializer<BinarySerializerOptions>
{
public override void Serialize(Object obj, Stream s, BinarySerializerOptions opt)
{
}
}
更新特定语法用法:
public class TextSerializer : BaseSerializer
{
public void Serialize(Object obj, Stream s, TextSerializerOptions opt)
{
this.Serialize(obj, s, opt);
}
public override void Serialize(object obj, Stream s, BaseSerializerOptions opt)
{
if (opt is TextSerializerOptions options)
{
}
else
{
}
}
}
public class BinarySerializer : BaseSerializer
{
public void Serialize(Object obj, Stream s, BinarySerializerOptions opt)
{
this.Serialize(obj, s, opt);
}
public override void Serialize(object obj, Stream s, BaseSerializerOptions opt)
{
if (opt is BinarySerializerOptions options)
{
}
else
{
}
}
}
答案 1 :(得分:0)
几年前,我有机会使用Haskell学习了一些有关函数式编程的知识,而大多数FP概念对我来说似乎都很直观。由于我对问题的建议不满意,所以我有一个很好的主意要问自己:我将如何在Haskell中做到这一点?因此,下面是我从FP角度进行思考而得出的解决方案。基本上,我放弃一种继承层次结构(带有序列化程序类的那些),而改用静态方法。函数式编程使我成为一个更好的OOP程序员...真实的故事!
public abstract class BaseSerializerOptions
{
Boolean SortComponents { get; set; }
}
public class TextSerializerOptions : BaseSerializerOptions
{
public Boolean TrimStrings { get; set; }
}
public class BinarySerializerOptions : BaseSerializerOptions
{
public Boolean SkipNulls { get; set; }
}
public class SerializerResult
{
}
public static class Serializer
{
public static SerializerResult Write(Object obj, Stream s, BaseSerializerOptions opt)
{
switch (opt)
{
case TextSerializerOptions o: return WriteText(obj, s, o);
case BinarySerializerOptions o: return WriteBinary(obj, s, o);
}
throw new Exception();
}
public static SerializerResult WriteText(Object obj, Stream s, TextSerializerOptions opt)
{
return null;
}
public static SerializerResult WriteBinary(Object obj, Stream s, BinarySerializerOptions opt)
{
return null;
}
}
class Program
{
static void Test(BaseSerializerOptions o)
{
var obj = new Object();
using (var fs = File.Open("...", FileMode.Create))
{
var r1 = Serializer.Write(obj, fs, o);
CheckSerializerResults(r1);
}
}
static void TestSerializers()
{
Test(new TextSerializerOptions { TrimStrings = true });
Test(new BinarySerializerOptions { SkipNulls = false });
}
static void CheckSerializerResults(SerializerResult r)
{
}
}