Command pattern implementation Recently, I have get familiar with Command Pattern implementation. I have a problem where I need to get a choice for user to select what kind of method to execute typing method name or number, which concerns to this concrete method execution. Everything will be OK if I don't need to create separate class for each method as it describes below:

public interface Order {
    void execute ();
}

// Receiver class.
class StockTrade {
    public void buy() {
        System.out.println("You want to buy stocks");
    }
    public void sell() {
        System.out.println("You want to sell stocks ");
    }
}

// Invoker.
class Agent {

    void placeOrder(Order order) {
        order.execute();
    }    
}

//ConcreteCommand Class.
class BuyStockOrder implements Order {

    private StockTrade stock;

    public BuyStockOrder(StockTrade st) {
        stock = st;
    }
    public void execute() {
        stock.buy();
    }
}

//ConcreteCommand Class.
class SellStockOrder implements Order { 
    private StockTrade stock;
    public SellStockOrder (StockTrade st) {
        stock = st;
    }
    public void execute() {
        stock.sell();
    }
}

// Client
public class Client {
    public static void main(String[] args) {
        StockTrade stock = new StockTrade();
        BuyStockOrder buyStockOrder = new BuyStockOrder(stock);
        SellStockOrder sellStockOrder = new SellStockOrder(stock);
        Agent agent = new Agent();

        agent.placeOrder(buyStockOrder ); // Buy Shares
        agent.placeOrder(sellStockOrder ); // Sell Shares
    }
}


How can I avoid too much class creation for each method that it will be also correct and flexible?
I may not be able to review the point you specifically ask for, but I'll go for a general review:


Omit public and abstract keyword from interfaces, it is implicit.
Be consistent with spacing out your syntax. You for example have public abstract void execute ( );, whereas it should be void execute();, some strings in your program also could have spacing fixed.
Don't start instance variables with m_, perhaps this is common practice from another background, but not in Java. You seem to adhere camelCasing elsewhere though.
Make parameters final where possible. It gives compiler errors if you accidentally change the primitive or the reference to an object.
Use generics, in Agent you specify m_ordersQueue as new ArrayList(), you want to use generics. If all types of objects can be stored, then use new ArrayList&lt;Object&gt;(). I see that you have forgotten to declare the type of the variable, using Java 7's Diamond Operator, it would be private final List&lt;Object&gt; ordersQueue = new ArrayList&lt;&gt;();, also notice that I define only List&lt;Object&gt; as type, as you always want to use interfaces to declare types if possible.
Give variables descrptive names, bsc and ssc are not descriptive.
Lastly, I would consider using a Queue&lt;Object&gt; for the ordersQueue in Agent, the name implies that you want a queue implementation, yet you are using a list implemention.


The good news is that you seem to be following quite some paradigms correctly and you understand OOP quite reasonably, yet you do need to improve on some points.