command pattern variable number of parameters in execute method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    command pattern variable number of parameters in execute method

    i want to write a bank account programme and use a command pattern where execute method has variable number of parameters.


    [code]
    public interface Command {
    public abstract void execute (String ... s);
    }
    [code]
    how do i get the parameters passed to that method?
    when is the execute method called?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by oll3i
    i want to write a bank account programme and use a command pattern where execute method has variable number of parameters.


    Code:
    public interface Command {
    	 public abstract void execute (String ...  s);
    }
    how do i get the parameters passed to that method?
    when is the execute method called?
    You're in trouble here: other objects are not supposed to know anything at all
    about an actual implementation of the Command interface so they don't know
    how many Strings to pass to that particular implementation. All that the other
    objects know is that they can pass zero or more Strings to your Command,
    that's all. I think you have to go back to the drawing board for a bit more time.

    kind regards,

    Jos

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      so i'm not on a good path?
      what's wrong with my Command?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by oll3i
        so i'm not on a good path?
        what's wrong with my Command?
        That's what I wrote in my previous reply, but maybe things are not so bad.
        What do these Strings mean? Can every Command take a variable number
        of Strings or are the number of Strings depending on *which* Command is
        to be executed?

        kind regards,

        Jos

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #5
          the number of Strings is depending on which Command is
          to be executed i think ?

          Code:
          public class WplacnaKonto implements Command { 
          // class for putting the money into account
          private Konto konto = new Konto();  // Account account = new Account();
          
          public WplacnaKonto(Konto k){
          	konto=k;	
          }
          
          
          public void execute(String ... rozkaz){
          	double suma = Double.valueOf(rozkaz[0].trim()).doubleValue();
          	 konto.wplata(suma); //account put the money  into account
          }
          
          }
          this is a class responsible for putting the money into the account
          i was following the example from this tutorial


          kind regards

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by oll3i
            the number of Strings is depending on which Command is
            to be executed i think ?
            Then there's trouble ahead; suppose you have three different commands: A, B
            and C all implementing your Command interface. Command A needs 2 Strings,
            Command B needs 1 String and Command C needs 4 Strings. Your other
            objects are not supposed to know which Command they're dealing with. How
            should they know how many Strings to pass to a Command?

            kind regards,

            Jos

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              uups ... every Command can take a variable number
              of Strings

              public void execute(String ... rozkaz){
              double suma = Double.valueOf( rozkaz[0].trim()).double Value();
              konto.wplata(su ma);
              }



              putintoaccount. execute("100.00 ");
              displayhistoryo faccounts.execu te("38102098765 43456","3810204 455667876");

              and i dont know if am doing it right ?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by oll3i
                uups ... every Command can take a variable number
                of Strings

                public void execute(String ... rozkaz){
                double suma = Double.valueOf( rozkaz[0].trim()).double Value();
                konto.wplata(su ma);
                }



                putintoaccount. execute("100.00 ");
                displayhistoryo faccounts.execu te("38102098765 43456","3810204 455667876");

                and i dont know if am doing it right ?
                Not really because your scenario allows me to do this:
                Code:
                putintoaccount.execute("foo", "bar", "baz");
                displayhistoryofaccounts.execute("100.00", "-42", "Fido the Poodle");
                In other words: I don't know to which Command I'm talking and I don't know
                what to put as parameter values. All I know is that I'm talking to a Command
                that takes zero or more Strings as its parameter list.

                kind regards,

                Jos

                Comment

                • oll3i
                  Contributor
                  • Mar 2007
                  • 679

                  #9
                  so what would you suggest me to do?

                  Comment

                  • oll3i
                    Contributor
                    • Mar 2007
                    • 679

                    #10
                    should i change my command execute method?

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by oll3i
                      should i change my command execute method?
                      Something like that; I don't think you want a Command pattern here, i.e. you
                      want to say different things to different objects and you want to know about
                      the difference of those objects. For an account thing you want to say: gimme
                      all the account details and withdraw or deposit some money. Make those
                      different methods. IMHO you want an Account interface with different methods,
                      one for each purpose. Squeezing everything into one method is not the way to go.

                      kind regards,

                      Jos

                      Comment

                      • oll3i
                        Contributor
                        • Mar 2007
                        • 679

                        #12
                        but my account class has methods for deposit and withdrawal then i created a class responsible for depositing and call account.deposit (sum) from that class execute method

                        Code:
                        public class Deposit implements Command {
                        private Account account = new Account();
                        
                        public Deposit(Account a){
                        	account=a;	
                        }
                        
                        
                        public void execute(String... rozkaz){
                        	double sum = Double.valueOf(rozkaz[0].trim()).doubleValue();
                        	
                        	account.deposit(sum);
                        }
                        
                        }
                        and another class responsible for withdrawal
                        i call account.withdra wal(sum) from that class execute method



                        Code:
                        public class Withdrawal implements Command {
                        private Account account= new Account();
                        
                        public Withdrawal( Account a){
                        	account=a;	
                        }
                        
                        public void execute(String ... rozkaz){
                        	double sum= Double.valueOf(rozkaz[0].trim()).doubleValue();
                        	 account.withdrawal(sum);
                        }
                        }
                        and then i created a class responsible for displaying the history of the accounts
                        that was displayhistoryo faccounts.execu te("number of the account","numbe r of the account") but you said that's not the way to go

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by oll3i
                          and then i created a class responsible for displaying the history of the accounts
                          that was displayhistoryo faccounts.execu te("number of the account","numbe r of the account") but you said that's not the way to go
                          But why do you want to implement a Command class for all those different
                          actions? Those different actions all take a different number of parameters,
                          even different types of parameters.

                          Why hide that fact by using a varargs String ... parameter list. It doesn't buy
                          you anything here. If I want to withdraw money from an account I know what
                          to do: tell the Account to withdraw some money; same with the deposit action.

                          If you really want to hide those details you should encapsulate those details
                          in the Command implementing classes; it'll be quite a bit of overhead:
                          Code:
                          public class Withdrawer implements Command {
                             private Account a;
                             private double amount;
                             public Withdrawer(Account a, double amount) {
                                this.a= a;
                                this.amount= amount;
                             }
                             // interface implementation
                             public void execute() {
                                a.withdraw(amount);
                             }
                          }
                          Code:
                          public class Depositer implements Command {
                             private Account a;
                             private double amount;
                             public Depositer(Account a, double amount) {
                                this.a= a;
                                this.amount= amount;
                             }
                             // interface implementation
                             public void execute() {
                                a.deposit(amount);
                             }
                          }
                          Code:
                          public interface Command {
                             public void execute();
                          }
                          Only then you have abstracted away what actually has been done to accounts
                          but at quite a high cost, i.e. for every account action you have to create a new
                          object with the Account and possibly some money value.

                          Listing all accounts does fit too in that scenario. Possibly a static method in the
                          Account class can do the real job, delegated by another Command class.

                          kind regards,

                          Jos

                          Comment

                          • oll3i
                            Contributor
                            • Mar 2007
                            • 679

                            #14
                            but the requirement is to write it with execute method with variable number of parameters

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by oll3i
                              but the requirement is to write it with execute method with variable number of parameters
                              Well go for it then but I'd say: shoot those requirements and the moron who
                              made them up. The one who makes up requirements is usually not the one
                              who has to implement the inconsistent, ambiguous, incomplete, not well
                              thought out rubbish and waste of precious bytes and trees.

                              Since when do requirement defining business analysts prescribe the patterns
                              to be used anyway?

                              kind regards,

                              Jos ;-)

                              Comment

                              Working...