静态VS在主窗口实例成员 [英] Static vs Instance members in MainWindow

查看:127
本文介绍了静态VS在主窗口实例成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解静态和实例的概念,但我很困惑,当我有一个类,其中只有1个实例将永远存在,这是一种被称为在我的应用程序的启动实例,我应该使用(= Application.Current .MainWindow)

I understand the concept of Static and Instance but I am confused which I should use when I have a class of which only 1 instance will ever exist which is the instance which is called at the start of my application (=Application.Current.MainWindow)

我要保持我需要thoughout不同类别在我的计划全时段的列表。
我应该让静态的,因为那里永远不会存在主窗口的两个实例?或者我应该让非静态的,因为它听起来是正确的说,它属于实例?

I want to keep a list which I need thoughout different classes the whole time in my program. Should I make it static because there won't ever exist 2 instances of MainWindow? Or should I make it non-static since it sounds right saying that it belongs to the instance?

如果我选择让非静态这也将意味着我总是需要提到它在其他类以(主窗口)Application.Current.MainWindow,这是烦人

If I choose to make it non-static this will also mean that I always need to reference to it in other classes with "(MainWindow)Application.Current.MainWindow" which is annoying

难道是坏,使静态列表在这里?

Would it be "bad" to make the list static here?

推荐答案

如果您使用的实例模式,它铺平了道路为的依赖注入和的模块发现时,这又便于使用该服务的组件的单元测试。

If you use an instance pattern, it paves the way for Dependency Injection and Module Discovery, which in turn facilitates unit testing of the components that use the service.

该实例模式还提供了后期绑定,如果类需要的东西,不知道直到运行时它可以是非常有用的。最后,该实例模式使可变性。

The instance pattern also provides for late binding, which can be very useful if the class needs something that is not known until run time. Lastly, the instance pattern enables 'mutability'.

如果您的应用程序是建立了由各种服务的交互,那么静态模式可能更合适。两者

If your application is built up by the interaction of various services, then a static pattern may be more appropriate.

性能和内存占用量的差异可以忽略不计。

Performance and memory footprint differences between the two are negligible.

这篇关于静态VS在主窗口实例成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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