Abstract fields implemented by static final class?

477 views Asked by At

I'm trying to create custom color palettes for my Android app. I started doing it this way:

public class Theme {

    public static final class DARK {
        final static int POSITIVE_GREEN = Color.rgb(0, 200, 0);
        final static int NEGATIVE_RED = Color.rgb(200, 0, 0);
        final static int BACKGROUND_GREEN = Color.argb(255 - 220, 0, 255, 0); //220
        final static int BACKGROUND_RED = Color.argb(255 - 220, 255, 0, 0);
    }

    public static final class PASTEL {
        final static int POSITIVE_GREEN = Color.parseColor("#326262");
        final static int NEGATIVE_RED = etc
        final static int BACKGROUND_GREEN = etc
        final static int BACKGROUND_RED = etc
    }

}

but realized something was amiss when I wanted to refactor one of the field names. The field names should be abstract in THEME or something, but since these shouldn't be instantiable classes, I can't use the constructor trick suggested here: Why not abstract fields? How should I be doing this?

4

There are 4 answers

0
serg.nechaev On

Use of interfaces seems apppropriate here:

Every theme will implement this interface:

public interface Theme {
    int getPositiveGreen(); 
    ...
}

For example:

public class Dark implements Theme {

   private final static int POSITIVE_GREEN = Color.rgb(0, 200, 0);

   public int getPositiveGreen() {
       return POSITIVE_GREEN;
   }
   ....
}

In general use of public static variable should be limited to some very simple scenarios which is not what you are trying to achieve.

https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

0
march On

Use enum instead of int constants You can use enum instead, for example:

public enum DARK {
        POSITIVE_GREEN (0,200, ..){
              void abstractField(){
                 //Achieve your custom methods
              }
        },
        NEGATIVE_RED (200,0, ..){
           //Achieve your custom methods
        },
        BACKGROUND_GREEN (.....){
           //Achieve your custom methods
        },
        BACKGROUND_RED (...){
          //Achieve your custom methods
        },
      

       // Offer (with the Senate) constructor to initialize the fields defined
       DARK (int variable1, int variable2) {
                this.variable1 = variable1;
                this.variable2 = variable2;
       }
       // Define the fields you need
       private int variable1;
       private int variable2;

       //Custom abstract methods you need
       abstract void abstartField();
}

I do not know if you can help

0
ZhongYu On

An abstract method is just a function that the subclass needs to provide.

An "abstract field" can be thought as a value that needs to be provided too

public class Theme {

    public final int POSITIVE_GREEN;
    public final int NEGATIVE_RED;
    public final int BACKGROUND_GREEN;
    public final int BACKGROUND_RED;

    private Theme(int POSITIVE_GREEN, int NEGATIVE_RED, int BACKGROUND_GREEN, int BACKGROUND_RED)
    {
        this.POSITIVE_GREEN = POSITIVE_GREEN;
        this.NEGATIVE_RED = NEGATIVE_RED;
        this.BACKGROUND_GREEN = BACKGROUND_GREEN;
        this.BACKGROUND_RED = BACKGROUND_RED;
    }

    public static final Theme DARK = new Theme(
        Color.rgb(0, 200, 0),
        Color.rgb(200, 0, 0),
        Color.argb(255 - 220, 0, 255, 0),
        Color.argb(255 - 220, 255, 0, 0)
    );

    public static final Theme PASTEL = new Theme( ...
0
Aakash On

If you just want a contract of names to be used, you can use below concept.

public interface Theme {
    public int POSITIVE_GREEN;
    public int NEGATIVE_RED;
    public int BACKGROUND_GREEN;
    public int BACKGROUND_RED;
    public Color getColor(int colorName);
}

public static final class Theme1 extends Theme {
    Map<Integer, Color> colorMap = new HashMap<>();
    private Theme1() {
        colorMap.put(POSITIVE_GREEN, Color.rgb(0, 200, 0));
        colorMap.put(NEGATIVE_RED, Color.rgb(200, 0, 0));
        colorMap.put(BACKGROUND_GREEN, Color.argb(255 - 220, 0, 255, 0));
        colorMap.put(BACKGROUND_RED, Color.argb(255 - 220, 255, 0, 0));
    }

    @Override
    public Color getColor(int colorName) {
        return colorMap.get(colorName);
    }
}