简体   繁体   中英

Class variable vs passing parameter in java - design issue

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?

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.

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.

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
   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM