knowledge-database (beta)

Current group: comp.object

Empty interface and polymorphism

Empty interface and polymorphism  
Vijay
 Re: Empty interface and polymorphism  
Vijay
 Re: Empty interface and polymorphism  
Ilja Preuß
 Re: Empty interface and polymorphism  
Vijay
 Re: Empty interface and polymorphism  
Ilja Preuß
 Re: Empty interface and polymorphism  
Andrew McDonagh
 Re: Empty interface and polymorphism  
Ilja Preuß
 Re: Empty interface and polymorphism  
Andrew McDonagh
 Re: Empty interface and polymorphism  
t_p_c
 Re: Empty interface and polymorphism  
Ilja Preuß
 Re: Empty interface and polymorphism  
Ilja Preuß
From:Vijay
Subject:Empty interface and polymorphism
Date:20 Jan 2005 04:08:57 -0800
Hi

I have just looked at some code, which looks bit useless to me but I
might be wrong.

public interface SomeDAO
{
// empty interface - no methods or fields
}


public class BaseDAO implements SomeDAO
{
public method1()
{

}

public method2()
{

}

}


public class SpecificDAO extends BaseDAO
{

public method3()
{
// uses the inherited methods from BaseDAO

}

public method4()
{
// uses the inherited methods from BaseDAO

}

}




My question is what functionality does the interface SomeDAO provides
to this code. The only thing I can think of is that it can identify the
objects, but it can't do anything with them until they are down casted.

The author of the SomeDAO has comments for this interface, which says
as follow:

"Interface which the BaseDAO implements
This is an Empty interface, this will be required in case
there is a need for a DAO factory, so that the DAO factory
can return this interface "


Please comment.

Regards
Vijay
From:Vijay
Subject:Re: Empty interface and polymorphism
Date:20 Jan 2005 06:23:06 -0800

Ilja Preu=DF wrote:
> This is often called a Tag Interface - see
> http://c2.com/cgi/wiki?TagInterface
>

I accept that the empty interface is used (like Serializable interface
in java) for tagging.

> We are using this in similar cases, not to enable polymorphism, but
just to
> communicate intent: you can see in the source code that a method is
meant to
> return DAOs, and can easily search for all classes that are meant to
be
> DAOs.

But how can a DAO of Type SomeDAO(The empty interface) be used, if it
was returned by a factory?


>
> I'm not yet sure wether I really like it, though.
>=20
> Cheers, Ilja
From:Ilja Preuß
Subject:Re: Empty interface and polymorphism
Date:Thu, 20 Jan 2005 22:34:58 +0100
Vijay wrote:

> But how can a DAO of Type SomeDAO(The empty interface) be used, if it
> was returned by a factory?

The client needs to know the more specific type and cast.

Cheers, Ilja
From:Vijay
Subject:Re: Empty interface and polymorphism
Date:21 Jan 2005 02:26:32 -0800

Ilja Preu=DF wrote:
> Vijay wrote:
>
> > But how can a DAO of Type SomeDAO(The empty interface) be used, if
it
> > was returned by a factory?
>
> The client needs to know the more specific type and cast.
>
> Cheers, Ilja

So that means it is not one of the best practices, may be not even a
common practice. I know, may be a anti-pattern. Thanks Ilja.
From:Ilja Preuß
Subject:Re: Empty interface and polymorphism
Date:Fri, 21 Jan 2005 16:36:43 +0100
Vijay wrote:
> Ilja Preuß wrote:
>> Vijay wrote:
>>
>>> But how can a DAO of Type SomeDAO(The empty interface) be used, if
>>> it was returned by a factory?
>>
>> The client needs to know the more specific type and cast.
>>
>> Cheers, Ilja
>
> So that means it is not one of the best practices, may be not even a
> common practice. I know, may be a anti-pattern. Thanks Ilja.

No, that's not what it means, as far as I can tell. How do you come to that
conclusion?

