AlexTheo
AlexTheo

Reputation: 4184

Java enum class

I am implementing the enum class which I will use to retrieve some background in application, a 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 each time I trying to get a painter but I prefer just to write the type of the painter instead. I thought that if I can to extend the Painter by my enum then probably I will get the right functionality, but it looks that it is not possible in java.

public enum Painters extends Painter<Component>

Do you know any solution for this problem???

Currently I am using it in this way:

Painters.Background.painter();

but I need:

Painters.Background;

Upvotes: 3

Views: 1379

Answers (2)

Perception
Perception

Reputation: 80598

You could always define your painter as an interface and have your enum implement it. You won't be able to use generic wildcards, but as long as thats not a constraint you will be able to reference the painter in code the way you are wanting too:

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

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

    private Painter<Component> _painter;

    // ...
}

Painter somePainter = Painters.Background;

Your enum would effectively be proxying over actual painter implementations.

Upvotes: 2

Garrett Hall
Garrett Hall

Reputation: 30042

Typically Enums are reserved for immutable constants where you need the the syntactic sugar of the switch/case and constructors. A factory pattern is more appropriate here:

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

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

Upvotes: 2

Related Questions