3 Replies Latest reply on Mar 10, 2010 5:25 PM by 800387

    Question about class design


      I am trying to design a web game using JSP, servlets, etc. I have some ideas about the classes I have to create and how to split the work among them. I.e:

      class Player
      - contains the player data like name, nickname, age, etc...
      - have a static method getPlayerById(id) which calls load(id) method.
      - load method calls DBPlayer.load(id)

      class DBPlayer
      - uses Database objetct to get the connetion.
      - static load(id) method executes a "select" query and gets the data from database and create a new Player instance sending the data by constructor

      class Database
      - get the connetion data and connect to database

      I could have a JSP page named showPlayerInfo.jsp which gets the player_id by request. The page calls Player.getPlayerById(player_id) ans shows the data in html format.
      would this be correct?

      If I would like to show the list of players of a game. Could I have a GamePlayerList servlet which uses Database object, get an array of Player objects which can be shown using a JSP page or the GamePlayerList servlet? should the "retrieving player list data" be in DBPlayer instead?

      Thanks in advance for your advices
        • 1. Re: Question about class design
          There is no right answer. What you have is a model object (Player) a data access object (DBPlayer, normally this would be named something like PlayerDao) and a utilities class to connect to the database (Database or you could call it DatabaseUtil or DatabaseHelper, whatever) Another option for your Database class is to instead call it AbstractDao, have PlayerDao extend it, and in AbstractDao place methods like connect(), rollback() and commit(). One reason you want these methods in either a helper class or a superclass is so you can place your try-catch blocks there and not have to worry about cluttering up the rest of your code.

          In terms of an overall architecture, you should take a look at the MVC pattern. Your Player and PlayerDao objects are your model. Your servlets are your controllers. And your JSP's are your view. It's a fairly standard way of organizing your application.

          You have several options for how to load new players. The first would be as you have done, to place a static factory method in Player. Another option would be to create a separate PlayerFactory object that can create new or return existing or delete existing players. The Servlet would call the PlayerFactory which would delegate to the PlayerDao returning a Player object. In this design, your Servlet would either invoke the PlayerFactory or PlayerDao to save, etc. If you instead want a bit more encapsulation at the price of separation of concerns, you can have the Player object itself have methods like save(), delete(), etc.

          There is no right answer. Designs involve trade-offs and to a large extent personal (or corporate or academic) preference.

          - Saish
          • 2. Re: Question about class design
            Thank you very much for your answer Saish. You helped me a lot.
            • 3. Re: Question about class design
              Best of luck.

              - Saish