Which pattern is suitable to send multiple type of responses through REST web service?

I have bunch of REST style web services which support XML and JSON type responses. Now I have to modify existing web services such that they provide Protobuf type of responses.

Developer who has developed these services has created three layers. Service layer, DAO layer and Processing layer.

Service layer calls one of the method of processing layer and processing layer calls DAO methods and returns either JSON or XML type response (POJO) to the service. Service layer then sends back this POJO to client.

For example, if service to add list of items is called then that code roughly looks as shown below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Path("/item")
class ShoppingItemService {
// annotations
public ItemAddResponse addList(/* parameters required for this method */) {
ItemProcessor processor = new ItemProcessor();
ItemAddResponse response = processor.process(/* pass some data to processing layer */);
return Response.ok().format("XML").build(response);
}
}
</code>
<code>@Path("/item") class ShoppingItemService { // annotations public ItemAddResponse addList(/* parameters required for this method */) { ItemProcessor processor = new ItemProcessor(); ItemAddResponse response = processor.process(/* pass some data to processing layer */); return Response.ok().format("XML").build(response); } } </code>
@Path("/item")
class ShoppingItemService {
    // annotations
    public ItemAddResponse addList(/* parameters required for this method */) {
        ItemProcessor processor = new ItemProcessor();
        ItemAddResponse response = processor.process(/* pass some data to processing layer */);
        return Response.ok().format("XML").build(response);
    }
}

Here ItemAddResponse is already annotated with JAXB type annotations for XML response and other annotations for JSON response.

I cannot reuse same ItemAddResponse because Protobuf generates its own classes using schema files.

Processor class has all the business logic and If I want to add support for Protobuf then I have create new method in processor and copy-paste almost all the business logic, but just return object of protobuf generated class.

This approach is time consuming and there atleast 50+ similar methods where I have to do copy-paste work. I want to avoid this and would like to design some elegant solution.

I would like to know how can I add support for Protobuf with minimal amount of changes?

I am reading Head First Design patterns book but this book is really huge and it will take lot of time to find proper design pattern.

Is there any design pattern which can be useful in this type of situations?

Thanks,

Chandrashekhar

If I want to add support for Protobuf then I have create new method in processor and copy-paste almost all the business logic

NO!

An object oriented approach

  • ItemProcessor “has all the business logic”. Don’t pollute the class, keep it strictly business logic
  • Your data/response structure is universal. JSON, XML, protobuf are just different syntactical representations of the same thing.
  • All your existing layers operate on the same data structure, all they need is to receive or send in their desired form (JSON, XML, protobuf)
  • You need something that transforms between the various formats so that the client, ItemProcessor, whatever, all get the format they expect.

OO Design Sketch

  • A ItemProcessor instance needs a Translator object to get the data to or from, or both, the desired form.

  • ItemProcessor maybe needs different Translators for interacting w/ the client and DAO respectively.

  • So we need to make a ItemProcessor/Translator composite at run time. creational design patterns help us do that.

  • It may be that you end up designing a single, static, Translator class.

  • Design patterns do not dictate your design. To start with a blank sheet of paper and say “I want to use the xyz design pattern” is wrong, wrong, wrong.

Fitting a pattern to your design

You still need to design things. Only you can define the various parts overall. From there, a creational pattern shows us how to decouple the creation of an object from its user. In this way we avoid the problem as quoted at the beginning of this answer.

Look at creation design patterns like factory, abstract factory, and builder.

Which design pattern you use is generally driven by the complexity of building the given object. That there are 3 kinds of factory has to do with complexity.

You will tell the factory, etc. to build you something with “JSON – protobuffer” transformation for example. So maybe you get back a ItemProcessor that has a JSONprotobufTranformer object, or XMLprotobufTransformer, or … whatever.

1

I wonder each input parameter is different or not.
Anyway you may be able to return result as Object and cast it. Also you may be able to get parameter as Object.

You may be looking for that how to generate methods in runtime.
https://stackoverflow.com/q/6680674

I recommend you’d better make all methods. I think It may be good for readability.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật