In my qooxdoo application I have 4 buttons. Login, logout, register and profile. Every button has an action class. Those classes are subclassed from a common abstract class. By using the command pattern I call the execute function of the according class every time a button is clicked. The function looks like this
execute: function() {
var contentString = "login-form";
//do some generic stuff
if (win.getContentString() === contentString) {
//do some generic stuff
} else {
var content = new myapp.apps.userActions.SLoginForm();
//do some more generic stuff
}
}
That execute function has to be implemented in all 4 subclasses and the only things that change are the variables content and contentString.
I am thinking of using a factory function and every time return the appropriate object based on the contentString variable.
execute:function(){
var contentString = "login-form";
this.doTheGenericStuff(contentString);
},
doTheGenericStuff: function(contentString){
//do the generic stuff
var content = this.getTheObject(contentString);
//do some more generic stuff
},
getTheObject: function(contentString){
switch(contentString){
case "login-form":
return new myapp.apps.userActions.SLoginForm();
break;
case "register-form":
return new myapp.apps.userActions.SRegisterForm();
break;
//etc
}
}
While this seems ok (haven't tested it yet) I don't like it much because every time I add new actions I have to update the factory function. Is there any more clever way to achieve this? Maybe some feature of javascript that I don't know?
I think the use of template method pattern is more appropriate in this case.
So on your abstract class your have:
getMyContentString: function() { return "login-form"; //or any default value },
getMyContent: function() { return new myapp.apps.userActions.SLoginForm() },
execute: function() {
var contentString = getMyContentString(); // to be overridden
//do some generic stuff
if (win.getContentString() === contentString) {
//do some generic stuff
} else {
var content = getMyContent();
//do some more generic stuff
}
}
And each child object just need to provide the appropriate getMyContentString()
and getMyContent()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments