如何从Java中的另一个类获取类的特定实例? [英] How to get specific instance of class from another class in Java?

查看:362
本文介绍了如何从Java中的另一个类获取类的特定实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 main 方法创建了下面的类,它创建了 Application 的新实例, ApplicationModel ApplicationView ApplicationController $ c> Application 。

  public class Application 
{

//变量

private ApplicationSettings设置;
private ApplicationModel model;
private ApplicationView视图;
private ApplicationController controller;

//构造函数

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

//主方法

public static void main(String [] args)
{
应用程序应用程序);
}

//应用程序的设置,模型,视图,控制器的获取器

}
pre>

我知道, Application 只会有一个唯一的实例。



而我想在我的 ApplicationModel ApplicationView code> ApplicationController 类。



如何实现?

解决方案

我将在Application类上使用单例。



放置一个 public static 方法返回您的唯一应用程序实例。

  public class Application 
{
private Application ){} //使你的构造函数私有,所以只有war
//访问应用程序是通过单例模式

private static Application _app;

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

您可以阅读更多关于 singleton设计模式 此处



每当您需要一个且只有 Application 实例时,您可以执行:

 应用程式app = Application.getSharedApplication(); 


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?

解决方案

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();

这篇关于如何从Java中的另一个类获取类的特定实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