0

Right now I have a config reader class which goes through a text config file and adds values it finds to variables I have declared earlier. Then all these variables are passed to a constructor. Needless to say, its messy and it looks like this:

String buttonText = "";
String buttonPassword = ""; 
String buttonAction = ""; 
String buttonFont = "";
int buttonPosX = 0;
int buttonPosY = 0;
int buttonWidth = 0;
int buttonHeight = 0;
int buttonFontSize = 0;
int buttonFontColor = 0;
int buttonBgColor = 0;
int buttonLayoutPosition = 0;

// Gathering information here ...

parent.addButton(buttonText, buttonPassword, buttonPosX, buttonPosY, buttonLayoutPosition, buttonWidth, buttonHeight, buttonAction, buttonFont, buttonFontSize, buttonFontColor, buttonBgColor);

Is there a better way to do this? I've heard of maps, would this be a good location to use one? I want to keep efficiency in mind but also code readability and maintenance.

user83643
  • 2,773
  • 2
  • 16
  • 13

4 Answers4

3

You can create a ButtonConfig class that has all those values as fields. Then you only need to pass a single object to the c'tor. Depending on the organization of your overall program, it may make sense to move the code that initializes all the config parameters into the configuration class itself.

Ted Hopp
  • 227,407
  • 48
  • 383
  • 507
2

This is a link to an old thread, but it explains exactly what I would do, I'd use the builder pattern: Managing constructors with many parameters in Java

Community
  • 1
  • 1
Kaj
  • 10,744
  • 1
  • 31
  • 27
2

If you use Eclipse you can choose in the context menu

->refactor->Extract class

to create a data object. This data object has to be written only once and will be passed as whole object to the constructor.

This is a valuable alternative to maps, since you do not loose type validation and javadocs etc.

Your code would then look like:

public static class Data
{
  String buttonText = "";
  String buttonPassword = ""; 
  String buttonAction = ""; 
  String buttonFont = "";
  int buttonPosX = 0;
  ...
}

...
private Data data = new Data();
...
parent.addButton(data);
...

The dialog from Eclipse to do this for you will look like the following:

Extract Class

Omnaest
  • 3,131
  • 1
  • 17
  • 18
  • wow I didn't know you could do this. Is this standard at all? If I use this method will someone else pull a "wtf is he doing?" when looking at my code? – user83643 Jun 03 '11 at 19:57
  • It's used all the time and basically the same thing as my answer. That won't stop someone from wondering "wtf is he doing?"; you'll just have to live with that risk. :) – Ted Hopp Jun 03 '11 at 20:03
  • Its quite a standard approach, nothing spectacular at all. You can use it for certain... – Omnaest Jun 03 '11 at 20:04
1

(It is pointless here to initialize a String to "", by the way.)

You probably want to construct a Map of String to Object, using perhaps HashMap, and pass that in. That is a simple way to pass an arbitrary key-value set. A Properties object may also be appropriate since that class can read key-value pairs from a file, though all are treated as text.

Is it efficient? there's really not much overhead there, no. I don't perceive this is in a tight loop, so until you prove it's too slow, don't worry about it.

Sean Owen
  • 65,067
  • 23
  • 139
  • 171
  • Thanks @Sean Owen, I'll look into HashMaps and see if that will be prettier in terms of readability. I mean, what's happening is straightforward but I don't like long lines in programming, so ideally I'd want to pass a Map of some sort. – user83643 Jun 03 '11 at 19:39