Re: Can use of singletons denote poor project design?



"Phlip" <phlip2005@xxxxxxxxx> wrote in message
news:LcMLf.1428$lT2.388@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Daniel Parker wrote:

This discussion of a Singleton and related abuses is using the wrong
example.

I think it's using the right example.

A global that production code never writes on is mostly harmless. So it's
not the right example of the kind of Singleton that will irritate test
cases. Plenty of globals and Singletons won't (like I said before).

I suggested a test case that uses the defacto-global behind time() as a
little bit harder.

Well, in that case, you may as well use this one.

class Singleton {
static Singleton instance = new Singleton();

Singleton getInstance() {return instance;}

int flag;
int input;
int output;

void setFlag(int flag) {this.flag = flag;}
int getFlag() {return flag;}
void setInput(int input) {this.input = input;}
int getInput() {return input;}
void setOutput(int output) {this.output = output;}
int getOutput() {return output;}
}

void main(String[] args) {
Singleton.getInstance().setFlag(7);
Singleton..getInstance().setInput(3);

doSomething();
}

void doSomething() {
if (Singleton.getInstance().getFlag() == 2) {
int x = Singleton.getInstance().getInput();
Singleton.getInstance().setOutput(x+20);
}else if (Singleton.getInstance().getFlag() == 7) {
int x = Singleton.getInstance().getInput();
Singleton.getInstance().setOutput(x+29);
}
}

Sigh. This brings back memories of "Bob". Bob didn't like to pass
parameters. Bob liked global flags that changed what functions did. Bob
didn't like "else". Bob wrote in C and assembly on PC's with DOS extenders,
and when you'd call one of his functions to say, calculate a bond price, the
printer would print some hex characters, or things of that sort. Everyday I
would come in and remove a little bit of Bob.

Regards,
Daniel Parker
http://servingxml.sourceforge.net/


.



Relevant Pages