在属性调用期间填充属性对象

发布于 2024-07-09 14:56:40 字数 316 浏览 6 评论 0原文

我想知道这种方法是否正确,或者它们是否是更好的方法。

我基本上有一个 Person 类,它有许多其他类作为变量,每个自定义类都是通过传递 Person ID 来实例化的,然后该类使用该 ID 检索它需要的数据。 我通过属性公开变量类。

目前,我在创建 Person 类的实例时实例化变量类,大量变量类意味着实例化 Person 类所需的时间不断增长。 我的想法是将变量类的实例化移动到 Propertie 声明中,然后在此处使用 If 语句来实例化它(如果尚未完成)。

正如我上面所说,这种方法是正确的还是他们是更好的方法?

谢谢

I'd like to know whether this approach is correct or if their are better ways of doing this.

I have what is basically a Person class which has a number of other classes as variables, each of the custom classes is instantiated by passing the Person ID and then that class retrieves the data it needs using that ID. I expose the variable classes via Properties.

Currently I am instancing the variable classes when I create an instance of the Person class, and the large number of these mean that the time it takes to instantiate the Person class is growing. My idea was to move the instancing of the variable classes to the Propertie declaration and then using an If statement here to instantiate it if it hasn't yet been done.

As I said above is this approach correct or is their a better way of doing this?

Thanks

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

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

发布评论

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

评论(2

站稳脚跟 2024-07-16 14:56:40

您所描述的技术有一个术语; 它被称为“延迟加载属性”。 它绝对应该有助于分散该对象上的负载,远离“前端加载”构造函数。

另一方面,听起来您所描述的内容将导致一个非常紧密耦合的对象模型(如果您还没有一个),这可能会对代码的可维护性产生负面影响。 然而,我认为关于该主题以及如何工作的严肃论文确实不在这个问题的范围内。

There is a term for the technique you're describing; it's called "lazy-loaded properties". It should definitely help spread out your load on this object away from a "front-loaded" constructor.

On a different note, it sounds like what you're describing is going to result in a terribly tightly-coupled object model (if you don't have one already) which is likely to have a negative impact on this code's maintainability. However, I don't think that a serious dissertation on that topic and how to work otherwise is really within the scope of this question.

世态炎凉 2024-07-16 14:56:40

只是为了澄清:如果您的意思是在其访问器的 getter 上实例化类,那么是的,这是一个很好的方法 - 称为延迟加载。

例如

public Property ChildClass as PersonChildClass

   Get

      if _childClass is Nothing          
          _childClass = new PersonChildClass(_personId)
      End If
      return _childClass
  End Get
End Property

Just to clarify: If you mean instantiating the classes on the getter of their accessor then yes this is a fine approach - referred to as Lazy Loading.

Eg

public Property ChildClass as PersonChildClass

   Get

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