Skip to main content

To follow Open Close Principle and also Single responsibility Principle I suggest you such divisions:

public interface ContentSerializer{
    String serialize(Content str);
    String deserialize(Content str);
}

public interface RequestSerializer extends ContentSerializer{}
public interface Response SerializerResponseSerializer extends ContentSerializer{}

and then have special implementation for each combination you have:

JsonCreateRequestSerializer, JsonCreateResponseSerializer ...,
XmlUpdateSerializer, ... XmlUpdateSerializer

It will rapidly increase number of classes in your project, but will bring you freedom how to expand this structure, and also will give your code right order.

At the end of the day your code will still contain that big switch-case in some form at least when constructing proper instance of ContentSerializer but you can still hide it in factory:

public class RequestSerializerFactory{
    public static ContentSerializer createCreateRequestSerializer(ContentType type){}
    public static ContentSerializer createUpdateRequestSerializer(ContentType type){}
    public static ContentSerializer createRetrieveRequestSerializer(ContentType type){}
    public static ContentSerializer createDeleteRequestSerializer(ContentType type){}

} //and same for Response

To follow Open Close Principle and also Single responsibility Principle I suggest you such divisions:

public interface ContentSerializer{
    String serialize(Content str);
    String deserialize(Content str);
}

public interface RequestSerializer extends ContentSerializer{}
public interface Response Serializer extends ContentSerializer{}

and then have special implementation for each combination you have:

JsonCreateRequestSerializer, JsonCreateResponseSerializer ...
... XmlUpdateSerializer

It will rapidly increase number of classes in your project but will bring you freedom how to expand this structure and also will give your code right order.

At the end of the day your code will still contain that big switch-case in some form at least when constructing proper instance of ContentSerializer but you can still hide it in factory:

public class RequestSerializerFactory{
    public static ContentSerializer createCreateRequestSerializer(ContentType type){}
    public static ContentSerializer createUpdateRequestSerializer(ContentType type){}
    public static ContentSerializer createRetrieveRequestSerializer(ContentType type){}
    public static ContentSerializer createDeleteRequestSerializer(ContentType type){}

} //and same for Response

To follow Open Close Principle and also Single responsibility Principle I suggest you such divisions:

public interface ContentSerializer{
    String serialize(Content str);
    String deserialize(Content str);
}

public interface RequestSerializer extends ContentSerializer{}
public interface ResponseSerializer extends ContentSerializer{}

and then have special implementation for each combination you have:

JsonCreateRequestSerializer, JsonCreateResponseSerializer,
XmlUpdateSerializer, ...

It will rapidly increase number of classes in your project, but will bring you freedom how to expand this structure, and also will give your code right order.

At the end of the day your code will still contain that big switch-case in some form at least when constructing proper instance of ContentSerializer but you can still hide it in factory:

public class RequestSerializerFactory{
    public static ContentSerializer createCreateRequestSerializer(ContentType type){}
    public static ContentSerializer createUpdateRequestSerializer(ContentType type){}
    public static ContentSerializer createRetrieveRequestSerializer(ContentType type){}
    public static ContentSerializer createDeleteRequestSerializer(ContentType type){}

} //and same for Response
Source Link

To follow Open Close Principle and also Single responsibility Principle I suggest you such divisions:

public interface ContentSerializer{
    String serialize(Content str);
    String deserialize(Content str);
}

public interface RequestSerializer extends ContentSerializer{}
public interface Response Serializer extends ContentSerializer{}

and then have special implementation for each combination you have:

JsonCreateRequestSerializer, JsonCreateResponseSerializer ...
... XmlUpdateSerializer

It will rapidly increase number of classes in your project but will bring you freedom how to expand this structure and also will give your code right order.

At the end of the day your code will still contain that big switch-case in some form at least when constructing proper instance of ContentSerializer but you can still hide it in factory:

public class RequestSerializerFactory{
    public static ContentSerializer createCreateRequestSerializer(ContentType type){}
    public static ContentSerializer createUpdateRequestSerializer(ContentType type){}
    public static ContentSerializer createRetrieveRequestSerializer(ContentType type){}
    public static ContentSerializer createDeleteRequestSerializer(ContentType type){}

} //and same for Response