This content has been marked as final. Show 3 replies
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.
Thank you very much for your answer Saish. You helped me a lot.
Best of luck.