java - Same Method for Client and Server slightly different behavior -


most of classes have different behavior if client or server side. (client: gui, server: connection stuff) have common behavior. want know what's best way this.

example content of class:

public class example{     private void commonmethod(){         //common code     }      public void clientmethod(){         commonmethod()         //client code     }      public void servermethod(){         commonmethod()         //server code     }  } 

what want:

  • 1 method way specify client or server
  • readable code

what allowed:

  • still have 3 private methods : server, common , client

what want avoid:

  • case (unless readable / short)
  • if

things thinking of:

  • enums (to specify client , server) , case (better use meaningless ints or booleans)
  • annotations (@clientside) (@serverside)

edit:

my classes loaded in api, example client/server method init. in main class need run method classes need initialization.

if (and if understand needs) use common interface implemented client , server class, abstract class in middle:

public interface example {     public void method(); }  public abstract class abstractexample implements example {      @override     public void method() {         common();         implmethod();     }      private void common() {         // common implementation     }      protected abstract void implmethod(); }  public class exampleclientimpl extends abstractexample {      @override     protected void implmethod() {         // client implementation     } }  public class exampleserverimpl extends abstractexample {      @override     protected void implmethod() {         // server implementation     } } 

with approach can split classes in common/client/server packages or better modules.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -