单身模式 - 设计问题 [英] singleton pattern - design question

查看:91
本文介绍了单身模式 - 设计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在构建一个应用程序,我有一个ConfigManager类来保存有关应用程序的所有重要信息。

我只需要一个ConfigManager类的实例,可以在整个应用程序中从应用程序内的任何位置使用。根据我的阅读,单例模式似乎与我的问题相符

但是,我使用带参数的构造函数构建了ConfigManager。


到现在为止,我'尝试过使用全局变量。 ConfigManager类的一个实例声明为全局并在整个应用程序中使用。它有效,但我对此并不十分舒服。这就是我需要第二意见的原因。

谢谢,


Adina

Hello,
I''m building an application, in which I have a ConfigManager class to hold all the important info about the application.
I need only one instance of ConfigManager class to be available throughout the application, from anywhere inside the application. From what I have read, the singleton pattern seems to match my problem
However, I built the ConfigManager with a constructor with parameters.

Until now, I''ve tried using global variables. One instance of ConfigManager class declared as global and used throughout th app. It works, but I am not very confortable with this. That''s why I need a second opinion.

Thanks,

Adina

推荐答案


你好,

我正在构建一个应用程序,我在其中有一个ConfigManager类来保存有关的所有重要信息应用程序。

我只需要在整个应用程序中从应用程序内的任何位置使用一个ConfigManager类实例。根据我的阅读,单例模式似乎与我的问题相符

但是,我使用带参数的构造函数构建了ConfigManager。


到现在为止,我'尝试过使用全局变量。 ConfigManager类的一个实例声明为全局并在整个应用程序中使用。它有效,但我对此并不十分舒服。这就是我需要第二意见的原因。

谢谢,


Adina
Hello,
I''m building an application, in which I have a ConfigManager class to hold all the important info about the application.
I need only one instance of ConfigManager class to be available throughout the application, from anywhere inside the application. From what I have read, the singleton pattern seems to match my problem
However, I built the ConfigManager with a constructor with parameters.

Until now, I''ve tried using global variables. One instance of ConfigManager class declared as global and used throughout th app. It works, but I am not very confortable with this. That''s why I need a second opinion.

Thanks,

Adina



您是否尝试创建单例,然后(创建和)使用setVariable()方法?

Have you tried to create the singleton, and then (creating and) using setVariable() methods?



您好,

我正在构建一个应用程序,我有一个ConfigManager类来保存有关应用程序的所有重要信息。

我只需要一个ConfigManager类实例从应用程序内部的任何地方都可以获得。根据我的阅读,单例模式似乎与我的问题相符

但是,我使用带参数的构造函数构建了ConfigManager。


到现在为止,我'尝试过使用全局变量。 ConfigManager类的一个实例声明为全局并在整个应用程序中使用。它有效,但我对此并不十分舒服。这就是我需要第二意见的原因。

谢谢,


Adina
Hello,
I''m building an application, in which I have a ConfigManager class to hold all the important info about the application.
I need only one instance of ConfigManager class to be available throughout the application, from anywhere inside the application. From what I have read, the singleton pattern seems to match my problem
However, I built the ConfigManager with a constructor with parameters.

Until now, I''ve tried using global variables. One instance of ConfigManager class declared as global and used throughout th app. It works, but I am not very confortable with this. That''s why I need a second opinion.

Thanks,

Adina



据我所知,你可以使用参数化构造函数创建单例,但问题是谁将分配参数?如果你能做出那个决定那么就有可能。尝试查看 http://ehsanbraindump.blogspot.com ,它有一些关于单身人士的好内容。

As far as I know you can create singleton''s with parameterized constructors but the problem is who will assign the parameters? If you can make that decision then it will be possible. Try lookin in http://ehsanbraindump.blogspot.com it has some good content on singletons.



你好,

我正在构建一个应用程序,我有一个ConfigManager类来保存关于应用程序的所有重要信息。

我只需要在整个应用程序中从应用程序内的任何位置使用一个ConfigManager类实例。根据我的阅读,单例模式似乎与我的问题相符

但是,我使用带参数的构造函数构建了ConfigManager。


到现在为止,我'尝试过使用全局变量。 ConfigManager类的一个实例声明为全局并在整个应用程序中使用。它有效,但我对此并不十分舒服。这就是我需要第二意见的原因。

谢谢,


Adina
Hello,
I''m building an application, in which I have a ConfigManager class to hold all the important info about the application.
I need only one instance of ConfigManager class to be available throughout the application, from anywhere inside the application. From what I have read, the singleton pattern seems to match my problem
However, I built the ConfigManager with a constructor with parameters.

Until now, I''ve tried using global variables. One instance of ConfigManager class declared as global and used throughout th app. It works, but I am not very confortable with this. That''s why I need a second opinion.

Thanks,

Adina



我认为你可以用这种方式设计你的课程


每个发布指南删除的代码希望这能解决你的问题。振作起来!!!

I think you can make design of your class this way

code removed per posting guidelines Hope this solves your problem. Cheer up !!!


这篇关于单身模式 - 设计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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