Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 摆脱单身汉?_Javascript_Oop_Singleton - Fatal编程技术网

Javascript 摆脱单身汉?

Javascript 摆脱单身汉?,javascript,oop,singleton,Javascript,Oop,Singleton,我正在编写一个变得太复杂的web应用程序,我想简化我的组件如何协同工作。我有几个单身汉,他们知道所有与他们有关的东西 例如,我有一个窗口系统,其中包含所有存在的窗口对象的数组。所有的窗口都互不了解,但我有一个恼人的单例,比如closeAllWindows函数或ifsameWindowExists{return}类型的东西,我认为需要某种方式来跟踪所有的窗口。我在程序启动时创建一个Windows系统实例 感觉这些是不必要的,因为他们知道的比他们应该知道的更多。我还有其他选择吗 编辑:这里有一些代码

我正在编写一个变得太复杂的web应用程序,我想简化我的组件如何协同工作。我有几个单身汉,他们知道所有与他们有关的东西

例如,我有一个窗口系统,其中包含所有存在的窗口对象的数组。所有的窗口都互不了解,但我有一个恼人的单例,比如closeAllWindows函数或ifsameWindowExists{return}类型的东西,我认为需要某种方式来跟踪所有的窗口。我在程序启动时创建一个Windows系统实例

感觉这些是不必要的,因为他们知道的比他们应该知道的更多。我还有其他选择吗

编辑:这里有一些代码显示了各种系统的创建


我真正不喜欢的是,我将许多函数从其他系统传递给其他系统,而它们又将这些函数传递给对象——就像一个窗口——它们创建的对象似乎伸缩性不好。

手动依赖项注入可以由一个单例提供。我知道您正在尝试摆脱这些,但是如果您有一个跟踪所有有趣实例(如windows)的实例,您可以说像Injector.getWindow,Debug;获取调试代码所需的任何窗口实例。这仍然为您提供了注入—如果需要,可以为调试类提供不同的窗口,并且可以通过多种方式配置提供的类实例的配置—数据、硬编码等

然后,您还可以使用Injector.getAllWindow获取并关闭它们


我知道您仍然有一个单例,但至少它只是一个,它为您在一个位置重新配置类提供了一些灵活性。

您可以尝试将它传输到所有窗口继承的基类,而不是将窗口管理逻辑放在窗口上方的单例中。它可能看起来像:

function BaseWindow() {
    //whatever common constructor logic you may want
    //such as creating an id
    this.id = this.id + 1
}

//this is static
BaseWindow.activeWindow = null;

//this is a property visible to each window instance but is updated by the base class
BaseWindow.prototype.id = 0;

//this is a property visible to each window instance but may be overridden by a subclass
BaseWindow.prototype.name = "BaseWindow";

//this is function visible to each window instance
BaseWindow.prototype.show = function ( ) {
    //hide BaseWindow.activeWindow then show "this" window;
};


function WindowA() {
    //do some window specific stuff like set the window name
    this.name = "WindowA";
}

WindowA.prototype = new BaseWindow;

你所做的听起来像是一个窗口管理器——这并没有什么错,但如果你不能看到更多的代码并理解你所说的内容的复杂性,就很难判断。好吧,你有这个类,你只创建了它的一个实例。向该类添加逻辑以阻止您创建更多实例是否确实对您有所帮助?您是否尝试创建更多实例?如果您无意中编写了代码来创建另一个实例,那么单例逻辑需要多长时间才能真正提醒您这一事实另一方面,你的抱怨是这些例子知道的比他们应该知道的更多;除了这里有一堆窗口实例之外,Windows系统还知道什么?@Karl,@Stephen:我添加了代码。有太多的函数在系统之间来回抛出,这让我觉得应该有更好的方法来实现它。Java不是Javascript。虽然有些概念可能会在两者之间转移,但是Spring所做的不会帮助任何人,除非他们想在JS中模仿Spring——这可能需要大量的工作。我改变了建议,仍然建议使用注入,但是手动而不是自动。如果那样做,就不会有太多的工作。
function BaseWindow() {
    //whatever common constructor logic you may want
    //such as creating an id
    this.id = this.id + 1
}

//this is static
BaseWindow.activeWindow = null;

//this is a property visible to each window instance but is updated by the base class
BaseWindow.prototype.id = 0;

//this is a property visible to each window instance but may be overridden by a subclass
BaseWindow.prototype.name = "BaseWindow";

//this is function visible to each window instance
BaseWindow.prototype.show = function ( ) {
    //hide BaseWindow.activeWindow then show "this" window;
};


function WindowA() {
    //do some window specific stuff like set the window name
    this.name = "WindowA";
}

WindowA.prototype = new BaseWindow;