SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is this a 'god class'?

    i'm still pretty new to oop. i've heard many warnings against the use of 'god classes'. i'm reading a cms tutorial (http://www.intranetjournal.com/artic...07_06_04a.html). the tutorial divides the basic structure of their cms into database, file uploads, templates, and authentication. all of these extend a system object. here's their description:
    We have four main PHP modules (or "classes") that will be widely used in the system. These are tasked with accessing the database, allowing the user to upload files to the site, reading and writing templates, and logging users in and out. These classes all "extend" one parent class called "systemObject."
    to my uninitiated eyes, this looks suspiciously like the dreaded 'god class' which i've been warned so heavily against. if i'm wrong, can someone please explain the difference? if i'm right, can someone please briefly outline the correct way to do this?

  2. #2
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A god class is something which, rather than focussing on a specific responsibility, tries to do many different things. OOP is all about drawing lines of demarcation cleverly; a god class would be ripe for refactoring into smaller units.

    The intent of the SystemComponent superclass appears to be to make some configuration settings available globally. It doesn't seem to have any behaviour: objects ought to bring variables and the functions which work on them together under one roof. You could almost call it a Phrasebook if you were feeling generous. And had been drinking.

    In effect, the author is using inheritance to pass some vars around. Assuming you did want to pass a SystemComponent around in the first place, that's extremely naive. OOP beginners often latch onto inheritance (myself included) but it shouldn't be used simply to create an extended scope between two unrelated classes. SystemComponent could easily be replaced with a few config constants.

    I don't think that's a very useful tutorial or good design (although not in the Tony Rantson league). A copy of Patterns of Enterprise Application Architecture by Martin Fowler would have more to get your teeth into.

  3. #3
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP helps the design process by supporting object-oriented programming (OOP). When putting together our system, there are certain chunks of programming that are needed again and again, such as database access, user authentication, etc. To keep this code neat and tidy, we bundle it together in PHP files called "classes."
    This to me sounds like your typical OO newbie using a class to aggregate a bunch of loosely related functions. A sinlge class for each sub system is going to be a god class because each sub system will be made from a number of roles.

    I don't see why he's using this SystemComponent class. obviously, he's a newbie as he's extending a base class which doesn't have much relationship with the child classes (bad coupling / dependancies). Like all newbies, inheritance looks like an obvious example. a Singleton would be best here.

  4. #4
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MiiJaySung
    This to me sounds like your typical OO newbie using a class to aggregate a bunch of loosely related functions.
    Actually, if you look at his code, it is a Monostate pattern: http://www.intranetjournal.com/artic...08_05_04b.html

    The code would work fine (witout modification!) if he removed the "extends SystemComponent" line. The only time he uses SystemComponent is here, in the DbConnector constructor:

    $settings = SystemComponent::getSettings();

    Remove the inheriance and rename "SystemComponent" to just "Application" and you have yourself some half useful code. Take a look at the Monostate/Singleton thread for more info: http://www.sitepoint.com/forums/showthread.php?t=215994

    Douglas
    Hello World

  5. #5
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see it more commonly as either:
    PHP Code:
    class DbConnectort {
        var 
    $theQuery;
        var 
    $link;

       function 
    DbConnector($connectionsettings){
            
    $settings $connectionsettings->getSettings();
            
    $host $settings['dbhost'];
            
    $db $settings['dbname'];
            
    $user $settings['dbusername'];
            
    $pass $settings['dbpassword']; 
    Or simply:
    PHP Code:
    class DbConnectort {
        var 
    $theQuery;
        var 
    $link;

       function 
    DbConnector($settings){
            
    $host $settings['dbhost'];
            
    $db $settings['dbname'];
            
    $user $settings['dbusername'];
            
    $pass $settings['dbpassword']; 
    I don't think the code in the article was so much a god class as just not the best use of inheritance.


Bookmarks

Posting Permissions

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