Lets say I have some domain objects that will need to be serialized/packed using a custom binary format so they can be sent over sockets to external processes. My first instinct would be to create an interface that represents any object that can be packed into this binary format.
public interface IPackable
{
byte[] Pack();
}
Then my domain objects (such as Entity
, State
, Vector
, etc) would each implement this interface. This is similar to how the default serialization works in .NET and Java.
public class Vector : IPackable
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
// other operators and methods...
public byte[] Pack
{
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(X);
writer.Write(Y);
writer.Write(Z);
return stream.ToArray();
}
However, the more I learn about design principles (such as the SOLID's Single Responsibility Principle), the more I think that I should create a separate class for packing my domain objects.
I can see advantages and disadvantages with both. What do you all think? If the answer is create a separate class for packing the domain objects, should I create a separate packer for each domain object (such as EntityPacker
, StatePacker
, VectorPacker
, etc)?
Thanks