English 中文(简体)
When is it ok to change object state (for instance initialization) on property getter access?
原标题:

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

最佳回答

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.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签