Smalltalk Best Practice Patterns: Constructor Method

Sadly reading is going slower than expected due to being so busy with various things in life. Oh well, just a single pattern today.

Constructor Method

How do you represent instantiation?

In addition to a vanilla constructor, add methods for common cases to instantiate typical objects. For strange cases allow the use of accessors.

Using Perl (with Moose) an example might be:

package Point;

use Moose;

has x => (is => 'ro');
has y => (is => 'ro');

sub r_theta {
  my ($class, $r, $theta) = @_;

    x => $r * cos($theta),
    y => $r * sin($theta),


So now both of the following work:

my $p = Point->new(5, 6);
my $v = Point->r_theta(10, 1.4);
Posted Thu, Sep 1, 2011

If you're interested in being notified when new posts are published, you can subscribe here; you'll get an email once a week at the most.