Static class for constants with reference to java resources

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite
1












I want to store the colors I use in the colors.xml file. In addition to using them in an xml-layout, I also want to use these colors in my Java code, when drawing things to the Canvas.



The thing is, I am not always using the colors in a class that extends Application. For example, I have classes for objects that get drawn to the Canvas.



The thing is, I don't want to store the context in each class that needs access to my resources file.



My current solution is the following:



colors.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorOne">#123456</color>
</resources>


A static class called Attributes for constants, including colors:



public class Attributes 
private static final Resources RESOURCES = MainActivity.getContext().getResources();

public static final int ONE_COLOR = RESOURCES.getColor(R.color.colorOne);
public static final int SOME_OTHER_CONSTANT = 1;



MainActivity:



public class MainActivity extends Activity {

private static Context context;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
this.context = getApplicationContext();

@Override
protected void onPause()
super.onPause();


@Override
protected void onResume()
super.onResume();


public static Context getContext()
return context;




And, finally in my object class:



public class ScreenArea 
private Rect area;
private Paint paint;

public ScreenArea(Rect area)
this.area = area;
paint = new Paint();
paint.setColor(Attributes.COLOR_ONE);


public void draw(Canvas canvas)
canvas.drawRect(area, paint);




Okay, so first of all, this works. But, I am wondering if this is the right way to go. I have been reading about the Singleton class, but most examples are very basic an not focusing on storing constants, like I am doing right now.



So my questions:



  • Is this the right way to go or should I implement a Singleton?

  • If so, who can point me in the right direction?






share|improve this question





















  • "Is this the right way to go or should I implement a Singleton?" Using a Singleton is rarely a good decision.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Jan 26 at 11:31










  • @πάνταῥεῖ: So, what is your suggestion?
    – MWB
    Jan 28 at 15:20










  • Not to use a Singleton.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Jan 28 at 17:39










  • And leave it the way I am currently doing? Doesn't that impose risks, considering app lifecycles etc?
    – MWB
    Jan 28 at 17:41
















up vote
2
down vote

favorite
1












I want to store the colors I use in the colors.xml file. In addition to using them in an xml-layout, I also want to use these colors in my Java code, when drawing things to the Canvas.



The thing is, I am not always using the colors in a class that extends Application. For example, I have classes for objects that get drawn to the Canvas.



The thing is, I don't want to store the context in each class that needs access to my resources file.



My current solution is the following:



colors.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorOne">#123456</color>
</resources>


A static class called Attributes for constants, including colors:



public class Attributes 
private static final Resources RESOURCES = MainActivity.getContext().getResources();

public static final int ONE_COLOR = RESOURCES.getColor(R.color.colorOne);
public static final int SOME_OTHER_CONSTANT = 1;



MainActivity:



public class MainActivity extends Activity {

private static Context context;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
this.context = getApplicationContext();

@Override
protected void onPause()
super.onPause();


@Override
protected void onResume()
super.onResume();


public static Context getContext()
return context;




And, finally in my object class:



public class ScreenArea 
private Rect area;
private Paint paint;

public ScreenArea(Rect area)
this.area = area;
paint = new Paint();
paint.setColor(Attributes.COLOR_ONE);


public void draw(Canvas canvas)
canvas.drawRect(area, paint);




Okay, so first of all, this works. But, I am wondering if this is the right way to go. I have been reading about the Singleton class, but most examples are very basic an not focusing on storing constants, like I am doing right now.



So my questions:



  • Is this the right way to go or should I implement a Singleton?

  • If so, who can point me in the right direction?






share|improve this question





















  • "Is this the right way to go or should I implement a Singleton?" Using a Singleton is rarely a good decision.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Jan 26 at 11:31










  • @πάνταῥεῖ: So, what is your suggestion?
    – MWB
    Jan 28 at 15:20










  • Not to use a Singleton.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Jan 28 at 17:39










  • And leave it the way I am currently doing? Doesn't that impose risks, considering app lifecycles etc?
    – MWB
    Jan 28 at 17:41












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I want to store the colors I use in the colors.xml file. In addition to using them in an xml-layout, I also want to use these colors in my Java code, when drawing things to the Canvas.



The thing is, I am not always using the colors in a class that extends Application. For example, I have classes for objects that get drawn to the Canvas.



The thing is, I don't want to store the context in each class that needs access to my resources file.



My current solution is the following:



colors.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorOne">#123456</color>
</resources>


A static class called Attributes for constants, including colors:



public class Attributes 
private static final Resources RESOURCES = MainActivity.getContext().getResources();

public static final int ONE_COLOR = RESOURCES.getColor(R.color.colorOne);
public static final int SOME_OTHER_CONSTANT = 1;



MainActivity:



public class MainActivity extends Activity {

private static Context context;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
this.context = getApplicationContext();

@Override
protected void onPause()
super.onPause();


@Override
protected void onResume()
super.onResume();


public static Context getContext()
return context;




And, finally in my object class:



public class ScreenArea 
private Rect area;
private Paint paint;

public ScreenArea(Rect area)
this.area = area;
paint = new Paint();
paint.setColor(Attributes.COLOR_ONE);


public void draw(Canvas canvas)
canvas.drawRect(area, paint);




Okay, so first of all, this works. But, I am wondering if this is the right way to go. I have been reading about the Singleton class, but most examples are very basic an not focusing on storing constants, like I am doing right now.



So my questions:



