Class inheritance design.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Benny the Guard
    New Member
    • Jun 2007
    • 92

    Class inheritance design.

    I am writing a class that gives access to a database. The purpose is to hide the complexityof the database behind this obejct and offer a simple interface. That said I want to only allow one object with write access at any given time, but possibly multiple ones with read access. Furthermore when going for write access I need to do some more things such as backup (to provide for recovery in case of failure) that read access does not need worry about. Probably some items specialized in the read only as well.

    Initially I had though of writing this as a single class with a read_only input to the constructor. But I am thinking if I can use inheritance to do this. That is provide the core functionality in a core class and derive a writeable and readonly class off of it. The problem is on construction. What I really want is the following calling order Base Constructor, Derived Constructor, Open connection to the database. In the 1 class design this is simple, but the only way I can think of with hierarchy is to provide an open() method to be called inside the derived classes.

    Whats your opinions on the best approach here?
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by Benny the Guard
    I am writing a class that gives access to a database. The purpose is to hide the complexityof the database behind this obejct and offer a simple interface. That said I want to only allow one object with write access at any given time, but possibly multiple ones with read access. Furthermore when going for write access I need to do some more things such as backup (to provide for recovery in case of failure) that read access does not need worry about. Probably some items specialized in the read only as well.

    Initially I had though of writing this as a single class with a read_only input to the constructor. But I am thinking if I can use inheritance to do this. That is provide the core functionality in a core class and derive a writeable and readonly class off of it. The problem is on construction. What I really want is the following calling order Base Constructor, Derived Constructor, Open connection to the database. In the 1 class design this is simple, but the only way I can think of with hierarchy is to provide an open() method to be called inside the derived classes.

    Whats your opinions on the best approach here?
    What I would suggest is the use of database library. It is the best approach. It also hides complexity and it includes inheritence features also.

    Hope this helps

    Comment

    • Benny the Guard
      New Member
      • Jun 2007
      • 92

      #3
      What database library? In this case I am actually implementing access to the database myself.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This desire of yours:
        Originally posted by Benny the Guard
        I am writing a class that gives access to a database. The purpose is to hide the complexityof the database behind this obejct and offer a simple interface. That said I want to only allow one object with write access at any given time, but possibly multiple ones with read access. Furthermore when going for write access I need to do some more things such as backup (to provide for recovery in case of failure) that read access does not need worry about. Probably some items specialized in the read only as well.
        Looks like:
        1) access read or write
        2) one write at a time
        3 back up
        4) etc...

        which looks a skeletal method which, in turn, looks like the design pattern called Template Method.

        I suggest you hustle up a copy of Design Patterns by Eric Fromm et. al. Addison-Wesley 1994 and check out the Template Methodd. It's on page 325.

        Let me know what you think.

        Comment

        • Benny the Guard
          New Member
          • Jun 2007
          • 92

          #5
          I'll definitly try to find a copy of that book and look it over. Although I did take a first staab at this and it seems to work nicely. What I have is:

          Code:
          class Database_Core
          {
          public:
          	Database_Core ();
          	virtual ~Database_Core ();
          			
          	// Implement a get record object as all derived classes require
                  // this functionality.
          	Record_List *get_records (Record_Type type,
          				const char *obj_name);
            			
          protected:
          	void open_database ();
          };
          
          class Database_Readonly : public Database_Core
          {
          public:
                Database_Readonly () : Database_Core ()
                   { 
                   // Nothing special just open the database
                   open_database ();
                   }
                Database_Readonly::~Database_Readonly ()
          }
           
          class Database_ReadWrite :public Database_Core
          {
          public:
                Database_ReadWrite () : Database_Core ()
                   {
                   if (database_is_already_open)
                         throw "Database already open";
                   open_database ();
                   }
               Database_ReadWrite::~Database_ReadWrite ()
               
              bool add_record (const Record_Base & record);        
          }
          Now I abstracted some of the code, but thats the general idea. It does require I call open_database in each constructor, but because I need to process Database_Core:: Constructor then Database_ReadWr ite::Constructo r then iopen the connection to the database its what I figured would be best.

          Comment

          • dumparun
            New Member
            • Feb 2007
            • 26

            #6
            Originally posted by Benny the Guard
            I'll definitly try to find a copy of that book and look it over. Although I did take a first staab at this and it seems to work nicely. What I have is:

            Code:
            class Database_Core
            {
            public:
            	Database_Core ();
            	virtual ~Database_Core ();
            			
            	// Implement a get record object as all derived classes require
                    // this functionality.
            	Record_List *get_records (Record_Type type,
            				const char *obj_name);
              			
            protected:
            	void open_database ();
            };
            
            class Database_Readonly : public Database_Core
            {
            public:
                  Database_Readonly () : Database_Core ()
                     { 
                     // Nothing special just open the database
                     open_database ();
                     }
                  Database_Readonly::~Database_Readonly ()
            }
             
            class Database_ReadWrite :public Database_Core
            {
            public:
                  Database_ReadWrite () : Database_Core ()
                     {
                     if (database_is_already_open)
                           throw "Database already open";
                     open_database ();
                     }
                 Database_ReadWrite::~Database_ReadWrite ()
                 
                bool add_record (const Record_Base & record);        
            }
            Now I abstracted some of the code, but thats the general idea. It does require I call open_database in each constructor, but because I need to process Database_Core:: Constructor then Database_ReadWr ite::Constructo r then iopen the connection to the database its what I figured would be best.
            you can have a singleton class for the write access one.
            so that there is only a single instance of the same.

            Now comes read access, which you can have many.
            i dont know why you are having inheritance, since there is nothing which you are going to share. you can have a composition if intended....
            templating is also not necessary.

            another method is to use a factory method and see control the instantiation from there.

            Comment

            Working...