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
Post a Comment