473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

command pattern variable number of parameters in execute method

oll3i
679 512MB
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?
Apr 14 '07 #1
14 6639
JosAH
11,448 Expert 8TB
i want to write a bank account programme and use a command pattern where execute method has variable number of parameters.


Expand|Select|Wrap|Line Numbers
  1. public interface Command {
  2.      public abstract void execute (String ...  s);
  3. }
  4.  
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
Apr 14 '07 #2
oll3i
679 512MB
so i'm not on a good path?
what's wrong with my Command?
Apr 14 '07 #3
JosAH
11,448 Expert 8TB
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
Apr 14 '07 #4
oll3i
679 512MB
the number of Strings is depending on which Command is
to be executed i think ?

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class WplacnaKonto implements Command { 
  3. // class for putting the money into account
  4. private Konto konto = new Konto();  // Account account = new Account();
  5.  
  6. public WplacnaKonto(Konto k){
  7.     konto=k;    
  8. }
  9.  
  10.  
  11. public void execute(String ... rozkaz){
  12.     double suma = Double.valueOf(rozkaz[0].trim()).doubleValue();
  13.      konto.wplata(suma); //account put the money  into account
  14. }
  15.  
  16. }
  17.  
this is a class responsible for putting the money into the account
i was following the example from this tutorial
http://www.javaworld.com/javaworld/j...68.html?page=1

kind regards
Apr 14 '07 #5
JosAH
11,448 Expert 8TB
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
Apr 14 '07 #6
oll3i
679 512MB
uups ... every Command can take a variable number
of Strings

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



putintoaccount.execute("100.00");
displayhistoryofaccounts.execute("3810209876543456 ","3810204455667876");

and i dont know if am doing it right ?
Apr 14 '07 #7
JosAH
11,448 Expert 8TB
uups ... every Command can take a variable number
of Strings

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



putintoaccount.execute("100.00");
displayhistoryofaccounts.execute("3810209876543456 ","3810204455667876");

and i dont know if am doing it right ?
Not really because your scenario allows me to do this:
Expand|Select|Wrap|Line Numbers
  1. putintoaccount.execute("foo", "bar", "baz");
  2. 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
Apr 15 '07 #8
oll3i
679 512MB
so what would you suggest me to do?
Apr 15 '07 #9
oll3i
679 512MB
should i change my command execute method?
Apr 15 '07 #10
JosAH
11,448 Expert 8TB
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
Apr 15 '07 #11
oll3i
679 512MB
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Deposit implements Command {
  3. private Account account = new Account();
  4.  
  5. public Deposit(Account a){
  6.     account=a;    
  7. }
  8.  
  9.  
  10. public void execute(String... rozkaz){
  11.     double sum = Double.valueOf(rozkaz[0].trim()).doubleValue();
  12.  
  13.     account.deposit(sum);
  14. }
  15.  
  16. }
  17.  
  18.  
and another class responsible for withdrawal
i call account.withdrawal(sum) from that class execute method



Expand|Select|Wrap|Line Numbers
  1. public class Withdrawal implements Command {
  2. private Account account= new Account();
  3.  
  4. public Withdrawal( Account a){
  5.     account=a;    
  6. }
  7.  
  8. public void execute(String ... rozkaz){
  9.     double sum= Double.valueOf(rozkaz[0].trim()).doubleValue();
  10.      account.withdrawal(sum);
  11. }
  12. }
  13.  
  14.  
  15.  
and then i created a class responsible for displaying the history of the accounts
that was displayhistoryofaccounts.execute("number of the account","number of the account") but you said that's not the way to go
Apr 16 '07 #12
JosAH
11,448 Expert 8TB
and then i created a class responsible for displaying the history of the accounts
that was displayhistoryofaccounts.execute("number of the account","number 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:
Expand|Select|Wrap|Line Numbers
  1. public class Withdrawer implements Command {
  2.    private Account a;
  3.    private double amount;
  4.    public Withdrawer(Account a, double amount) {
  5.       this.a= a;
  6.       this.amount= amount;
  7.    }
  8.    // interface implementation
  9.    public void execute() {
  10.       a.withdraw(amount);
  11.    }
  12. }
Expand|Select|Wrap|Line Numbers
  1. public class Depositer implements Command {
  2.    private Account a;
  3.    private double amount;
  4.    public Depositer(Account a, double amount) {
  5.       this.a= a;
  6.       this.amount= amount;
  7.    }
  8.    // interface implementation
  9.    public void execute() {
  10.       a.deposit(amount);
  11.    }
  12. }
Expand|Select|Wrap|Line Numbers
  1. public interface Command {
  2.    public void execute();
  3. }
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
Apr 16 '07 #13
oll3i
679 512MB
but the requirement is to write it with execute method with variable number of parameters
Apr 16 '07 #14
JosAH
11,448 Expert 8TB
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 ;-)
Apr 16 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Cath B | last post by:
I am pretty sure I am getting a command timeout when execute a SQL procedure that has an output parameter. The code below is in an asp page that is called using RSGetASPObject. I want to be able...
5
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm...
4
by: Tom | last post by:
I want to open a recordset object on an .asp page. When I open the recordset I would like to use a stored procedure that expects a parameter to be passed for the stored procedure. I will then use...
3
by: Samuel R. Neff | last post by:
We're working on implementing a Command Pattern design with our application in order to help facilitate undo/redo. This is fine four user actions we control which basically means menu actions. ...
8
by: Charles Law | last post by:
I am implementing the command pattern in VB.NET, where the commands have been serialised. That is, I have several classes that all inherit from my base Command class, that implements ICommand...
4
by: FluffyCat | last post by:
New on November 29, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Command Pattern. Since you all enjoyed the Visitor Pattern so much yesterday, today I have the Command Pattern...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
8
by: Ben | last post by:
Hi! I already sent this to the ACCESS newsgroup. But since I do not know really which side is really causing the problem, I have decided to send this inquiry to this newsgroup also, if I may....
1
by: alexcpn | last post by:
Hi I have a Command class of type class Command { T* m_objptr; void (T::*method)(); : explicit Command(T* pObj,void (T::*p_method)(),long timeout,const
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.