什么时候可以在属性getter访问中更改对象状态(例如初始化)? [英] When is it ok to change object state (for instance initialization) on property getter access?

查看:100
本文介绍了什么时候可以在属性getter访问中更改对象状态(例如初始化)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(除了代理设置!)

我花了一些时间在这里写一个问题,以解决我遇到的问题的更好模式-该类对几乎每个属性getter都执行了一些条件初始化,因为基类中的初始化依赖于继承类的数据,在建筑上不可用.

I spent some time writing a question here regarding a better pattern for a problem I had - of a class that performed some conditional initialization on almost every property getter, since the initialization in the base class relied on data from the inheriting classes that wasn't available on construction.

在写我得出结论的问题时,最好在继承者构造上进行初始化.这将要求每个继承的类都调用父级初始化方法,但我认为这更好,因为:

While writing the question I came to the conclusion it would be better practice to initialize on inheritor construction. This would require every inheriting class to call the parents initialization method, but I think it's better, because:

  1. 我不必记住在每个新的属性getter/setter的基类中进行初始化.
  2. 我在调试时不会意外触发初始化(在此处查看我的问题)

如果您曾经使用过代码来更改属性获取器中的状态,那么您认为它绝对合理吗?你能举一个例子吗? (甚至描述模式?)

If you ever had code that changes state in a property getter, do you think it's absolutely justified? Can you give an example for such a case? (Or even describe the pattern?)

我只能想到代理访问,在属性访问之前,您不希望执行初始化...

I could only think of proxy access, where you don't want to perform initialization until property access...

有人建议我使用工厂/静态方法进行初始化-这实际上是一个好主意(当构造简单时,在继承类之间不统一时会更加困难),但是答案被删除了,然后才有机会提交我的回复.太糟糕了.

Somebody suggested that I initialize using a factory/static method - that's actually a good idea (when the construction is simple, a bit harder when it's not uniform across inheriting classes), but the answer was deleted before I had a chance to submit my reply. too bad.

推荐答案

惰性缓存.在不访问属性之前,您不从数据库中加载数据的位置. (不知道这是否是代理访问的意思).

Lazy caching. Where you dont load the data from the database until the property is accessed. (Not sure if this is what you mean by proxy access).

但是,我真的不会认为这在逻辑上改变了对象的状态,因为在访问之前和之后类的行为保持不变.数据始终隐式存在.逻辑状态保持不变.

However, I wouldnt really consider this to be logically changing the state of the object as the behaviour of the class remains the same before and after access. The data is implicitly there at all times. The logical state remains unchanged.

我永远不会通过吸气剂改变类的逻辑状态,因为它是反直观的并且在逻辑上是不正确的.您确实要承担各种意想不到的后果.

I would never change the logical state of a class through a getter as it is counter intuitive and logically incorrect. You do risk all sorts of unintended consequences..

您可以执行以下操作:

    public class baseone
    {
        private baseone ()
        {
        }

        public baseone ( string huh )
        {
                initialise(huh);
        }

            protected abstract initialise(string stuff); 
    }


    public class niceone : baseone
    {
        public niceone (string param)
         : base(param)
        {

        }

            protected override initialise(string stuff)
            {
               // do stuff..
            }
    }

将基类的默认构造函数设为私有可确保必须传递必需的参数以初始化该类.

Making the default constructor of the base class private ensures that the required parameters must be passed to initialise the class.

这篇关于什么时候可以在属性getter访问中更改对象状态(例如初始化)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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