### Home Classes Methods

### In Files

- lib/facets/multiton.rb

### Namespace

- MODULE Multiton::Inclusive
- MODULE Multiton::MetaMethods

### Methods

- #_dump
- #clone
- #dup

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Random
- Random::ArrayExtensions
- Random::HashExtensions
- Random::IntegerExtensions
- Random::NumericExtensions
- Random::RangeExtensions
- Random::StringExtensions
- Random::StringExtensions::Self
- FileUtils
- FileUtils::DryRun
- FileUtils::NoWrite
- FileUtils::Verbose
- Digest
- Digest::Class
- Digest::Instance
- Multiton
- Multiton::Inclusive
- Multiton::MetaMethods
- URI
- URI::Hash
- URI::Kernel
- Argvector
- Argvector
- ERB
- ERB::OpenTemplate
- Enumerable
- Enumerable::Arguments
- GetoptLong
- GetoptLong::DSL
- Memoizable
- Memoizable::Copy
- Numeric
- Numeric::Multipliers
- Object
- Object::ArgVector
- Timer
- Timer::Dummy
- Array
- BasicObject
- CGI
- Cloneable
- Continuation
- Date
- DateTime
- Equitable
- File
- Find
- Hash
- Instantiable
- Integer
- Interval
- Kernel
- Math
- Module
- Net
- NilClass
- NullClass
- OpEsc
- OpenStruct
- Pathname
- Platform
- Range
- RbConfig
- Set
- Shellwords
- String
- StringScanner
- Time
- Tuple
- YAML
- Zlib

No matching classes.

# Multiton

Multiton design pattern ensures only one object is allocated for a given state.

The 'multiton' pattern is similar to a singleton, but instead of only one instance, there are several similar instances. It is useful when you want to avoid constructing objects many times because of some huge expense (connecting to a database for example), require a set of similar but not identical objects, and cannot easily control how many times a contructor may be called.

    class SomeMultitonClass
      include Multiton
      attr :arg
      def initialize(arg)
        @arg = arg
      end
    end

    a = SomeMultitonClass.new(4)
    b = SomeMultitonClass.new(4)   # a and b are same object
    c = SomeMultitonClass.new(2)   # c is a different object

## How It Works

A pool of objects is searched for a previously cached object, if one is not found we construct one and cache it in the pool based on class and the args given to the contructor.

A limitation of this approach is that it is impossible to detect if different blocks were given to a contructor (if it takes a block). So it is the constructor arguments only which determine the uniqueness of an object. To workaround this, define the class method ::multiton_id.

    def SomeMultitonClass.multiton_id(*args, &block)
      # ...
    end

Which should return a hash key used to identify the object being constructed as (not) unique.

### Public Instance Methods

clone() click to toggle source

Disable build-in copying methods.

    # File lib/facets/multiton.rb, line 135
    def clone
      raise TypeError, "can't clone Multiton #{self}"
      ##self
    end

dup() click to toggle source

    # File lib/facets/multiton.rb, line 140
    def dup
      raise TypeError, "can't dup Multiton #{self}"
      ##self
    end

### Protected Instance Methods

_dump(depth=-1) click to toggle source

Default marshalling strategy.

    # File lib/facets/multiton.rb, line 149
    def _dump(depth=-1)
      Marshal.dump(@multiton_initializer)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.