InstanceFlyweightPattern

Last edit April 4, 2010
The InstanceFlyweightPattern is an extreme form of the FlyweightPattern.

  • Name: InstanceFlyweightPattern
  • Classification: I have no clue.
  • Motivation:
  • Applicability: Replacing value types with immutable reference types while saving memory.
  • Structure: ?
  • Participants: ?
  • Collaborations: ?
  • Consequences: Decrease on memory usage; however, some calculations might become harder because of the inability to directly manipulate new instances as an optimization.
  • Implementation: Memoize the 'new' method!
  • Sample Code:
    #!/usr/bin/env perl
    package Integer;
    { my %ints;
     sub new {
        my $class = shift;
        my $self;
        unless(exists($self = $ints{$_[0]})){
             $self = bless {'int' => $_[0]}, $class;
        }
        $self;
     }
    }

CategoryPattern