Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Encapsulation through stringification - a variation on flyweight objects

by adrianh (Chancellor)
on Apr 02, 2003 at 10:19 UTC ( #247459=note: print w/ replies, xml ) Need Help??


in reply to Re: Encapsulation through stringification - a variation on flyweight objects
in thread Encapsulation through stringification - a variation on flyweight objects

Personally, I think that going for this kind of hard encapsulation is self-defeating. The whole point of OOP is to decouple your code. If your colleague is violating this decoupling, then it is incumbent upon you to educate your colleague.

We've had this discussion before but I just can't let it go :-)

I agree when the violation is deliberate. The problem with the "normal" perl OO model is that you can interfere accidentally with "private" methods and attribute.

Even if they completely stick to the documented API they can be bitten if there is a change to the private implementation if it is done in the naive perl OO style.

The good news is that, in my experience, it doesn't happen very often. However, I'm still very much looking forward to being able to get around this nicely when perl6 hits the streets.


Comment on Re^2: Encapsulation through stringification - a variation on flyweight objects
Re3: Encapsulation through stringification - a variation on flyweight objects
by dragonchild (Archbishop) on Apr 02, 2003 at 15:26 UTC
    The problem with the "normal" perl OO model is that you can interfere accidentally with "private" methods and attribute.

    This kind of assertion demands an example. I humbly await your response.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      package Foo; sub new {return bless {} => shift} sub oogle {my $shelf = shift; $self -> {key} = shift} package Bar; our @ISA = qw /Foo/; sub gurgle {my $self = shift; $self -> {key} = shift;}

      Both Bar and Foo happen to use the same key, because there's no encapsulation.

      Perl na(t)ive OO model stinks, because you've to work hard to get encapsulation. (Note: I'm not saying I want private stuff, I just want encapsulation of the implementation). I do not know a common language whose OO model sucks more than Perls.

      Abigail

        Ok. I'm missing something when it comes to encapsulation. Why on earth would you want to prevent Bar from accessing attributes it inherited from Foo? That's always been something I didn't understand in C++ - why private attributes were not inherited ...

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Perhaps these examples demonstrate it, although German, they should be understandable. It's kind of a summary of that discussion some time ago.

      --
      http://fruiture.de

      This kind of assertion demands an example.

      I live for examples ;-)

      Bob writes a module to manage persistent objects:

      The documented API are the methods uuid, load, save & mark. The hash key _change is an implementation detail.

      Now, Mary's writing a module for representing the state of vending machines. She needs the information to persist, finds Persist on CPAN, and decides to use this as her base class.

      Now, despite sticking to the published API, Mary is going to have a nasty time debugging her code. She'll have to go and read the source for Persist to find out that Bob's implementation detail ($self->{_change} = boolean value indicating whether an object has changed) clashes with her implementation detail ($self->{_change} = object representing available change in the vending machine).

      Note this is not deliberate. Mary is not sneaking a look inside Bob's module and using the hash for her own nefarious means - it's an accident.

      You get exactly the same issue with "private" methods. What if Bob decides to extend Persist to only load the objects when needed. As part of this he adds a _loaded method that will return true when the object is fully loaded from backing store. Without changing the published API his new version of Persist will break when used with Mary's old VendingMachine module - because it clashes with Mary's _loaded method. Nasty.

      There are, of course, ways around these issues. You can use inside out objects to get around problems with attribute clashes. You can use alternate calling styles to get around problems with private methods. However these, and other techniques, all require a fair amount of discipline and expertise to use well.

      Good encapsulation is a pretty darn basic feature of an object oriented language. In my opinion its absence hurts perl. It makes it harder to write good modular reusable code. It makes it easier for people to score points in those pointless language flame fests. Good encapsulation should be a basic part of perl (and will be in perl6... huzzah!).

      Enough ranting :-) You might also find these exchanges on private attributes and private methods of interest.

        Let me get this straight: The basic concern with the lack of truly private attributes/methods has to do with the fact that you may only want to inherit the interface, not the object implementation. You want to know that "I am a Foo" and "Foos can bark", so "I can bark" and not have to think anymore about it. Plus, you want to know that by using Foo's capability to bark, you have not harmed your capability to develop any which way you want.

        Does that accurately sum it up?

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://247459]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2014-09-17 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (55 votes), past polls