[Chicago-talk] Proper encapsulation

Jim Thomason thomasoniii at gmail.com
Sat Jan 22 11:54:38 PST 2005


It completely 100% utterly totally absolutely violates encapsulation.
Attributes should be defined within the class period, end of
discussion. Think about it - if you're defining attributes outside of
the class, then they're not really encapsulated inside anything, are
they? :-)

Now then, there are still some novel things that can be done with
defining attributes outside of the class, so I'm not going to
discourage them completely because there is reason to do it. I would
probably frown upon it, though, but I can be persuaded of its uses.
And hey, this is perl, we can play fast and loose with the rules.

But if you know what the attributes are going to be in advance (and
99.9% of the time you will) and you don't want to add new ones (and
99.9% of the tiem you won't), you should always define them in advance
inside the class.

-Jim....

On Sat, 22 Jan 2005 00:01:56 -0600, Jay Strauss <me at heyjay.com> wrote:
> Hi, I'm using Class::Accessor, it provides a method ->mk_accessors
> (which ironically creates setters and getters for your class).  Anyway,
> I think typically you'd invoke this from the class, but you can do it
> from the code that instatiates the class.
> 
> Like:
> 
> #!/usr/bin/perl
> 
> use 5.006;
> use strict;
> use warnings;
> 
> package SomeClass;
> 
> use base qw/Class::Accessor/;
> 
> package main;
> 
> SomeClass->mk_accessors(qw/cache/);
> 
> my $o = SomeClass->new;
> $o->cache("data");
> 
> print $o->cache,"\n";
> 
> My question is: does creating the accessors from outside the class
> violate OO principles, or is acceptable since the functionality is provided?
> 
> Thanks
> Jay
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>


More information about the Chicago-talk mailing list