[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: advice needed: OO style
- From: Emmanuel Oga <emmanueloga@...>
- Date: Wed, 27 Apr 2011 21:31:44 -0300
On Wed, Apr 27, 2011 at 6:50 PM, Lorenzo Donati
<lorenzodonatibz@interfree.it> wrote:
> On 27/04/2011 21.55, Javier Guerra Giraldez wrote:
>>
>> On Wed, Apr 27, 2011 at 2:29 PM, Lorenzo Donati
>> <lorenzodonatibz@interfree.it> wrote:
>>>
>>> - the actual object is stored in the proxy table using a "private" index
>>> (the metatable itself) to achieve encapsulation and prevent the client
>>> from
>>> tampering with the object directly.
>>
>> as i understand it, encapsulation means having all relevant state
>> within the same object. what you describe here is access control.
>
> Fair enough. I just was way too sloppy with terminology (and probably too
> much emphatic when speaking of "tampering") :-P
>
> I really meant "data hiding". As for access control, I don't care really
> much about it (I don't need sandboxing or to operate with strict security
> constraints). I just want to make sure (cheaply - without too much coding)
> that the client won't accidentally overwrite a field or a method of my
> objects and to ensure that objects are opaque, just to better support the
> abstraction they represent. I like clean interfaces!
IMO there is just too much overhead in using a proxy "just in case the
user modifies the values". It is different if you __need__ that proxy
to implement before/after callbacks for __every__ function stored in
the table (if you need to do that just for a few things, I would
rather use decorators). If you want something to be private, just use
a descriptive property name, like "_priv":
local MyObject_MT = {}
MyObject_MT.__index = MyObject_MT
local function MyObject(someoption)
local object = { _priv = {} }
object._priv.someoption = someoption
return setmetatable(object, MyObject_MT)
end
function MyObject_MT:DoSomething()
assert(self._priv)
local obj = self._priv
print( "this is object named: " .. obj.someoption )
end
-----------
local obj = MyObject( "foobar" )
obj:DoSomething()
Trying to catch all the bad things users can do with your library is
not really worth the effort: users are more clever than you would
think :-). Just let them know what's private and should not be
touched. If they want to shoot their foot, they will find a way.
>>
>> IMHO, encapsulation is great for clean code; access control is
>> overrated. (note that Python doesn't have it and they claim it to be
>> a 'clean', 'readable', language)
>>
> I agree on access control. As for Python, I used it for too short a time to
> make a meaningful comparison (never got to the point to learn its OO feature
> - when I was about to, I stumbled into Lua! :-D )
>>
>>> Note: I don't need inheritance support, since almost always use this
>>> pattern
>>> to represent objects of "concrete classes".
>>
>> hurray!
>>
>>
> :-D
>
>
> --
> Lorenzo
>
>