How to get specific instance of class from another

2019-04-29 22:53发布

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?

5条回答
欢心
2楼-- · 2019-04-29 23:14

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

}
查看更多
家丑人穷心不美
3楼-- · 2019-04-29 23:15

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.

查看更多
女痞
4楼-- · 2019-04-29 23:19

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...

查看更多
Juvenile、少年°
5楼-- · 2019-04-29 23:31

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

查看更多
淡お忘
6楼-- · 2019-04-29 23:37

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();
查看更多
登录 后发表回答