创建许多新实例还是重用它们?

发布于 2024-08-30 10:15:38 字数 295 浏览 7 评论 0原文

我在 VB.NET Windows 窗体应用程序中有多个业务实体。现在,它们在应用程序启动时实例化并在需要时使用。它们保存业务实体以及存储和检索数据的方法的描述。长话短说,它们是一些需要构建的重对象(它们有一些内部字典和对其他对象的引用),创建并保存在一个名为“BLogic”的大全局变量中。

我应该重构它,以便每个对象在需要时创建并在超出范围时释放吗?那么 UI 上的每个事件都可能会创建一些这样的对象。

我应该努力最小化新对象的创建还是最小化静态和全局对象的数量?一般来说,我试图最小化每个变量的范围,但是我应该特别对待这个业务逻辑对象吗?

I have multiple business entities in VB.NET Windows Forms application. Right now they are instanced on application startup and used when needed. They hold descriptions of business entities and methods for storing and retrieving data. To cut the long story short, they are somewhat heavy objects to construct (they have some internal dictionaries and references to other objects) created and held in one big global variable called "BLogic".

Should I refactor this so that each object is created when needed and released when out of scope? Then every event on UI will probably create a few of this objects.

Should I strive to minimize creation of new objects or to minimize number of static and global objects? Generally I am trying to minimize the scope of every variable but should I treat this business logic objects specially?

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

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

发布评论

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

评论(2

冷了相思 2024-09-06 10:15:38

让我们看看您提供的两个选项:

单一全局实例

优点:

  • 每种方法的性能成本较低,因为对象已经创建
  • 无需弄清楚如何传递数据

缺点:

  • 有可能对象被创建但从未被使用。这既浪费内存,又浪费性能。这可以通过使用延迟初始化来缓解,延迟初始化将在第一次使用对象时创建对象,但仍保留单个全局创建的实例。
  • 多线程问题,例如确保数据一致性(潜在的性能和开发成本)

每个函数实例都是唯一的

优点:

  • 每个函数都有自己的副本,不必担心其他函数可能同时在做什么。
  • 如果对象没有不断地创建和销毁,则程序的总内存使用量可能会较低。

缺点:

  • 根据创建对象所需的时间,您可能会在 UI 中遇到响应问题。
  • 根据对象的创建方式,您可能必须等待另一个函数完成对象的创建,然后才能创建副本。

这并不是一个详尽的列表,但它确实指出了我所看到的主要权衡。显然,您更了解情况以及哪些权衡是可以接受的。一些明智选择的全局变量可能很有用,但像大多数人一样,我会倾向于避免使用大量全局变量,除非它们代表只能存在其中之一的东西,例如串行端口,或者整个应用程序应该只具有其中之一,就像一个 ApplicationSettings 类。

不要低估您的时间(无论是现在还是稍后您回来进行维护时)作为成本。有时“更好”的解决方案实际上可能更糟糕,因为实施时间太长。通常,“足够好”最终证明是,嗯,足够好。

Let's look at the two options you've presented:

Single Global Instances

Pros:

  • Less performance cost in each method because the objects are already created
  • No need to figure out how to pass the data around

Cons:

  • Chance that objects are created but never used. This is a waste of both memory and performance. This could be mitigated by using lazy initialization that will create the object the first time it is used but still keeping a single globally created instance.
  • Multithreading concerns such as ensuring data consistency (potential performance and development costs)

Unique per function instances

Pros:

  • Each function gets it's own copy and does not have to worry about what other functions may be doing at the same time.
  • If the objects are not being constantly created and destroyed, you may have a lower total memory usage for your program.

Cons:

  • Depending on how long it takes to create the object, you could have responsiveness issues in the UI.
  • Depending on how the object is created, you may have to wait for another function to finish creating the object before you can create your copy.

This is not meant to be an exhaustive list, but it does point out the main tradeoffs as I can see them. Obviously, you know more about the situation and which tradeoffs are acceptable. A few judiciously chosen globals can be useful, but like most people I would lean away from having a lot of globals unless they represent something that there can only be one of, like a SerialPort, or something that the entire application should only have one of, like an ApplicationSettings class.

Don't undervalue your time (both now and later when you come back for maintenance) as a cost. Sometimes a "better" solution may actually be worse because it takes too long to implement. Often "good enough" turns out to be, well, good enough.

_失温 2024-09-06 10:15:38

是的,您应该重构为仅在需要时分配对象,并在不再需要时将其丢弃。这始终是一个不错的设计选择,除非无法满足特定且可测量的性能要求。

将分配推迟到绝对需要的时候的优点是,在许多(大多数?)情况下,对象根本不会被分配。拖延是要付出代价的! ;>您的应用程序运行得更精简,整个系统应该负担更少、更敏捷。没有人喜欢使用内存消耗者。

按需分配的缺点是它可能会导致用户反馈/响应延迟,这会令人恼火。如果您有一个对象需要花费相当多的时间来构造或初始化(例如,通过网络加载数据),那么如果用户期望,那么该对象可能不太适合在按钮单击时分配就是点击按钮后立即看到结果。如果您可以立即调出用户界面来响应按钮单击,但新表单上的控件会尽快从网络数据中填充自己(并表明它们正在加载某些内容),那么这就不是什么问题了。 “相当长的时间”的一般 UI 指标通常是点击和 UI 响应之间最多大约半秒。

Yes, you should refactor to allocate the objects only when needed, and dispose of them when they are no longer needed. This is always a good design choice unless trumped by specific and measurable performance requirements.

The advantage of deferring allocation until absolutely needed is that in many (most?) cases, the object will never be allocated at all. Procrastination pays! ;> Your app runs leaner and the system as a whole should be less taxed and snappier. Nobody enjoys using a memory hog.

The disadvantage of allocate on demand is that it may introduce delays in user feedback/responsiveness that will be irritating. If you have an object that takes a considerable amount of time to construct or initialize (say, loading data across the network), that would probably not be a great match for allocate on button click if the user expectation is to see results immediately after the button click. If you can bring up UI immediately in response to the button click but have controls on the new form fill themselves in from network data as soon as they can (and indicate that they are loading something), that would be less of an issue. The general UI metric for "considerable amount of time" is usually about half a second max between click and UI response.

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