From: Bob Sidebotham Date: 2004-10-02T11:19:59+09:00 Subject: Re: Can you rewrite this in a better way? Robert Klemme wrote: > One thing I find irritating about your approach: you define fields on > class level but query them on instance level. As long as "fields" is > just a list of symbols I'd prefer to deal with them solely on class > level. Because that's where it belongs. > > If OTOH you want this mechanism to do something like attr_accessor (i.e. > define something that affects instances' state) then you will access > them on instance level but the query ("which fields are there?") still > belongs to the class IMHO. > > Note also that you may want to freeze your fields array especially when > accessing it via instances because clients still can modify the array > arbitrarily. > > Kind regards > > robert > Thanks for the observation. You may be right that keeping levels separate would be a good idea. Still, if I hand you an object, and tell you that I want you to do something with a set of items that are somehow associated with the object (which in this example, I've called "fields"), and if I tell you that you can get a list of those fields by calling some particular function, why should I ALSO have to tell you HOW to call the function (i.e. foo.fields vs. foo.class.fields). Doesn't this break encapsulation by forcing the caller to know too much about the internals of the receiver? In this case, I think it's a minor point, anyway: the two classes are very closely related and not intended to work independently. I think part of the issue is that the right answer to a question like this perhaps depends upon the application. So without knowing more about the application, and the context within which all this happens, I'm not sure you can say that one pattern is necessarily better than the other. Bob