Subject: An OOP Pattern Discovered by Accident

From: Shlomi Fish <[email-masked]>
Date: Fri, 26 Jan 2007 18:15:10 +0200

After I implemented the Math::GrahamFunction code to calculate the Graham 
Function using Perl ( 
http://search.cpan.org/~shlomif/Math-GrahamFunction-0.01/ ), I decided to 
translate the code to Common Lisp in order to learn Lisp better. So I started 
translating the methods of the classes, one by one.

Now, I have an SqFacts class that manages squaring factors, which for the 
purpose of our discussion are an increasing series of natural numbers. 
Furthermore, there's a Dipole class that inherits from it and manages two 
squaring factors - a result and a composition. Now in my Dipole Perl code I 
had the following declaration for the factors method:

<<<<<<<<<
sub factors
{
    my $self = shift;

    return $self->result()->factors();
}

And besides it similar declarations for some other functions:

<<<<<<<<<<
sub is_square
{
    my $self = shift;
    return $self->result()->is_square();
}

sub exists
{
    my ($self, $factor) = @_;

    return $self->result()->exists($factor);
}

sub first
{
    my $self = shift;

    return $self->result()->first();
}

Now when I implemented it in my lisp code, I implemented the SqFacts class, 
and tested it and then starting implementing the Dipole class. The factors() 
method was one of the first I implemented, and then I wrote two tests for the 
is_square predicate method, for both true and false returns. Surprisingly, 
they both passed without even implementing is_sqaure.

As it turned out, is_square and friends for the SqFacts class, used the 
factors() method and since factors() was already implemented and called the 
factors of the "result" property, then in Dipole, they just did the same for 
the factors of the "result". So I didn't have to implement the rest of the 
methods.

Thinking about it, I realised one can consciously use this scheme by working 
on an accessor and then over-riding this accessor in the derived class to 
point to the accessor of the containing class.

So we can do:

<<<<<<<<<<<<
class Account
{
	function account() { return self; }

	# Other functions that operate on account.
}

class B inherit from Account
{
	Account myaccount;

	function account() { return myaccount; }

	# We get the functions for free.
}

Is this a known pattern in Object Oriented Programming and Design, and if so 
how is it called?

Regards,

	Shlomi Fish

---------------------------------------------------------------------
Shlomi Fish      [email-masked]
Homepage:        http://www.shlomifish.org/

Chuck Norris wrote a complete Perl 6 implementation in a day but then
destroyed all evidence with his bare hands, so no one will know his secrets.

---

From: Tzahi Fadida <[email-masked]>
Date: Sat, 27 Jan 2007 03:37:07 +0200

I did not completly understand what is the situation you are describing but
when you seem to describe the "Template Method Design Pattern".
In this pattern you have the base algorithm, for example (in loose pseudo code 
that looks like java):

abstract class SomeAlgorithm{

	final public calcIT(){
		this.calc1() + this.calc2()
	}
	abstract public calc1();
	public calc2(){
		return 0; //by default.
	}

}

and the concrete classes who supply the actual implementation of parts
of the algorithm. At any time you can also override the calc2() which is 
optional.

class concreteAlgorithm1 extends SomeAlgorithm{
	public calc1(){
		return 1;
	}
}

class concreteAlgorithm2 extends SomeAlgorithm{
	public calc1(){
		return 2;
	}
	public calc2(){
		return 3;
	}
}

On Friday 26 January 2007 18:15, Shlomi Fish wrote:
> translate the code to Common Lisp in order to learn Lisp better. So I
> started translating the methods of the classes, one by one.
>
> and tested it and then starting implementing the Dipole class. The
> factors() method was one of the first I implemented, and then I wrote two
> tests for the is_square predicate method, for both true and false returns.
> Surprisingly, they both passed without even implementing is_sqaure.
>
> factors of the "result" property, then in Dipole, they just did the same
> for the factors of the "result". So I didn't have to implement the rest of
> the methods.
>
> Thinking about it, I realised one can consciously use this scheme by
> working on an accessor and then over-riding this accessor in the derived
> class to point to the accessor of the containing class.
>
> So we can do:
>
> <<<<<<<<<<<<
> class Account
> {
> Is this a known pattern in Object Oriented Programming and Design, and if
> so how is it called?
>
> Regards,
>
> 	Shlomi Fish
>

-- 
Regards,
        Tzahi.
--
Tzahi Fadida
Blog: http://tzahi.blogsite.org | Home Site: http://tzahi.webhop.info
WARNING TO SPAMMERS:  see at 
http://members.lycos.co.uk/my2nis/spamwarning.html