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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Edward Ruchevits
  • 6,411
  • 12
  • 51
  • 86

5 Answers5

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();
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
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

smcg
  • 3,195
  • 2
  • 30
  • 40
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.

maba
  • 47,113
  • 10
  • 108
  • 118
  • 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. – Edward Ruchevits Aug 16 '12 at 16:33
  • @EdwardRuchevits Just don't forget that [Singletons are evil](http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons). – maba Aug 16 '12 at 19:14
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

}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I think, Singleton pattern will be better for me, because I want only one instance of my `Application`. But thank you for your answer! – Edward Ruchevits Aug 16 '12 at 16:36
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...

Less
  • 3,047
  • 3
  • 35
  • 46