Re: Abstract Factory of Factory??



On 2006-05-25 23:16:15 -0500, "Saurabh Aggrawal" <games60@xxxxxxxxxxx> said:

I have application called "Client" whose function is to monitor the
status of application "Server". Client is polling Server every 2
seconds for its current status. Server's status could be X, Y, Z, XX
,YY ,ZZ. Depending on the state of the Server the Client will popup a
dialog box that will be different for all the state's of Server. i.e
for the status X in Server the Client has dialog box DiaX in it.
Similarly DiaY, DiaZ and so on.....
-------------
| |
| Status |
----|---------
|
|
|--------|-------|--------|--------|---------|
X Y Z XX YY ZZ
------------
| |
| Dialog |
----|---------
|
|
|--------|-------|--------|--------|---------|
DiaX DiaY DiaZ DiaXX DiaYY DiaZZ


and the code goes like this:----

if(Status == iX)
{
X = new X();
DiaX = new DiaX();
}
else if(Status == iY)
{
Y = new Y();
DiaY = new DiaY();
}
........
.......
.......
......so on

Could you please suggest me which pattern to use, Fatory Pattern or
Abstract Factory Pattern. I think i am having problems understanding
them.

Thanks,
Saurabh Aggrawal

It seems to me that the factory you need looks like this:

public Client {
private ServerState serverState = null;

public void PollServerStatus() {
setServerState(getServerStatus());
}

public String getServerStatus() {
....
}

public void setServerState(String stateName) {
serverState = StateFactory.makeState(stateName);
serverState.showDialog();
}
}

public interface ServerState {
public void showDialog();
}

public class StateFactory {
private Map stateDictionary = new HashMap();
public StateFactory() {
stateDictionary.add("X", new StateX());
stateDictionary.add("Y", new StateY());
...
}

public ServerState makeState(String stateName) {
return stateDictionary.get(stateName);
}
}

public class StateX implements ServerState {
public void showDialog() {
Dialog d = new DialogX();
d.show();
}
}

.....

--
Robert C. Martin (Uncle Bob)  | email: unclebob@xxxxxxxxxxxxxxxx
Object Mentor Inc.            | blog:  www.butunclebob.com
The Agile Transition Experts  | web:   www.objectmentor.com
800-338-6716                  |



.