摆脱单身人士?

发布于 2024-12-07 18:17:08 字数 1398 浏览 0 评论 0原文

我正在编写一个变得过于复杂的网络应用程序,我想简化我的组件如何协同工作。我有一些单身人士“了解”与他们有关的所有对象。

例如,我有一个 windowSystem ,它保存所有存在的 window 对象的数组。所有的窗口彼此都不了解,但我有一个令人恼火的单例,用于诸如 closeAllWindows() 函数或 if(sameWindowExists()) { return } -类型的东西(我认为)需要某种方式来跟踪所有窗口。当我的程序启动时,我创建一个 windowSystem 实例。

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

编辑:这是一些代码,显示了各种 _____System 的创建,

var refDate = usDate.now();

    var eventSystem = usEventSystem($("#topLevelElement")),
        backend = usBackend(eventSystem.trigger),
        windowSystem = usWindowSystem($("#windows"), eventSystem.registerEvent),
        timelineSystem = usTimelineSystem($("#view"), 
                                        backend.getEvents, 
                                        usDate.now().shift({ hours:-6 }), 
                                        usDate.now().shift({ hours:6 }),
                                        eventSystem.registerEvent,
                                        eventSystem.unregisterEvent,
                                        windowSystem.createWindow);

    usWindow.setRegisterEventFunc(eventSystem.registerEvent).setUnregisterEventFunc(eventSystem.unregisterEvent);                           

我真正不喜欢的是,我将许多函数从其他系统传递给彼此(然后他们又将这些传递给他们创建的对象(例如窗口)),这似乎无法很好地扩展。

I'm writing a web app that is getting too complex and I'd like to simplify how my components work together. I have a few singletons that "know" about all the objects they have to do with.

For example I have a windowSystem that holds an array of all the window objects that exist. All of the windows don't know anything about each other but I have this irritating singleton there for things like a closeAllWindows() function or if(sameWindowExists()) { return } -type things that (I think) require some sort of way to keep track of all the windows. I create one windowSystem instance when my program starts.

It feels like these are unnecessary because they know more than they should. What other options do I have?

Edit: Here is some code that shows the creation of various _____Systems

var refDate = usDate.now();

    var eventSystem = usEventSystem($("#topLevelElement")),
        backend = usBackend(eventSystem.trigger),
        windowSystem = usWindowSystem($("#windows"), eventSystem.registerEvent),
        timelineSystem = usTimelineSystem($("#view"), 
                                        backend.getEvents, 
                                        usDate.now().shift({ hours:-6 }), 
                                        usDate.now().shift({ hours:6 }),
                                        eventSystem.registerEvent,
                                        eventSystem.unregisterEvent,
                                        windowSystem.createWindow);

    usWindow.setRegisterEventFunc(eventSystem.registerEvent).setUnregisterEventFunc(eventSystem.unregisterEvent);                           

What I really dislike about it is that I'm passing lots of functions from other systems into each other (and they in turn pass those on to the objects -like a window- they create) which doesn't seem to scale well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡墨 2024-12-14 18:17:08

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

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;

Instead of having your window managing logic in a singleton sitting above the windows you could try transferring it to a base class that all windows inherit from. It could look something like:

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;
栖竹 2024-12-14 18:17:08

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

然后,您还可以使用 Injector.getAll("Window") 来获取并关闭它们。

我意识到您仍然有一个单例,但至少它只是一个,并且它为您提供了一些灵活性,可以在一个地方重新配置您的类。

Manual dependency injection could be provided by one singleton. I know you are trying to get rid of those, but if you had a single one that tracked all your interesting instances (like windows) you could say something like Injector.get("Window", "Debug"); to grab whatever window instance your Debug code wants. This still gives you injection--a different window could be provided to the Debug class if needed, and the configuration of provided class instances could be configured in a number of ways (Data, hard-coded, etc).

You could also then use Injector.getAll("Window") to get and close them all.

I realize you've still got a singleton, but at least it's just one and it provides you some flexibility down the line to reconfigure your classes in one place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文