Re: An OOP Pattern Discovered by Accident
Tzahi Fadida <[email protected]>
| Newsgroups | gmane.culture.hackers.israel |
|---|---|
| Message-ID | <[email protected]> |
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:
> 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 shlomif-ik1l9ssToec+JF/[email protected]
> 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.
--
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
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/hackers-il/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/hackers-il/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[email protected]
mailto:[email protected]
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/