热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

我想要一个单独的设计方案。-Iwantadesignalternativetoasingleton

Irealizethereismuchdiscussionaboutsingletonsandwhythatarebad.Thatisnotwhatthisques

I realize there is much discussion about singletons and why that are bad. That is not what this question is about. I understand the drawbacks to singletons.

我意识到有很多关于单身人士的讨论,为什么这很糟糕。这不是这个问题的实质。我理解单身人士的缺点。

I have a scenario where using a singleton is easy and appears to make sense. However, I want an alternative that will accomplish what I need without a lot of overhead.

我有这样一个场景:使用单例对象很容易,并且看起来很有意义。然而,我想要一个替代方案,它可以在不需要大量开销的情况下完成我所需要的工作。

Our application is designed as a client that typically runs on laptops in the field and communicates with a back end server. We have a status bar at the bottom of the main application. It contains a few text areas that show various statues and information as well as several icons. The icons change their image to indicate their state. Such as a GPS icon that indicates if it is connected or not as well as error state.

我们的应用程序被设计成一个客户端,通常在该领域的笔记本电脑上运行,并与后端服务器通信。在主应用程序的底部有一个状态栏。它包含一些文本区域,显示各种雕像和信息以及一些图标。图标会改变它们的图像来表示它们的状态。例如一个GPS图标,指示它是否连接以及错误状态。

Our main class is called MobileMain. It owns the status bar area and is responsible for creating it. We then have a StatusBarManager class. The StatusBarManager is currently a static class, but could also be a singleton. Here is the start of the class.

我们的主要课程叫MobileMain。它拥有状态栏区域并负责创建它。然后我们有一个StatusBarManager课程。StatusBarManager目前是一个静态类,但也可以是一个单例类。这是课程的开始。

public static class StatusBarManager
{
    static ScreenStatusBar StatusBar;

