Sharing server and client code

I'm a little stuck here. I know how I can do this when separating the server and client code completely, but I don't want to do that if it's possible to share code to save myself some work and to follow the principle "don't repeat yourself". I start to feel anxious when I don't follow this principle. However I'm not sure how to share the code and what to share. The client and server have something in common, but they also do some things completely differently. The client requests and receives, while the server does most of the work.

Someone suggested to me to think MVC. The client is the View layer and the server is the Controller and Model layer. This way they do entirely different things, therefore I wouldn't need to worry about sharing code and at the same time I wouldn't be repeating myself. Sounds good. But...

What about representing data? Wouldn't that need to be somewhat the same? They're talking about the same thing. Doesn't it make sense to think that there is some code which can be shared?

In some cases I would have to send at least N (= amount of players which might will be between 10 and 20) as much data. Because each player must vote each round and each vote must be able to be viewed in a 'history context', step by step, how much votes each player had at a specific point in time. This is because this game is about analyzing the votes and basing your votes on the analysis. Both the client and server have to sort on the votes, so it would make sense to let them share the sort method.

Any ideas? Perhaps I'm thinking the wrong way. And sorry if I'm being unclear about my problem. I have some difficulty expressing the problem, maybe because I don't completely understand the problem.

Comments

  • 1. if they are really common, write a library function
    For example :
    class Util
    {
      public static ErrorCode validateDate(String date) ...
      public static ErrorCode validBuySellPrice(BigDecimal buy, BigDecimal sell)...
    }
    

    2. if they are not really common, you should code two version.

    3. if there are only a few differences, you may use subclassing
      class MessageSender
      {
        // hundred of lines of code in common here ...
      }
    
      class ServerMessageSender extends MessageSender
      {
        // server specific code, override some methods if necessary
      }
    
      class ClientMessageSender extends MessageSender
      {
        // client specific code, override some methods if necessary
      }
    
    
Sign In or Register to comment.