After all, not using the interface doesn't remove the need for the cast,
does it?

Curious, Ilja
From:Andrew McDonagh
Subject:Re: Empty interface and polymorphism
Date:Fri, 21 Jan 2005 19:28:48 +0000
Vijay wrote:
> Ilja Preuß wrote:
>
>>Vijay wrote:
>>
>>
>>>But how can a DAO of Type SomeDAO(The empty interface) be used, if
>
> it
>
>>>was returned by a factory?
>>
>>The client needs to know the more specific type and cast.
>>
>>Cheers, Ilja
>
>
> So that means it is not one of the best practices, may be not even a
> common practice. I know, may be a anti-pattern. Thanks Ilja.
>

Not necessarily, it can help to remove build dependencies (See
dependency Inversion Principle).

We have a java library for a framework that allows you to add plugins
into certain objects. These plugins just implement the Tag interface.

We created our own interfaces that derived from these tag interfaces, so
that when they are used we cast them again to our still generic
interface, regardless of their actual type, thereby preserving the need
to have to know the exact type to cast too.

Pseudo code eg.


interface TagInterface{
}

interface OurInterface extends TagInterface {
public someMethod();
}

Class FirstPlugin implements OurInterface() {
public someMethod() {
....
}
}

Class SecondPlugin implements OurInterface() {
public someMethod() {
....
}
}

class ClientSetupCode {

public AddPlugins() {
FrameWorkObject.addPlugin(new FirstPlugin() );
FrameWorkObject.addPlugin(new SecondPlugin() );
}

}


Class ClientCodeThatUsesFrameWork {

public doSomething() {
OurInterface ours = (OurInterface)FrameWorkObject.getPlugin()
ours.someMethod();
}
}



It also means the frame work can be a little more constrained than
simply allowing any object to be added as a plugin.
From:Ilja Preuß
Subject:Re: Empty interface and polymorphism
Date:Fri, 21 Jan 2005 23:11:13 +0100
Andrew McDonagh wrote:

[a very nice example]

We are doing exactly the same! :)

Cheers, Ilja
From:Andrew McDonagh
Subject:Re: Empty interface and polymorphism
Date:Fri, 21 Jan 2005 23:39:06 +0000
Ilja Preuß wrote:
> Andrew McDonagh wrote:
>
> [a very nice example]
>
> We are doing exactly the same! :)
>
> Cheers, Ilja
>
>

Cool!

The guys on my team thought it barmy when I first suggested it, then
realised its power and potential. Mind you it helped a great deal when
they discovered that its actually a known idiom/pattern that I'd used -
even though I didn't at the time!
From:t_p_c
Subject:Re: Empty interface and polymorphism
Date:21 Jan 2005 02:25:40 -0800
So the interface must had the methods declared...
then when the factory returns a SomeDAO it can be used....
right?
From:Ilja Preuß
Subject:Re: Empty interface and polymorphism
Date:Fri, 21 Jan 2005 16:39:12 +0100
t_p_c wrote:
> So the interface must had the methods declared...
> then when the factory returns a SomeDAO it can be used....
> right?

If you can do that, it's best.

It's not always possible, though. Sometimes you the different DAO will be
used by different clients with different needs (and therefore needs for
different methods). In that case every client needs a different interface to
work with, and can safely cast to it.

Cheers, Ilja
From:Ilja Preuß
Subject:Re: Empty interface and polymorphism
Date:Thu, 20 Jan 2005 13:42:44 +0100
This is often called a Tag Interface - see
http://c2.com/cgi/wiki?TagInterface

We are using this in similar cases, not to enable polymorphism, but just to
communicate intent: you can see in the source code that a method is meant to
return DAOs, and can easily search for all classes that are meant to be
DAOs.

I'm not yet sure wether I really like it, though.

Cheers, Ilja
   

Copyright © 2006 knowledge-database   -   All rights reserved