When is it ok to change object state (for instance initialization) on property getter access?

独自空忆成欢 提交于 2019-12-24 07:01:14

问题


(except for proxy setup!)

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. I don't have to remember to initialize in the base class on every new property getter/setter.
  2. I don't accidentally trigger the initialization while debugging (see my question here)

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.


回答1:


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..

You could do something like the following :

    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.



来源:https://stackoverflow.com/questions/1761696/when-is-it-ok-to-change-object-state-for-instance-initialization-on-property-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!