How to get specific instance of class from another class in Java?
Asked Answered
T

5

6

I've created the following class with the main method, which creates new instance of Application and instances of ApplicationModel, ApplicationView and ApplicationController for this particular Application.

public class Application
{

    // Variables

    private ApplicationSettings         settings;
    private ApplicationModel            model;
    private ApplicationView             view;
    private ApplicationController       controller;

    // Constructor

    public Application()
    {
        settings        = new ApplicationSettings();
        model           = new ApplicationModel();
        view            = new ApplicationView(model);
        controller      = new ApplicationController();
    }

    // Main method

    public static void main(String[] args)
    {
        Application application = new Application();
    }

    // Getters for settings, model, view, controller for instance of Application

}

I know, that there will always be only one unique instance of Application.

And I want to get this particular instance in my ApplicationModel, ApplicationView and ApplicationController classes.

How is it possible?

Theoretician answered 16/8, 2012 at 12:27 Comment(0)
B
9

I would use a singleton on Application class.

Put a public static method to return your one and only application instance.

public class Application
{
    private Application() { } // make your constructor private, so the only war
                              // to access "application" is through singleton pattern

    private static Application _app;

    public static Application getSharedApplication() 
    {
        if (_app == null)
            _app = new Application();
        return _app;
    }
}

You can read more about singleton design pattern here.

Every time you need the one and only Application instance, you do:

Application app = Application.getSharedApplication();
Barncard answered 16/8, 2012 at 12:31 Comment(2)
And, probably, you made a typo. Application _app should be static, if it is referenced from static method. Please, correct it, as I'm not sure. :)Theoretician
@EdwardRuchevits: yes! It wasn't a typo. I just forget to put the keyword in there. It must be static.Barncard
G
2

You want to use the Singleton design pattern if you need to guarantee there will only be one instance of Application and you need it to be accessible by those classes. I'm not going to comment on if it's the best way to build your application, but it will satisfy the requirements in your question.

Singleton design pattern tutorial

Grote answered 16/8, 2012 at 12:31 Comment(1)
Yes, that will perfectly suit me. Thank you!Theoretician
Z
2

Change the constructor of your Application class to be private and send the this object to the ApplicationXxx classes.

private Application()
{
    settings        = new ApplicationSettings(this);
    model           = new ApplicationModel(this);
    view            = new ApplicationView(this, model);
    controller      = new ApplicationController(this);
}

You will be able to call new on the Application class from within the main method.

Your ApplicationSettings, ApplicationModel, ApplicationView and ApplicationController must be updated to take an Application object in their constructors.

Zusman answered 16/8, 2012 at 12:40 Comment(2)
Thank you for your answer. I've been using this approach, but it wasn't what I wanted to achieve. Now I'd rather use Singleton design pattern, as proposed by Pablo Santa Cruz and smcg.Theoretician
@EdwardRuchevits Just don't forget that Singletons are evil.Zusman
O
1

Add parameter of the Application type to the constructors of the composites. When you create their instances just pass this. For example

public class Application {
  String s = "Setting";
  class ApplicationSettings {
    Application sc;
    ApplicationSettings(Application sc){
      this.sc = sc;
      System.out.println(sc.s);
    }
  }

  // Variables

  private ApplicationSettings settings;

  // Constructor

  public Application()
  {
    settings = new ApplicationSettings(this);
  }

  // Main method

  public static void main(String[] args)
  {
    Application application = new Application();

  }

  // Getters for settings, model, view, controller for instance of Application

}
Obsequent answered 16/8, 2012 at 12:52 Comment(1)
I think, Singleton pattern will be better for me, because I want only one instance of my Application. But thank you for your answer!Theoretician
A
0

I tried this right now, you can pass this as a constructor arg within the constructor of Application to other classes (assuming the have a reference declared for the main class). That way you can obviously make multiple instances of the Application class. If that is what you want...

Avalos answered 16/8, 2012 at 12:39 Comment(1)
I want to create only one instance of Application. Anyway, thank you for your answer.Theoretician

© 2022 - 2024 — McMap. All rights reserved.