3

Say I have 2 classes in an SOA model application..

  1. Service class - which takes request and returns response
  2. For further processing (say, business logic/parsing/dao etc), it passes the request to a SvcBusiness class.

Question is, should SvcBusiness class use the request as its class variable or should it just use the request in one of it's business methods? It is possible that request needs to be passed to other lower layers like DAO layer. Should those classes also use request as a class variable or should the request be just part of a method?

ServiceImpl class:

public class ServiceImpl {

   public Response getDataForType1Request(Request type1) {
      SvcBusiness buzclazz = new SvcBusiness();
      return buzclazz.doOperationForType1(type1);
   }

   public Response getDataForType2Request(Request type2) {
      SvcBusiness buzclazz = new SvcBusiness();
      return buzclazz.doOperationForType2(type2);
   }

}

Option 1: when request is passed as a parameter.

public class SvcBusiness {

   public Response doOperationForType1(Request type1) {
      // do business and return response1
   }

   public Response doOperationForType2(Request type2) {
      // do business and return response2
   }

}

Option 2: request is set as a class variable. In this scenario.. ServiceImpl will pass the request to SvcBusiness constructor when the object is created.. and will simply call execute() method.

public class SvcBusiness {

   private Request request;

   public SvcBusiness(Request request) {
      this.request = request;
   }

   private Response doOperationForType1() {
      // do business and return response1
   }

   private Response doOperationForType2() {
      // do business and return response2
   }

   public Response execute() {
      // if type1 request call doOperationForType1()
      // if type2 request call doOperationForType1()
   }

}

Please help! What are the advantages and disadvantages of both? Is there a design pattern to address this scenario?

3 Answers 3

2

Don't use the Request (and Response) further down in your class hierarchy! The service (and everything called by the service) may be called from somewhere else, where there is no such thing as a Request. And then you will have a problem with filling that parameter. Use an own data model in the service, and extract and convert everything you need for that from the Request.

Sign up to request clarification or add additional context in comments.

Comments

0

Fully agree with Uwe's answer. However, if you still want to use Request class, it'll be less harmful as a parameter (The way Servlets work). Otherwise, you'd have to deal with synchronization on a highly probable multithreaded environment.

Comments

0

When I face a problem like this I always wonder if I really need an object. Usually I use the option 1 but creating all methods as static. As those methods don't rely in the current object state (there are no instance attributes), I save some memory just not creating such objects (other option is just implement the Singleton pattern).

public class SvcBusiness {

   public static Response doOperationForType1(Request type1) {
      // do business and return response1
   }

   public Response doOperationForType2(Request type2) {
      // do business and return response2
   }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.