[plt-scheme] Functional Abstraction in Java

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Jul 1 14:01:03 EDT 2009

Let me refine Robby's suggestion:

* If getSat and getSun are in the -same- class, you need to use the  
command pattern:

   interface GetXDay {
     int apply(int index);
   }

   Now implement this interface for Sat and Sun, and pass in  
appropriate instances. You don't apply these instances, instead you  
write

         if(getSaturdayIndex.apply(i) % x == 0) { ...

* If getSat and getSun belong to two distinct classes, create a  
superclass with an abstract method getXDay that you override with  
getSatDay in one subclass and with getSunDay in the other.

I consider both solutions in the spirit of OOPLs. -- Matthias





On Jul 1, 2009, at 1:32 PM, Robby Findler wrote:

> You can do the same thing as in HtDP by passing in a object with a
> single method that does what the function would have done.
>
> A more OO solution is to put a getXdays method in a common (abstract)
> superclass and then call getXIndex and have the two classes override
> getXIndex with getSundayIndex and getSaturdayIndex. This only works if
> the two classes can share a common superclass.
>
> This is called the "hook and template" design pattern, I believe.
>
> Robby
>
> On Wed, Jul 1, 2009 at 12:23 PM, Andrei  
> Estioco<chadestioco at gmail.com> wrote:
>> Hello. My apologies if I am asking this in the wrong mailing list  
>> (this
>> being a Java question). But since I encountered this concept in  
>> HtDP, I
>> thought of asking around here. (See Chapter 21 of HtDP). See I have a
>> function private int getSaturdays(int x, int fordays){ int Weeks =
>> getWeeks(x, fordays); int SaturdayCounter = 0; int i = 1; while(i <=
>> hartalWeeks){ if(getSaturdayIndex(i) % x == 0){ SaturdayCounter++;  
>> i++; }
>> else{ i++; } } return SaturdayCounter; } and another one private int
>> getSundays(int x, int fordays){ int Weeks = getWeeks(x, fordays); int
>> SundaysCounter = 0; int i = 1; while(i <= hartalWeeks){ if 
>> (getSundayIndex(i)
>> % x == 0){ SundayCounter++; i++; } else{ i++; } } return  
>> SundayCounter; } As
>> obvious, they do the same process, their sole difference being a  
>> function
>> call (the one in the if line). Now, if I were to do this in  
>> Scheme, I'll
>> just add another argument to my function which will be called in  
>> the if line
>> (as in Chapter 21). However, I do not know how to do this in Java.
>> I know how to sort in Java using the Comparator interface and all  
>> that OO
>> stuff. I thought of using that style here but then I was wondering  
>> if I
>> could do this without OO. Is it possible?
>>
>> Thanks in advance,
>> --
>> Chad Estioco
>> BS Computer Science (ongoing)
>> University of the Philippines-Diliman
>> ==============================
>> http://www.geocities.com/lokisky_walker
>>
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.