title: Design Pattern to model Request and Response Objects for Webservices
body: <p>I have about 7 REST web services to implement. Some of these web services have a standard (identical) response, while some have different responses.</p>
  
  <p>The requests for these web services are different but some requests and some of the responses have the same underlying data objects. </p>
  
  <p>I am not sure if I have to build separate request/response classes for each web service or reuse a standard one. I would like to know if there is a design pattern to model the request objects and response objects for these web services.</p>
  
  <p>Ok, say Account and Book are two rest resources my web services will be working on.</p>
  
  <pre><code>class Account {
      String username;
      String id;
  }
  
  
  class Book {
      String title;
      String isbn;
  }
  </code></pre>
  
  <p>So my web services look like this:</p>
  
  <pre><code>MYAPI/CreateAccountandBook
  MYAPI/Account/Create
  MYAPI/Book/Create
  MYAPI/Book/Update/{isbn}
  MYAPI/Account/Update/{id}
  MYAPI/Account/getInfo/{id} 
  </code></pre>
  
  <p>and so on.</p>
  
  <p>Now <code>CreateAccountandBook</code> request will take an account object and a list of books in the payload.
  Also the response object for <code>MYAPI/Account/getInfo/{id}</code> has an account object and a list of books associated with that account. But the response object also includes a <code>statusCode</code> and <code>Description</code>.</p>
  
  <p>Now I would like to create classes for these request and response objects
  in the best possible way. </p>
  
  <p>Okay for a start.</p>
  
  <p>I have two abstract classes <code>StandardRequest</code> and <code>StandardResponse</code>.</p>
  
  <p>All requests classes will extend the Standard Request class and customize accordingly. 
  All response classes will extend the Standard response class and customize accordingly.</p>
  
  <p>But these requests and response can be way different from each other but still re-use the same entity objects.</p>
  
  <p>For instance:</p>
  
  <p><code>createAccountandBook</code> request object looks like this:  </p>
  
  <pre><code>class CreateAccountAndBookRequest {
     Account account;
     List&lt;Book&gt; books;
  }
  </code></pre>
  
  <p>while the response for the <code>getInfo</code> web service  is:  </p>
  
  <pre><code>class GetInfoResponse {
     Account account;
     List&lt;Book&gt; books;
     String statusCode;
     String description;
  }
  </code></pre>
  
  <p>so there is overlap across request and response classes. I can create two (req/res) classes for each web service. But would like to know if there is a better way to model these classes.</p>