  • Is this the right way to go or should I implement a Singleton?

  • If so, who can point me in the right direction?






share|improve this question













I want to store the colors I use in the colors.xml file. In addition to using them in an xml-layout, I also want to use these colors in my Java code, when drawing things to the Canvas.



The thing is, I am not always using the colors in a class that extends Application. For example, I have classes for objects that get drawn to the Canvas.



The thing is, I don't want to store the context in each class that needs access to my resources file.



My current solution is the following:



colors.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorOne">#123456</color>
</resources>


A static class called Attributes for constants, including colors:



public class Attributes 
private static final Resources RESOURCES = MainActivity.getContext().getResources();

public static final int ONE_COLOR = RESOURCES.getColor(R.color.colorOne);
public static final int SOME_OTHER_CONSTANT = 1;



MainActivity:



public class MainActivity extends Activity {

private static Context context;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
this.context = getApplicationContext();

@Override
protected void onPause()
super.onPause();


@Override
protected void onResume()
super.onResume();


public static Context getContext()
return context;




And, finally in my object class:



public class ScreenArea 
private Rect area;
private Paint paint;

public ScreenArea(Rect area)
this.area = area;
paint = new Paint();
paint.setColor(Attributes.COLOR_ONE);


public void draw(Canvas canvas)
canvas.drawRect(area, paint);




Okay, so first of all, this works. But, I am wondering if this is the right way to go. I have been reading about the Singleton class, but most examples are very basic an not focusing on storing constants, like I am doing right now.



So my questions:



  • Is this the right way to go or should I implement a Singleton?

  • If so, who can point me in the right direction?








share|improve this question












share|improve this question




share|improve this question








edited Jan 26 at 11:18









Julien Rousé

446416




446416









asked Jan 26 at 11:08









MWB

111




111











  • "Is this the right way to go or should I implement a Singleton?" Using a Singleton is rarely a good decision.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Jan 26 at 11:31










  • @πάνταῥεῖ: So, what is your suggestion?
    – MWB
    Jan 28 at 15:20










  • Not to use a Singleton.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Jan 28 at 17:39










  • And leave it the way I am currently doing? Doesn't that impose risks, considering app lifecycles etc?
    – MWB
    Jan 28 at 17:41
















  • "Is this the right way to go or should I implement a Singleton?" Using a Singleton is rarely a good decision.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Jan 26 at 11:31










  • @πάνταῥεῖ: So, what is your suggestion?
    – MWB
    Jan 28 at 15:20










  • Not to use a Singleton.
    – Ï€Î¬Î½Ï„α ῥεῖ
    Jan 28 at 17:39










  • And leave it the way I am currently doing? Doesn't that impose risks, considering app lifecycles etc?
    – MWB
    Jan 28 at 17:41















"Is this the right way to go or should I implement a Singleton?" Using a Singleton is rarely a good decision.
– Ï€Î¬Î½Ï„α ῥεῖ
Jan 26 at 11:31




"Is this the right way to go or should I implement a Singleton?" Using a Singleton is rarely a good decision.
– Ï€Î¬Î½Ï„α ῥεῖ
Jan 26 at 11:31












@πάνταῥεῖ: So, what is your suggestion?
– MWB
Jan 28 at 15:20




@πάνταῥεῖ: So, what is your suggestion?
– MWB
Jan 28 at 15:20












Not to use a Singleton.
– Ï€Î¬Î½Ï„α ῥεῖ
Jan 28 at 17:39




Not to use a Singleton.
– Ï€Î¬Î½Ï„α ῥεῖ
Jan 28 at 17:39












And leave it the way I am currently doing? Doesn't that impose risks, considering app lifecycles etc?
– MWB
Jan 28 at 17:41




And leave it the way I am currently doing? Doesn't that impose risks, considering app lifecycles etc?
– MWB
Jan 28 at 17:41










1 Answer
1






active

oldest

votes

















up vote
2
down vote













well as pointed out using singletons is considered bad practice and using global constants in an object-orientatd world is bad practice as well...



So, what is your suggestion?




Consider the purpose of the attribute



you want to define a app-specific attribute (color), an attribute you want to configure - this is the very reason why you use ressources, right. Or do you want to define a constant?



1. it's a constant



if you don't want to specify a configurable attribute you should define a constant in your app



private static final int COLOR_ONE = 0x123456;


2. it's an attribute



but if you want to specify a configurable attribute you simply define it as such in your app:



int colorOne = MainActivity.getContext().getResources().getColor(R.color.colorOne); 


you are right, this might be a bit intricately so it would be just as simple to wrap this call in a method



private int getColorFromRessource(int colorId)
return MainActivity.getContext().getResources().getColor(colorId);



this would be applied in your setup of the Paint in an quite easy way:



public ScreenArea(Rect area) 
this.area = area;
paint = new Paint();
paint.setColor(getColorFromRessource(R.color.colorOne)); //obvious
//paint.setColor(Attributes.COLOR_ONE); far less obvious






share|improve this answer





