    /// 
    /// Creates the status bar that it manages and returns it.
    /// 
    public static ScreenStatusBar CreateStatusBar()
    {
        StatusBar = new ScreenStatusBar();
        return StatusBar;
    }

The MobileMain asks the StatusBarManager for a StatusBar. It then uses the StatusBar. No other classes see the StatusBar, just the StatusBarManager.

手机管理员向StatusBarManager要一个StatusBar。然后它使用StatusBar。没有其他类看到StatusBar,只有StatusBarManager。

Updates to the status bar can come from pretty much anywhere in the application. There are around 20 classes that can update the text areas on the status bar and additional classes that update the icon states.

状态栏的更新几乎可以来自应用程序中的任何地方。有大约20个类可以更新状态栏上的文本区域,还有其他类可以更新图标状态。

There will only every be one StatusBar and one StatusBarManager.

只有一个StatusBar和一个StatusBarManager。

Any suggestions for a better implemention?

有什么建议可以更好的实施吗?

Some thoughts that I had:

我有一些想法:

Make the StatusBarManager an instance class. In my MobileMain class hold onto a static public instance of the StatusBarManager class. Then to do status bar updates you would call MobileMain.StatusBarManager.SetInformationText or some other method of the manager. The StatusBarManager would not be a singleton, but the MobileMain would only be creating a static instance of it. The issue here is that MobileMain now has a StatusBar and a StatusBarManager, which just manages the StatusBar it owns. Still also have a globally avaialble static instance to the StatusBarManager, just a different owner.

使StatusBarManager成为一个实例类。在我的MobileMain类中,保持StatusBarManager类的静态公共实例。然后进行状态栏更新,您可以调用MobileMain.StatusBarManager。SetInformationText或管理器的其他方法。StatusBarManager不是单例的,但是MobileMain只会创建它的静态实例。这里的问题是,MobileMain现在有一个StatusBar和StatusBarManager,它只管理它拥有的StatusBar。对于StatusBarManager仍然有一个全局通用的静态实例,只是不同的所有者。

Another idea was to use something like an EventEggregator class. I've never used one, but have read about them. I guess the concept is that it would be a globally available class. In each class that wants to update the status bar it would publish a StatusBarUpdate event. The StatusBarManager would be the only classes subscribing to the StatusBarUpdate event, and receive all of the notifications. I've read though that can end up with leaks with this approach if you are not carefull with unsubscribing from events when cleaning up objects. Is this approach worth looking into?

另一个想法是使用类似于EventEggregator类的东西。我从未用过,但读过有关它们的书。我想这个概念是它是一个全局可用的类。在每个想要更新状态栏的类中,都会发布一个StatusBarUpdate事件。StatusBarManager将是订阅StatusBarUpdate事件的惟一类,并接收所有通知。我读到过,如果您在清理对象时没有从事件中取消订阅,那么这种方法可能会导致泄漏。这种方法值得研究吗?

5 个解决方案

#1


2  

Having a StatusBar class or a StatusBarManager class or not is not a big deal. But having many classes in your app know about StatusBars and StatusBarManagers is a bad idea, it will cause strong coupling, and some day probably pain.

是否有StatusBar类或StatusBarManager类并不是什么大问题。但是在你的应用程序中有很多类知道statusbar和StatusBarManagers是一个坏主意,它会导致强耦合,并且有一天可能会很痛苦。

How?

如何?

Imagine that the components that currently report status to a status bar have to be reused in another app that - uses a text console to report status? - reports status to multiple places? or - doesn't report status at all!

假设当前向状态栏报告状态的组件必须在另一个使用文本控制台报告状态的应用程序中重用?-向多个地方报告状态?或者根本不报告状态!

Best alternative: -Event listening. Expose a Status Changed event on your class (you can use a callback), or perhaps on an existing shared resource that your classes have in common. Other parties, like your status bar, can subscribe to the event. And should unsubscribe whenever the subscription is no longer needed/valid, to prevent leaks, as you mention!

最好的选择:事件监听。在类中公开一个状态更改事件(可以使用回调),或者可能是在您的类共有的现有共享资源上。其他的团体,比如你的状态栏,可以订阅这个活动。并且当订阅不再需要/有效时应该取消订阅,以防止泄漏,正如您所提到的!

-Since you've tagged WPF, for WPF, having a dependency property 'StatusText', might seem like another tempting option, with this approach when you have multiple status properties, you need a way of figuring out which one is telling you the most interesting status that needs to be displayed on your status bar now! Which could be a binding, multibinding (blech, complexity), or dependency property changed event handler.

因为你标记WPF为WPF,依赖房地产“StatusText”,似乎是另一个诱人的选择,使用这种方法,当你有多个状态属性,您需要一种方法找出哪一个是告诉你最有趣的地位在你现在的状态栏需要显示的!可以是绑定、多绑定(blech、复杂性)或依赖属性更改的事件处理程序。

However - I would advise you to keep DependencyObjects and DependencyProperties limited to your UI layer as much as possible. The reason is that they rely implicitly on a Dispatcher on the UI thread, and so can't be adapted easily for non-UI chores.

但是——我建议您尽可能将DependencyObjects和DependencyProperties限制在UI层中。原因是它们隐式地依赖于UI线程上的分派器,因此不能轻松地适应非UI事务。

Since there are many different parts of your app you may also possibly find it's reasonable to have a combination of both of these, using some one place and some another.

由于你的应用有很多不同的部分,你可能也会发现把这两个部分结合在一起是合理的,使用不同的地方。

#2


3  

I prefere Static classes that hold your objects. So the amount of objects you can access is restircted by the interface your static class offers. Static is not bad as long as your application still scales.

我喜欢包含对象的静态类。因此,可以访问的对象数量由静态类提供的接口重新激发。只要您的应用程序还在扩展,静态也不是坏事。

Another good alternative to singletons is the Monostate pattern, where you have a class that implements private static fields to represent "singleton" behavior.

单例的另一种很好的替代方法是单状态模式,其中有一个实现私有静态字段来表示“单例”行为的类。

See:
Monostate
Monostate vs. Singleton

参见:单态单态和单态

UPDATE: It often helps me to keep a REST like api in mind, even for internal program structures. Having one class that is updated from everywhere and sends notices to everybody is hard to control in respect to raise conditions and infinity loops (Update -> Event -> Update -> ...)

更新:它经常帮助我记住api,即使是内部程序结构。对于提高条件和无限循环(Update -> Event -> Update ->…)来说,拥有一个从各处更新并向每个人发送通知的类是很难控制的。

Build an (static or not) Status bar interface that you can access where you need it. Through a Static class where you get access to your Status bar interface or by dependency injection if you use such techniques (not recommended for smaller projects). Every call to your status bar interface has to be independent from any events that might be raised by the Status bar to avoid further issues with raise conditions. Think of the status bar interface like a website that can be called from other parts of the program to push and pull information.

构建一个状态栏接口,您可以在需要时访问它。通过静态类,您可以访问状态栏接口或依赖注入,如果您使用这些技术(不推荐用于较小的项目)。对状态栏接口的每次调用都必须独立于状态栏可能引发的任何事件,以避免引发条件的进一步问题。想象一下状态栏界面,就像一个网站,可以从程序的其他部分调用,以推动和拉动信息。

#3


1  

You could simply use the Observer pattern and add the StatusBar as a listener to your 20 objects. This will eliminate the singletons and better follow SRP and DIP, but you will have to consider whether it is worth the effort. A singleton may be better if the indirection adds too much complexity and dependency injection is not possible.

您可以简单地使用Observer模式,并将StatusBar添加为您的20个对象的侦听器。这将消除单例,并更好地遵循SRP和DIP,但是您必须考虑这样做是否值得。如果单向添加过多的复杂性和依赖注入是不可能的,那么单例可能会更好。

public class StatusBar implements StatusListener {
}

public interface StatusListener {
   public statusChanged(String newStatus)
}

#4


0  

Classes will depend implicitly on any use singleton and explicitly to any parameters in the constructor. I would suggest adding an interface to the singleton, so just the methods needed would be exposed to the classes using the IStatusBar. This is more code, but will ease unit testing.

类将隐式地依赖于任何单例使用,并显式地依赖于构造函数中的任何参数。我建议向singleton添加一个接口,这样只需使用IStatusBar将所需的方法公开给类。这是更多的代码,但是可以简化单元测试。

#5


-1  

It's hard to give advice without knowing more of your application's architecture, but perhaps you should consider dependency injection. For example, pass a StatusBar instance to the constructor of each class that directly uses it.

如果不了解应用程序的架构,就很难给出建议,但也许您应该考虑依赖项注入。例如,将StatusBar实例传递给直接使用它的每个类的构造函数。


推荐阅读
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • iOS Swift中如何实现自动登录?
    本文介绍了在iOS Swift中如何实现自动登录的方法,包括使用故事板、SWRevealViewController等技术,以及解决用户注销后重新登录自动跳转到主页的问题。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 在C#中,使用关键字abstract来定义抽象类和抽象方法。抽象类是一种不能被实例化的类,它只提供部分实现,但可以被其他类继承并创建实例。抽象类可以用于类、方法、属性、索引器和事件。在一个类声明中使用abstract表示该类倾向于作为其他类的基类成员被标识为抽象,或者被包含在一个抽象类中,必须由其派生类实现。本文介绍了C#中抽象类和抽象方法的基础知识,并提供了一个示例代码。 ... [详细]
  • 本博文基于《Amalgamationofproteinsequence,structureandtextualinformationforimprovingprote ... [详细]
  • Thisworkcameoutofthediscussioninhttps://github.com/typesafehub/config/issues/272 ... [详细]
  • [转载]从零开始学习OpenGL ES之四 – 光效
    继续我们的iPhoneOpenGLES之旅,我们将讨论光效。目前,我们没有加入任何光效。幸运的是,OpenGL在没有设置光效的情况下仍然可 ... [详细]
  • Non-ASCIIhelponitsownisOK: ... [详细]
author-avatar
米米丫头2502860283
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有