pivot-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alejandro Vilar" <alejandro.vi...@synacom.com.bo>
Subject RE: Constant Interface Antipattern
Date Thu, 22 Apr 2010 04:58:39 GMT
Hi Michael, 

You right about the "constant" meaning of this anti-pattern, but I'm talking
more about implementations into interfaces. An example could be:

public interface ComponentListener{
//...methods
}

public class Adapters{
   public static class ComponentListenerAdapter implements
ComponentListener{
	//...empty implementations
   }
}


import static Adapters.ComponentListenerAdapter;
public class Foo {

    void bar() {
        ComponentListener l = new ComponentListenerAdapter();
    }
}

//versus current
public class Foo {

    void bar() {
        ComponentListener l = new ComponentListener.Adapter();
    }
}


Also there is another issue when inheritance is used, for example

package example;
public interface Foo {
    public static class Adapter implements Foo {
        public void bar() {
            System.out.println("Implemented into interface");
        }
    }

    void bar();
}	

package example;
public abstract class AbstractAdapter extends Foo.Adapter implements Foo { }

package other;
import example.AbstractAdapter;

public class Adapter extends AbstractAdapter {
    public void bar() {
        System.out.println("Subclass implementation");
    }
    public static void main(String[] args) {
        Adapter adapter=new Adapter();
        adapter.bar();
    }
}

Running Adapter.main() method will print "Implemented into interface",
that’s because Foo.Adapter is taken instead of my own Adapter subclass,
static imports apply only to the current file, the previous example could be
transformed to:

package example;
public interface Foo {
    void bar();
}	

package example;
public class Adapters {
    public static class Adapter implements Foo {
        public void bar() {
            System.out.println("Implemented into Adapters class");
        }
    }
}

package example;
import static example.Adapters.Adapter;
public abstract class AbstractAdapter extends Adapter implements Foo { }



Running Adapter.main() method will print "Subclass implementation" as
expected. Another way to solve:

package other;
import example.AbstractAdapter;
public class Adapter extends AbstractAdapter {
    public void bar() {
        System.out.println("Subclass implementation");
    }
    public static void main(String[] args) {
        other.Adapter adapter=new other.Adapter();
        adapter.bar();
    }
}

(Note: Type my class's FQCN in my own class?)

Maybe it’s a extreme case, but wanted to point out. (and sorry for my
english :P)

Regards,
-Alejandro

-----Original Message-----
From: Michael Allman [mailto:msa@allman.ms] 
Sent: Miércoles, 21 de Abril de 2010 08:36 p.m.
To: user@pivot.apache.org
Subject: Re: Constant Interface Antipattern

Hi Alejandro,

I don't see any constants there, just a nested class.  It's not like you 
can do

foo = ComponentListener.Adapter;

Cheers,

Michael


On Wed, 21 Apr 2010, Alejandro Vilar wrote:

> Hi, just an observation but there are many places where this anti-pattern
> appears, for example ComponentListener.Adapter, anybody aware of that?
>
>
>
> Refs:
>
> http://en.wikipedia.org/wiki/Constant_interface
>
> http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html
>
>
>
> Cheers,
>
> -Alejandro
>
>
>
>
>
>


Mime
View raw message