Re: Composition vs. Inheritance (Was: colored point redux) (fwd)

Pascal Costanza <[email protected]> Mon, 26 Feb 2007 19:50:04 +0100
Newsgroups gmane.comp.lang.lightweight
Message-ID <[email protected]>
On 26 Feb 2007, at 18:10, Daniel Yoo wrote:

> My apologies, but my message didn't get through the first time; let  
> me try again.
>
> ---------- Forwarded message ----------
> Date: Mon, 26 Feb 2007 10:35:44 -0500 (EST)
> From: Daniel Yoo <[email protected]>
> To: Robbert Haarman <[email protected]>
> Cc: [email protected]
> Subject: Re: [ll-discuss] Composition vs. Inheritance (Was: colored  
> point redux)
>
>>  Or, another example, strings and linked lists are both sequences,  
>> and thus
>>  could inherit some behavior (methods) that are common to all  
>> sequences. How
>>  would this be accomplished by composition?
>
>
> Hi Robbert,
>
> Let's assume that we have some described behavior and we want to  
> reuse that behavior without retyping it.
>
> Let's do a concrete example: let's say we do have things that  
> support something "getitem"ish, and we'd like to provide different  
> ways to iterate over them.  We can provide some template class for  
> iteration.  I'll use Java for these examples just to be very concrete.
>
> /**********************************************************/
> import java.util.Iterator;
>
> public abstract class ForwardIteration {
>      // Returns the ith item
>      abstract Object getItem(int i);
>
>      // Returns the length
>      abstract int len();
>
>      public Iterator iter() {
> 	 class MyIter implements Iterator {
> 	     int k = 0;
> 	     public Object next() {
> 	     	return getItem(k++);
> 	     }
>
> 	     public boolean hasNext() {
> 	     	return k < len();
> 	     }
> 	     public void remove() {
> 	     	throw new UnsupportedOperationException();
> 	     }
> 	 }
> 	 return new MyIter();
>     }
> }
> /**********************************************************/
[...]

> Ok, that works.  One problem, though, is allowing these things to  
> support _multiple_ ways of iteration.  How do we use inheritance to  
> support both forward iteration and backward iteration?

What's wrong with this?

public abstract class Iteration {
      // Returns the ith item
      abstract Object getItem(int i);

      // Returns the length
      abstract int len();

      public Iterator forwardIter() {
	 class MyIter implements Iterator {
	     int k = 0;
	     public Object next() {
	     	return getItem(k++);
	     }

	     public boolean hasNext() {
	     	return k < len();
	     }
	     public void remove() {
	     	throw new UnsupportedOperationException();
	     }
	 }
	 return new MyIter();
     }

      public Iterator backwardIter() {
	 class MyIter implements Iterator {
	     int k = len() - 1;
	     public Object next() {
	     	return getItem(k--);
	     }

	     public boolean hasNext() {
	     	return k >= 0;
	     }
	     public void remove() {
	     	throw new UnsupportedOperationException();
	     }
	 }
	 return new MyIter();
     }
}


Let me guess: You cannot add methods to an existing class after the  
fact?

Pascal

-- 
Pascal Costanza, mailto:[email protected], http://p-cos.net
Vrije Universiteit Brussel, Programming Technology Lab
Pleinlaan 2, B-1050 Brussel, Belgium