Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Flyweights - different meaning in perl?

by BrowserUk (Pope)
on Dec 15, 2002 at 15:48 UTC ( #220010=note: print w/ replies, xml ) Need Help??


in reply to Flyweights - different meaning in perl?
in thread Yet Another Perl Object Model (Inside Out Objects)

I've studiously avoided the term "flyweight objects" as I have only ever seen it here and didn't really understand its meaning. I'll stick to "Inside-out" which only has meaning in perl terms as far as I am aware.

With regard to the example you describe, that may have made some sense in the world of 8-bit chars as you only need a pool of 256 object, but in the world of UCS, it becomes excessive I think.

Actually, I say only 256 objects, but even that doesn't make any sense to me. It might make sense in a pure OO, conceptual sense to have a seperate object for each char, but it my view its a very strange thing to do.

There are to my way of thinking only two things you can do with a char.

  • Use it. Ie. give it to some other object as a parameter.
    Eg. somestring.append('A'); or somestring.append( somechar );
  • Or ask it for its ordinal value.
    Eg. 'A'.ord(); or somechar.ord();

So even if you want to represent it at the language level as an object, in terms of what the compiler or interpreter would actually manipulate, it's just a number.

Unless I'm missing something, which is quite possible because I often do, the idea that the compiler/interpreter would manipulate a pointer to an object or worse, a handle to an object at the binary level is simple crazy?

Can you see a reason for having an object to represent each char? I mean, if your are going to allow attributes to be applied to individual chars (color, size, whatever), then pooling the objects isn't going to work because I wouldn't be able to have a red 'A' and a green 'A' at the same time.

I can see the need for being able to maintain and manipulate attributes for aggragations of chars. This string is ascii, that sting is EBCDIC, but doing this at the char level does make any sense (to me). If you could do it, then you could create aggregate strings of mixed ascii and EBCDIC and then what happens when you want to compare string 'T{ascii}h{EBCDIC}e{ascii}' with 'T{EBCDIC}h{ascii}e{EBCDIC}'--do they match or not?


Examine what is said, not who speaks.


Comment on Re: Flyweights - different meaning in perl?
Re^2: Flyweights - different meaning in perl?
by adrianh (Chancellor) on Dec 15, 2002 at 17:22 UTC
    I've studiously avoided the term "flyweight objects" as I have only ever seen it here and didn't really understand its meaning. I'll stick to "Inside-out" which only has meaning in perl terms as far as I am aware.

    Fair enough, but other people have - which makes me suspect it means something different in the perl and design pattern worlds.

    Which would be annoying. One of the main advantages of the design pattern movement for me is the development of a common vocabularly (finally, everybody knows what a singleton is :-). If it means something different in the perl world it would be useful to know.

    With regard to the example you describe, that may have made some sense in the world of 8-bit chars as you only need a pool of 256 object, but in the world of UCS, it becomes excessive I think...

    I don't really want to repeat everything in the chapter from the book, but basically...

    • The context is a word processor, so "character" doesn't just mean the code, it might support methods like draw(), intersects(), etc. We're talking WYSIWYG layout support.
    • I didn't mean to imply you had to create the whole pool of characters at once, you would create them as needed for a document and add them to the pool.

    As for practicality, the example was in the book was adapted from the Doc editor, produced using InterViews 3.0. Take a look at:

    • Paul R. Calder and Mark A Linton. Glyphs: Flyweight bjects for user interfaces. In ACM User Interface Software Technologies Conference, pages 92-101, Snowbird, UT, October 1990.
    • Paul R. Calder and Mark A Linton. The object-oriented implementation of a document editor. In Object-Oriented Programming Systems, Languages and Applications Conference Proceedings, pages 154-165m Vancouver, British Columbia, Canada, October 1992. ACM Press.

    To quote from the Design Patterns chapter on flyweights:

    Doc uses glyph objects to represent each character in the document. The editor builds one Glyph instance for each character in a particular style (which defines its graphical attributes); hence a character's intrinsic state consists of the character code and its style information (an index into a style table). This means only position is extrinsic making Doc fast.

    If you're interested in the details I'd recommend picking up a copy of Design Patterns and giving the flyweight chapter a read.

      If you'd talked about Gylph objects instead of 'character' objects, I wouldn't have had to sit here and get all confused and indignant:)

      Doc uses glyph objects to represent each character in the document. The editor builds one Glyph instance for each character in a particular style (which defines its graphical attributes); hence a character's intrinsic state consists of the character code and its style information (an index into a style table). This means only position is extrinsic making Doc fast.

      Ah! They mean Fonts! How original.


      Examine what is said, not who speaks.

        I do think you got lost in the specifics of the example and failed to see the forest for the trees there. The point, as far as I understood, was that when you have to handle a huge number of tiny objects of the same (ancestral) class, whatever they may represent, and you can split the intrinsic and extrinsic states, then you do so and create a pool of object instances to refer to for the representation of a particular "thing"'s intrinsic state. That's a "flyweight object" - a class that contains only intrinsic state in order for its instances to be usable in multitude.

        Makeshifts last the longest.

        I've studiously avoided the term "flyweight objects" as I have only ever seen it here and didn't really understand its meaning. I'll stick to "Inside-out" which only has meaning in perl terms as far as I am aware.
      Fair enough, but other people have - which makes me suspect it means something different in the perl and design pattern worlds.

      Which would be annoying. One of the main advantages of the design pattern movement for me is the development of a common vocabularly (finally, everybody knows what a singleton is :-). If it means something different in the perl world it would be useful to know.

      Well following this thread, and diotalevis comment I did a little research into the flyweight pattern. And it would seem that there are two different ideas being discussed. Its possible (and I think likely) that the concepts were originally the same, but I suspect the design pattern movement redefined and extended the concept from its earlier use. But i have little evidence to back up this hunch. Heres a quote from a design pattern site about the design pattern schools concept of a flyweight pattern

        If instances of a class that contain the same information can be used interchangeably, the Flyweight pattern allows a program to avoid the expense of multiple instances that contain the same information by sharing one instance.
        Overview of Design Patterns

      however this concept seems to be different from that briefly mentioned elsewhere and more importantly discussed in TheDamians book Object Oriented Perl. I think the later is the source of many peoples concept of the flyweight pattern in Perl, and probably derives from the earlier usage of the term "flyweght".

        Flyweight objects are most frequently used in OO languages that pass objects around by value because flyweight objects objects remain extremely small (no matter how much data they contain). Hence they are cheap to pass around. Because Perl Objects are invariably accessed via references, this advantage is not signifigant.

        However, the flyweight pattern still has something to offer in Perl, because it provides a simple mechanism for preventing direct access to abject attributes, thereby enforcing encapsulation. As a bonus, it also provides a means of easily keeping track of every object in a class, something closure-based encapsulation doesnt provide.

        Damian Conway -- Section 11.3 Object Oriented Perl (Manning)

      He then goes on to frame an object model that he calls the Flyweight Pattern. The idea of this is simple. Instead of storing attribute data in the object directly an object only holds a key into a lexically held hash that holds all of the objects for the class. The accessors extract this key from the object and then use it to look up the real object from the privately held table. In his example he uses soldiers and their ids, with a reference to the ID being blessed. (He points out that this has the weakness that it provides a means to violate encapsulation as an object can magicaly "transform" itself into using a different set of attributes. He discusses various approaches to making this less likely or impossible.)

      Now the pattern he describes is still compliant with the both usgaes of "flyweight". Two distinct objects can share attributes, furthermore the actually item that represents the object is "small" from an outside POV, even if the fact that its blessed is ignored.

      So I think this is a stuation that is similar to the term "closure" in perl. Generally speaking closures in Perlspeak are the same thing as closures in CSspeak. However all too often you see the term used as a synonym for "anonymous subroutine". In fact I would say that most non CS types would volunteer this is a defintion for a closure (assuming they knew what one was at all.) And just as much as this usage is a distortion and oversimplification of the real concept of "closure", I think that the term flywieght pattern has come to reflect primarily the idea of indexing a class specific lexically held register of objects, and not so much the idea of sharing attribute data between those objects. So from that point of view I would say its obvious that InsideOut objects fits that pattern. But....

      And this is where I take the other point of view, and try to answer diotalevis question

        I don't have my copy of _Design_Patterns_ handy but isn't this just a Flyweight implementation?
      At first glance the flyweight object design as described by TheDamian is very similar to Abigail-IIs inside out objects. The fact that inside out objects use a seperate hash for each attribute is a minor twist that doesnt distract much from the flyweight pattern. However the use of the references ID or some other reference/object specific unique identifier as the key into the attribute table(s) does in my opinion mean that it is not a flyweight implementation. Two distinct objects have in principle completely unique attribute representation, the objects do not by design share attributes. (Excepting that they can reference shared data if they so choose, but this is true of all object representations in Perl).

      Furthermore, Abigail-IIs pattern means that we can wrap an arbitrary number of InsideOut subclasses around any class/object. InsideOut objects fundamentally don't care about what item their blessed reference points at. However the flyweight pattern the data is small for sure, but it is very relevent to the objects internal state. You couldn't just wrap the Soldier class in TheDamians book around an IO::File object for instance. You could however do so with his BaseballPlayer example.

      So to sum up, the flyweight pattern in perl seems to focus on aspects that design pattern enthusiasts will see as inconsequential, but it still shares the same underlying ideas. InisdeOut objects are in my opinion not flyweight objects from the design pattern point of view, but have similar enough perl implementations that they are easily mistaken as being the same.

      Updated: Minor typographical errors have been corrected.

      --- demerphq
      my friends call me, usually because I'm late....

        Well following this thread, and diotalevis comment I did a little research into the flyweight pattern.

        Thanks for the info! It's been so long since I read TheDamian's book I'd completely forgotten this. (You've also made me realise that somebody has nicked my copy since it's not on my bookshelf... grrr... darn sneaky coworkers...).

        Fortunately the relevant chapter is online.

        Its possible (and I think likely) that the concepts were originally the same, but I suspect the design pattern movement redefined and extended the concept from its earlier use.

        It looks like it was the other way around. TheDamian references Design Patterns in his book.

        Personally, I don't think the technique described is an instance of a flyweight pattern, since we're taking not taking the state information from the class, just rearranging its location within the class.

        The whole point of using flyweights is to cut down on the number of objects you create by removing the context-dependant information from the class. The context specific state being passed to the flyweight objects as and when necessary by its client objects.

        TheDamian's technique might use some of the same stratagies as flyweights, but with a different intent.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-07-25 10:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (170 votes), past polls