-
The
Receiver
:
public interface Payment {
void apply();
}
-
The
Concrete Receiver
:
-
The
Command
:
public interface Transaction {
void apply();
}
-
The
Concrete Command
:
public class TransactionImpl implements Transaction {
private Payment payment;
public TransactionImpl(final Payment payment) {
this.payment = payment;
}
public void apply() {
payment.apply();
}
}
-
The
Caller
:
public class Operator {
private Transaction transaction;
public void setTransaction(final Transaction transaction) {
this.transaction = transaction;
}
public void apply() {
transaction.apply();
}
}
-
A simple class to test the
Command
design pattern:
public class CommandPatternTest {
public static void main(String[] args) {
// create the payment
Payment payment = new Debit();
// create the transaction
Transaction transaction = new TransactionImpl(payment);
// create the operator
Operator operator = new Operator();
operator.setTransaction(transaction);
// call methods
operator.apply();
}
}