samedi 29 octobre 2016

delegate or proxy or others

I want to handle different result by its type. And here is my codes public interface Handler { void handle(Result result) }

public class HandlerA implement Handler {
   public handle(Result result) {
      //do something
   }
}
public class HandlerB implement Handler {
   public handle(Result result) {
      //do something
   }
}
//....
public class HandlerImpl implement Handler {
  //..
    Handler A = new HandlerA();
    Handler B = new HandlerB();
  //..
  public void handle(Result result) {
    switch (result.getType()){
      case A:
       A.handle(result);
       break;
      case B:
       B.handle(result);
       break;
       //....
    }
  }
}

the service

public class Service{
   Handler handler = new HandlerImpl();
   public process() {
     //..
     Result result = .....
     handler.handle(result);
     //...
   }
}

But I feel wired about HandlerImpl for I think maybe it should not implement Handle. I have read some blogs about Proxy/Delefate design pattern, I didn't get some better implements. Anyone has some better suggestion about how to implment this funciton. Many thanks

Aucun commentaire:

Enregistrer un commentaire