Results 1 to 5 of 5
  1. #1
    vgehts is offline Member
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Default Class design best practices

    I've spent the last several hours looking through the Internet trying to find some examples of best practices for designing classes around database tables and I'm turning up with just snippets of code that show just individual SQL statements as opposed to examples of entire classes. What I'm trying to wrap my head around is how much SQL code needs to be in each class that represents a table in my database as opposed to having a base class to handle the various SQL functions (CRUD and multi-row result sets) regardless of the table. For example I have five tables in my database and I would imagine that I at least should have one class for each table and these classes will contain a property for each column in their respective table, but then how to go about laying out the code to perform all of the SQL functions. Also, what about designing around updates where let's so only one field was changed by the user. Would the update statement update all columns or is the better approach to have some way of tracking which fields were changed since the object was instatiated and only update those columns.

    What I'm looking for is any example that can show best practice for designing the classes and if that would involve extending a base class for all of the various SQL operations. I already know how to make the connection and get back a result set, now I'm trying to see some class design examples.

    Thanks,
    Richard

  2. #2
    pugazhendhi's Avatar
    pugazhendhi is offline Pugazhendhi
    Join Date
    May 2011
    Location
    Bangalore
    Posts
    20
    Rep Power
    0

    Default

    u have to use MVC pattern always .,


    consider u have a gui for display list of names that fetch from database this the scenario .,

    Classes to be used .,
    Java Code:
       public class DBManagaer{
    // write singelton method to get coneection from database 
    
     }
    
    public class DAO {
          
         public static Collections<User> listNames () {
               
    //getconnection and the query part here and retuirn the result as domain 
    
    
         return ;
    }
    
    public class User {
    
    // setter and getter 
    
    }
    
    public class gui {
    
    // call the doa list method to get list of names 
    }
    :) Small word can mean it more ,.

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    Pretty much that.
    Though I would make the DAO an interface, so you can easily swap out implementations.
    Also possibly have the GUI interact with a controller layer, that then talks to the DAOs.

    But that there is the basic structure. The model part (User in the above example) should not know anything about how it is persisted. The GUI should not know anything about where the data comes from.

  4. #4
    pugazhendhi's Avatar
    pugazhendhi is offline Pugazhendhi
    Join Date
    May 2011
    Location
    Bangalore
    Posts
    20
    Rep Power
    0

    Default

    what u have told is correct ., usually if u use ., rmi service like exporting ., we ill create interface for serviceimplementation . iorder hide our implementation ., we will share the interface with client and server side ., but for ., single client no need to to do like that ., anyway it will be exposable ., only
    :) Small word can mean it more ,.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    No.
    You code to interface as a matter of course. It has nothing to do with RMI.
    It is good coding practice.
    It allows for easy swapping out of implementations, which also helps in testing.

    A DAO should be an interface.

Similar Threads

  1. class design/interaction problem
    By wotupduck in forum New To Java
    Replies: 4
    Last Post: 03-20-2011, 05:54 AM
  2. Class Design Problems (BlackJack)
    By ConMan in forum New To Java
    Replies: 4
    Last Post: 03-16-2011, 08:38 AM
  3. Design Patterns and Programming Practices
    By macason in forum Suggestions & Feedback
    Replies: 2
    Last Post: 12-14-2009, 09:23 AM
  4. class design
    By dinosoep in forum New To Java
    Replies: 3
    Last Post: 12-07-2009, 10:20 AM
  5. Pls HeLp Me (Design a class named Fan)
    By faw in forum Advanced Java
    Replies: 5
    Last Post: 04-21-2008, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •