0

I have been assigned an IT project in which we have to program various different GUI's to do various things. We are also using a database. Let's assume we are accessing an "EntityManager" in a class called "Database":

public class GUI1 {

    private Database myDatabase;
    public void setDatabase(Database DB){
        myDatabase = DB;
    }

}
public class GUI2 {

    private Database myDatabase;
    public void setDatabase(Database DB){
        myDatabase = DB;
    }

}
public class GUI3 {

    private Database myDatabase;
    public void setDatabase(Database DB){
        myDatabase = DB;
    }
}
etc...

Lets say I'm in "GUI1" and I want to switch to "GUI3". After initializing "GUI3" I would have to pass "myDatabase" reference to it via the "setDatabase()" method, but if I want to go back to "GUI1", I would have to pass back the database reference again...

By now I have around 15 GUIs and it get's annoying to copy and paste the same code around when I know it could be replaced easily. In this case, wouldn't it be correct to just use a static reference to whatever I want inside the "Database" class instead of passing around the reference between all my "GUI*" classes?

1
  • Have you considered making an AbstractGUI base class to contain the shared code? Commented Aug 2, 2015 at 14:13

1 Answer 1

2

Create a singleton database object, where everybody access the same object:

public class Database {

    private Database(){ // privatize the constructor
        // your code here
    }


    private static Database INSTANCE;
    public static Database getInstance() {
        if(INSTANCE == null) {
            // let's make it thread-safe
            synchronized(Database.class) {
                if(INSTANCE == null) // may have changed in the mean while
                                     // by other thread
                    INSTANCE = new Database();
            }
        }
        return INSTANCE;
    }

}

EDIT: Even better, from a thread-safe perspective is the enum:

public enum Database {

    INSTANCE(); // pair of parenthesis, for constructor

    Database() { // constructor
        // your code here
    }

    public static Database getInstance() {
        return INSTANCE;// initialization controlled by system
    }

    public void someMethod(){
        // even allows you to add custom methods
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what I did first, but then my teacher said it would be incorrect according to OOP principles... I just want to verify, this method would be correct?
I doubt that it breaks OOP. Look Java provided the enum which is really just that. (And even better than my example, in a thread-safe perspective). Think of it, I'm going to edit my post to reflect it.
@Downvotter please comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.