Java Rename Class

I am implementing an enum class that I will use to get some background in the application, the current implementation of this class is here:

public enum Painters{

    /**
     * Available painters.
     */
    Background(getBackgroundPainter()),
    InactiveBackground(getInactiveBackgroundPainter()),
    DesktopBackground(getBackgroundPainter());

    /**
     * The background painter.
     */
    private Painter<Component> _painter;

    /**
     * Constructor will initialize the object.
     */
    Painters(Painter<Component> painter){
        _painter = painter;
    }

    /**
     * Will return a current painter.
     * @return instance of Painter<Component>
     */
    public Painter<Component> painter(){
        return _painter;
    }

    private static Painter<Component> getBackgroundPainter(){
        MattePainter mp = new MattePainter(Colors.White.alpha(1f));
        PinstripePainter pp = new PinstripePainter(Colors.Gray.alpha(0.2f),45d);
       return (new CompoundPainter<Component>(mp, pp)); 
    }

    private static Painter<Component> getInactiveBackgroundPainter(){
        MattePainter mp = new MattePainter(Colors.White.alpha(1f));
        GlossPainter gp = new GlossPainter(Colors.Gray.alpha(0.1f), GlossPainter.GlossPosition.BOTTOM);
        PinstripePainter pp = new PinstripePainter(Colors.Gray.alpha(0.2f), 45d);
        return (new CompoundPainter<Component>(mp, pp, gp));    
    }
}

My problem is that I need to call the painter () method every time I try to get an artist, but I prefer to just write the type of artist. I thought that if I can extend the Painter by my enumeration, then I will probably get the necessary functionality, but it seems that this is not possible in java.

public enum Painters extends Painter<Component>

Do you know any solution to this problem ???

I am currently using it as follows:

Painters.Background.painter();

but I need:

Painters.Background;
+3
source share
2 answers

, enum . , , , :

interface Painter<T> {
    // ...
}

public enum Painters implements Painter<Component> {
    Background(getBackgroundPainter()),
    InactiveBackground(getInactiveBackgroundPainter()),
    DesktopBackground(getBackgroundPainter());

    private Painter<Component> _painter;

    // ...
}

Painter somePainter = Painters.Background;

.

+2

Enums , / . factory :

public class PainterFactory {
    public static Painter<Component> getBackgroundPainter(){
    ...
    }

    public static Painter<Component> getInactiveBackgroundPainter(){
    ...
    }
}
+2

All Articles