I understand that static methods are not correct for interfaces (see: Why Doesn't C# Allow Static Methods to Implement an Interface?) yet have come up against a situation where I have an object that implements all methods of an interface where all can be static, so I think I must be designing incorrectly.
Trouble is, I cant see any alternative
My interface IDataSerializer
is implemented by several classes. One that de/serializes XML, one that does JSON, etc. All of these classes implement the same functions and none have any "state data" (members, etc), but all eventually result in the same type of object being output.
For example, the XML class:
public class MyXmlSerializer : IDataSerializer
{
public string SerializeFoo(object foo)
{
// uses .Net XML serialzer to serialize foo
}
public object DeserializeFoo(string foo)
{
// uses .NET XML serializer to deserialize foo
}
// Object type returned by above methods is only ever
// used by following method which returns a type available
// to all IDataSerializer implementations as this is
// the data actually used by the rest of the program
public IList<Bar> CreateBarList(object deserializedFoo)
{
// does some magic to extract a list of Bar from the
// deserialized data, this is the main work for any
// IDataSerializer implementation
}
}
Obviously all of the methods shown above could be static (they all take in all the info they need as parameters and all return the result of their working, there are no members or fields)... but because they should be implemented in a serializer that can do work for any type of serial data (XML,JSON, YAML, etc) then they form an interface... Which is it? Am I thinking about this wrong? Is there an alternative, specific, pattern for achieving what I want to do?
Afterthought: maybe I should simply change my idea of de/serialization being work that something can do to thinking of each implementation as is a serlializer, thus suggesting replacing interface with abstract class?
After-afterthought: overridden methods can't be static either, so changing to abstract class doesn't help any.