dimanche 14 juillet 2019

Design pattern many functions to one function to many functions implementation

Following is the flow of code:

Client Facing class:

class Main{
   void set(String key, String value, ...){
      wrapper.set(key, value, ..);
   }

   String get(String key, ...){
       return wrapper.get(key, ...);
   }

}
/**
 Wrapper/Facade layer which forwards the call to dao layer
 Currently Wrapper layer code looks ugly
 */
class Wrapper{
   void set(String key, String value, ...){
      executePreExecutors()
      backend.set(key, value, ..);
      executePostExecutors()
   }

   String get(String key, ...){
       executePreExecutors()
       backend.get(key, ...);
       executePreExecutors()
   }
}
 //db sdk connection classes
class backend{
    void set(String key, String value, ...){
      db.set(key, value, ..);
   }

   String get(String key, ...){
       return db.get(key, ...);
   }
}

Above code works fine but Wrapper does a lot of repetitive call to pre/post executors in all methods in wrapper. I want something like below but facing problem as different methods have different return types:

class Wrapper{
   <Return Type> wrap(input data){
      executePreExecutors();
      backend.method(input data);
      executePostExecutors();
   }
}

Problem I am facing is how do I generalise the above code to avoid ugly looking Wrapper layer

Aucun commentaire:

Enregistrer un commentaire