How can I reduce boilerplate from this function?

voger

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?

DJ.

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I reduce boilerplate from this function?

From Dev

Too much boilerplate, how can I reduce my POJO builders?

From Dev

How to reduce boilerplate with Kleisli

From Dev

How to reduce boilerplate provisioning in modules?

From Dev

Can I remove spaces from a property name inside a reduce function?

From Dev

How can I reduce the amount of buckets created by a Crossfilter group function?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can i use jsx instead of js extension in react boilerplate?

From Dev

Can I add an iterator to the reduce function?

From Dev

Can I add an iterator to the reduce function?

From Dev

how can i return a dataframe from a function

From Dev

how can i return the count from a function

From Dev

How to reduce react-redux boilerplate - I try creating a ComponentFactory but I'm getting react-redux errors

From Dev

How can I reduce the size of my repository?

From Dev

How can I reduce the duplicated use of if and else

From Dev

How can I reduce the size of the shapes in visio?

From Dev

How can I reduce this long list of if statements?

From Java

how can i reduce repeated code in laravel?

From Dev

How can I reduce startup time for vim?

From Dev

How can I improve this sum with reduce method

From Dev

How can I reduce code duplication in this JavaScript?

From Dev

How can I reduce elements in object?

From Dev

How can I reduce the size of a response to an HttpWebRequest

From Dev

How can I reduce the volume of DenyHosts emails?

From Dev

How can I implement partial reduce in scala?

From Dev

How can I reduce code duplication in this JavaScript?

From Dev

How can I reduce the number of TTYs?

From Dev

How can I reduce the duplicated use of if and else

Related Related

  1. 1

    How can I reduce boilerplate from this function?

  2. 2

    Too much boilerplate, how can I reduce my POJO builders?

  3. 3

    How to reduce boilerplate with Kleisli

  4. 4

    How to reduce boilerplate provisioning in modules?

  5. 5

    Can I remove spaces from a property name inside a reduce function?

  6. 6

    How can I reduce the amount of buckets created by a Crossfilter group function?

  7. 7

    How can I define the return type of a lodash reduce function with Typescript?

  8. 8

    How can I define the return type of a lodash reduce function with Typescript?

  9. 9

    How can i use jsx instead of js extension in react boilerplate?

  10. 10

    Can I add an iterator to the reduce function?

  11. 11

    Can I add an iterator to the reduce function?

  12. 12

    how can i return a dataframe from a function

  13. 13

    how can i return the count from a function

  14. 14

    How to reduce react-redux boilerplate - I try creating a ComponentFactory but I'm getting react-redux errors

  15. 15

    How can I reduce the size of my repository?

  16. 16

    How can I reduce the duplicated use of if and else

  17. 17

    How can I reduce the size of the shapes in visio?

  18. 18

    How can I reduce this long list of if statements?

  19. 19

    how can i reduce repeated code in laravel?

  20. 20

    How can I reduce startup time for vim?

  21. 21

    How can I improve this sum with reduce method

  22. 22

    How can I reduce code duplication in this JavaScript?

  23. 23

    How can I reduce elements in object?

  24. 24

    How can I reduce the size of a response to an HttpWebRequest

  25. 25

    How can I reduce the volume of DenyHosts emails?

  26. 26

    How can I implement partial reduce in scala?

  27. 27

    How can I reduce code duplication in this JavaScript?

  28. 28

    How can I reduce the number of TTYs?

  29. 29

    How can I reduce the duplicated use of if and else

HotTag

Archive