Re: Is encapsulation deprecated in ObjC?
Re: Is encapsulation deprecated in ObjC?
- Subject: Re: Is encapsulation deprecated in ObjC?
- From: "Erik M. Buck" <email@hidden>
- Date: Thu, 16 Aug 2001 12:47:43 -0500
Sometimes the purpose of a class is to store a value. The class would be
useless if there was no "set" accessor to set the value.
Having a "set" accessor does not violate encapsulation. Directly setting an
instance variable via structure de-reference does violate encapsulation. A
"set" accessor perfectly preserves encapsulation. In the extreme case, a
class could be modified so that it no longer contains an instance variable,
and the corresponding "set" accessor could be modified to store the value
some other way (a dictionary, on disk, whatever). The code that uses the
class via the "set" accessor does not need to change. The "set" accessor
enhanced (created) the encapsulation that enabled the complete
re-implementation of a class without changes to code that uses the class.
A Class that lets outsiders arbitrarily change its internal state is a bad
idea, unless the sole purpose of the class is to store an arbitrary state.