Top Qs
Timeline
Chat
Perspective

God object

Large or very powerful object in programming From Wikipedia, the free encyclopedia

Remove ads

In object-oriented programming, a god object (sometimes also called an omniscient or all-knowing object) is an object that references a large number of distinct types, has too many unrelated or uncategorized methods, or some combination of both.[1] The god object is an example of an anti-pattern and a code smell.[2]

Remove ads

Characteristics

Summarize
Perspective

A common programming technique is to separate a large problem into several smaller problems (a divide and conquer strategy) and create solutions for each of them. Once the smaller problems are solved, the big problem as a whole has been solved. Therefore a given object for a small problem only needs to know about itself. Likewise, there is only one set of problems an object needs to solve: its own problems. This also follows the single-responsibility principle.

In contrast, a program that employs a god object does not follow this approach. Most of such a program's overall functionality is coded into a single "all-knowing" object, which maintains most of the information about the entire program, and also provides most of the methods for manipulating this data. Because this object holds so much data and requires so many methods, its role in the program becomes god-like (all-knowing and all-encompassing). Instead of program objects communicating among themselves directly, the other objects within the program rely on the single god object for most of their information and interaction. Since this object is tightly coupled to (referenced by) so much of the other code, maintenance becomes more difficult than it would be in a more evenly divided programming design. Changes made to the object for the benefit of one routine can have a ripple effect on other unrelated functions.

A god object is the object-oriented analogue of failing to use subroutines in procedural programming languages, or of using far too many global variables to store state information.

Whereas creating a god object is typically considered bad programming practice, this technique is occasionally used for tight programming environments (such as microcontrollers), where the performance increase and centralization of control are more important than maintainability and programming elegance.

Remove ads

Example

Summarize
Perspective

The class GameManager in this C++ example can be seen as a "god object", doing everything from managing players, handling game logic, rendering, reading input and file I/O.

import std;

using std::string;
using std::vector;

class GameManager {
private:
    vector<string> players;
    int score;
    bool isRunning;
public:
    GameManager():
        score(0), isRunning(false) {}

    ~GameManager() = default;

    void addPlayer(const string& name) {
        players.push_back(name);
        std::println("Added player: {}", name);
    }

    void listPlayers() const {
        std::println("Players:");
        for (const string& p : players) {
            std::println(" - {}", p);
        }
    }

    void startGame() {
        isRunning = true;
        score = 0;
        std::println("Game started!");
    }

    void updateGame() {
        if (isRunning) {
            score += 10;
            std::println("Score updated: {}", score);
        }
    }

    void endGame() {
        isRunning = false;
        std::println("Game over! Final score: {}", score);
    }

    void draw() {
        std::println("[Rendering Game Screen]");
    }

    void handleInput(const string& input) {
        if (input == "quit") {
            endGame();
        } else if (input == "score") {
            std::println("Current score: {}", score);
        } else {
            std::println("Unknown input");
        }
    }

    void saveGame() {
        std::println("Saving game state to disk...");
    }

    void loadGame() {
        std::println("Loading game state from disk...");
    }
};

Instead, this could be more appropriately divided among separated responsibilities:

class PlayerManager {
    // handles players
};

class GameLogic {
    // handles rules and state
};

class Renderer {
    // handles drawing
};

class InputHandler {
    // handles user input
};

class SaveSystem { 
    // handles file I/O
};

class GameManager {
private:
    PlayerManager playerManager;
    GameLogic logic;
    Renderer renderer;
    InputHandler input;
    SaveSystem saveSystem;
public:
    GameManager() = default;
    ~GameManager() = default;

    void run() {
        // ...
    }
};
Remove ads

See also

References

Loading related searches...

Wikiwand - on

Seamless Wikipedia browsing. On steroids.

Remove ads