问题标签 [singleton-methods]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
0 回答
264 浏览

xamarin.forms - 我应该紧耦合,单例,还是松耦合,没有单例?Azure 服务、Xamarin 表单

我正在制作一个 Xamarin Forms 跨平台应用程序。我一直在试图弄清楚如何让我的应用程序以最佳方式与 Azure 后端集成。我遇到了一个问题。我一直在尝试构建尽可能松耦合的程序。但是后来我被告知将我的 Azure 服务类创建为单例,以停止多次创建并破坏后端连接

因为我不能使用带有单例的接口来使我的应用程序松散耦合。我必须选择。我认为我应该使用接口,以实现松散耦合。但是我对后端开发了解不多。创建我的 AzureServices 数据库类的多个实例会搞砸吗?

谢谢你的帮助。

这是我的 AzureServices 格式的示例

0 投票
2 回答
1325 浏览

java - Singleton Controller Java

I was reading about singletons the other day and thought to implement them in a project of mine, but I didn't like the way it seemed to flow logically, so I created what I'd call a controller class to manage the state of the singleton object. I want to make sure that the logic checks out though, and that I'm not inadvertently spawning off additional instances.

Edit* I do not view this as a duplicate of the question referenced, as that was a typical/standard implementation of a singleton, and this uses a different model altogether by utilizing a controller to manage the state of the singleton.

0 投票
2 回答
361 浏览

c# - 将员工数据列表变成单例类应用程序

我用 C# 制作了一个员工数据列表应用程序,其中包含 AddEmployee、DeleteEmployee、SearchEmployee 等功能。我想制作相同的程序,但使用单例设计模式。我知道 Singleton 的结构,但我搞砸了制作列表,然后将实际员工添加到列表中。

这是我的程序的一部分:

在主程序中,我有一个列表并调用 addStudent 函数:

列表 st = 新列表();

我知道我必须创建一个私有静态实例。

另外,上面应该有一个私有的 Student 构造函数而不是我的构造函数。

这就是创建我的新学生的方法。我也认为我需要一个类似的方法来创建我的列表,但我不确定。

请您给一些指导和建议。谢谢

0 投票
1 回答
43 浏览

java - 反序列化单例

MainList 类的 Singleton-Object 是序列化的,可以通过路径找到。

在 //HERE/// 对象成功反序列化为 mainListObj 的行上,但它是本地的..

我怎样才能使它全球化?我认为它可以通过改变 getInstance 方法来解决..不知何故..

0 投票
2 回答
62 浏览

php - 克隆对象可以在单例模式中具有相同的范围吗?

就我而言,我在下面创建了一个单例对象检查 -

然后在下面创建对象检查

然后克隆 $obj 然后

那么为什么会有不同的范围呢$obj$obj1如果克隆然后共享相同的范围,如何只创建一个对象?

0 投票
1 回答
44 浏览

ruby-on-rails - 有什么方法可以访问称为单例方法的 Parent 对象吗?

鉴于以下功能片段,我无法减少数据库查询:

帐户负载 (0.4ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]

开始

帐户负载 (0.3ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]

测试:用户做了某事

帐户负载 (0.3ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]

测试:用户做了某事

帐户负载 (0.3ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]

测试:用户做了某事

帐户负载 (0.3ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]

测试:用户做了某事

您可以看到 Account 模型是从每个用户的数据库中获取的!

我希望self.account在 Singleton 方法中使用类似的东西来引用原始帐户,但是默认情况下显然不存在这种关系,这就是我目前使用self.new.account.

我还有其他地方可以a从内部获取保存的原始 Account 模型self.do_something吗?我显然可以在参数中传递帐户,但这似乎很乏味,特别是如果我以后可以添加参数...