  • I think this is pretty much what I am trying to do. The main difference is that I have defined a constant to store the color, where you wrap it all in a method. The advantage of the method is that you don't have a singleton. The disadvantage, I guess is that using a constant is more efficient, right?
    – MWB
    Mar 11 at 12:15






  • 1




    Well it's up to you - if your color is a constant then you should define a constant in your code... if your color is an attribute you should define it in your xml and access it via the methods - just as answered above...
    – Martin Frank
    Mar 11 at 18:33










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186049%2fstatic-class-for-constants-with-reference-to-java-resources%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













well as pointed out using singletons is considered bad practice and using global constants in an object-orientatd world is bad practice as well...



So, what is your suggestion?




Consider the purpose of the attribute



you want to define a app-specific attribute (color), an attribute you want to configure - this is the very reason why you use ressources, right. Or do you want to define a constant?



1. it's a constant



if you don't want to specify a configurable attribute you should define a constant in your app



private static final int COLOR_ONE = 0x123456;


2. it's an attribute



but if you want to specify a configurable attribute you simply define it as such in your app:



int colorOne = MainActivity.getContext().getResources().getColor(R.color.colorOne); 


you are right, this might be a bit intricately so it would be just as simple to wrap this call in a method



private int getColorFromRessource(int colorId)
return MainActivity.getContext().getResources().getColor(colorId);



this would be applied in your setup of the Paint in an quite easy way:



public ScreenArea(Rect area) 
this.area = area;
paint = new Paint();
paint.setColor(getColorFromRessource(R.color.colorOne)); //obvious
//paint.setColor(Attributes.COLOR_ONE); far less obvious






share|improve this answer





















  • I think this is pretty much what I am trying to do. The main difference is that I have defined a constant to store the color, where you wrap it all in a method. The advantage of the method is that you don't have a singleton. The disadvantage, I guess is that using a constant is more efficient, right?
    – MWB
    Mar 11 at 12:15






  • 1




    Well it's up to you - if your color is a constant then you should define a constant in your code... if your color is an attribute you should define it in your xml and access it via the methods - just as answered above...
    – Martin Frank
    Mar 11 at 18:33














up vote
2
down vote













well as pointed out using singletons is considered bad practice and using global constants in an object-orientatd world is bad practice as well...



So, what is your suggestion?




Consider the purpose of the attribute



you want to define a app-specific attribute (color), an attribute you want to configure - this is the very reason why you use ressources, right. Or do you want to define a constant?



1. it's a constant



if you don't want to specify a configurable attribute you should define a constant in your app



private static final int COLOR_ONE = 0x123456;


2. it's an attribute



but if you want to specify a configurable attribute you simply define it as such in your app:



int colorOne = MainActivity.getContext().getResources().getColor(R.color.colorOne); 


you are right, this might be a bit intricately so it would be just as simple to wrap this call in a method



private int getColorFromRessource(int colorId)
return MainActivity.getContext().getResources().getColor(colorId);



this would be applied in your setup of the Paint in an quite easy way:



public ScreenArea(Rect area) 
this.area = area;
paint = new Paint();
paint.setColor(getColorFromRessource(R.color.colorOne)); //obvious
//paint.setColor(Attributes.COLOR_ONE); far less obvious






share|improve this answer





















  • I think this is pretty much what I am trying to do. The main difference is that I have defined a constant to store the color, where you wrap it all in a method. The advantage of the method is that you don't have a singleton. The disadvantage, I guess is that using a constant is more efficient, right?
    – MWB
    Mar 11 at 12:15






  • 1




    Well it's up to you - if your color is a constant then you should define a constant in your code... if your color is an attribute you should define it in your xml and access it via the methods - just as answered above...
    – Martin Frank
    Mar 11 at 18:33












up vote
2
down vote










up vote
2
down vote









well as pointed out using singletons is considered bad practice and using global constants in an object-orientatd world is bad practice as well...



So, what is your suggestion?




Consider the purpose of the attribute



you want to define a app-specific attribute (color), an attribute you want to configure - this is the very reason why you use ressources, right. Or do you want to define a constant?



1. it's a constant



if you don't want to specify a configurable attribute you should define a constant in your app



private static final int COLOR_ONE = 0x123456;


2. it's an attribute



but if you want to specify a configurable attribute you simply define it as such in your app:



int colorOne = MainActivity.getContext().getResources().getColor(R.color.colorOne); 


you are right, this might be a bit intricately so it would be just as simple to wrap this call in a method



private int getColorFromRessource(int colorId)
return MainActivity.getContext().getResources().getColor(colorId);



this would be applied in your setup of the Paint in an quite easy way:



public ScreenArea(Rect area) 
this.area = area;
paint = new Paint();
paint.setColor(getColorFromRessource(R.color.colorOne)); //obvious
//paint.setColor(Attributes.COLOR_ONE); far less obvious






share|improve this answer













well as pointed out using singletons is considered bad practice and using global constants in an object-orientatd world is bad practice as well...



So, what is your suggestion?




Consider the purpose of the attribute



you want to define a app-specific attribute (color), an attribute you want to configure - this is the very reason why you use ressources, right. Or do you want to define a constant?



1. it's a constant



if you don't want to specify a configurable attribute you should define a constant in your app



private static final int COLOR_ONE = 0x123456;


2. it's an attribute



but if you want to specify a configurable attribute you simply define it as such in your app:



int colorOne = MainActivity.getContext().getResources().getColor(R.color.colorOne); 


you are right, this might be a bit intricately so it would be just as simple to wrap this call in a method



private int getColorFromRessource(int colorId)
return MainActivity.getContext().getResources().getColor(colorId);



this would be applied in your setup of the Paint in an quite easy way:



public ScreenArea(Rect area) 
this.area = area;
paint = new Paint();
paint.setColor(getColorFromRessource(R.color.colorOne)); //obvious
//paint.setColor(Attributes.COLOR_ONE); far less obvious







share|improve this answer













share|improve this answer



share|improve this answer











answered Mar 1 at 12:58









Martin Frank

522315




522315











  • I think this is pretty much what I am trying to do. The main difference is that I have defined a constant to store the color, where you wrap it all in a method. The advantage of the method is that you don't have a singleton. The disadvantage, I guess is that using a constant is more efficient, right?
    – MWB
    Mar 11 at 12:15






  • 1




    Well it's up to you - if your color is a constant then you should define a constant in your code... if your color is an attribute you should define it in your xml and access it via the methods - just as answered above...
    – Martin Frank
    Mar 11 at 18:33
















  • I think this is pretty much what I am trying to do. The main difference is that I have defined a constant to store the color, where you wrap it all in a method. The advantage of the method is that you don't have a singleton. The disadvantage, I guess is that using a constant is more efficient, right?
    – MWB
    Mar 11 at 12:15






  • 1




    Well it's up to you - if your color is a constant then you should define a constant in your code... if your color is an attribute you should define it in your xml and access it via the methods - just as answered above...
    – Martin Frank
    Mar 11 at 18:33















I think this is pretty much what I am trying to do. The main difference is that I have defined a constant to store the color, where you wrap it all in a method. The advantage of the method is that you don't have a singleton. The disadvantage, I guess is that using a constant is more efficient, right?
– MWB
Mar 11 at 12:15




I think this is pretty much what I am trying to do. The main difference is that I have defined a constant to store the color, where you wrap it all in a method. The advantage of the method is that you don't have a singleton. The disadvantage, I guess is that using a constant is more efficient, right?
– MWB
Mar 11 at 12:15




1




1




Well it's up to you - if your color is a constant then you should define a constant in your code... if your color is an attribute you should define it in your xml and access it via the methods - just as answered above...
– Martin Frank
Mar 11 at 18:33




Well it's up to you - if your color is a constant then you should define a constant in your code... if your color is an attribute you should define it in your xml and access it via the methods - just as answered above...
– Martin Frank
Mar 11 at 18:33












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186049%2fstatic-class-for-constants-with-reference-to-java-resources%23new-answer', 'question_page');

);

Post as a guest













































































Uwy3FLg0PWr9PJgofWej
qOAK4mwA4SQ6u1W5,4S96rIdEvg,JWD,nJ7uySRR tU0k8b4 C O7XW0

Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Read an image with ADNS2610 optical sensor and Arduino Uno