Skip to content

Commit 45cf201

Browse files
committed
Add an example about Command Pattern.
1 parent 5298925 commit 45cf201

File tree

13 files changed

+96
-0
lines changed

13 files changed

+96
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Broker
5+
{
6+
private List<Order> orderList = new ArrayList<Order>();
7+
8+
public void takeOrder(Order order)
9+
{
10+
orderList.add(order);
11+
}
12+
13+
public void placeOrders()
14+
{
15+
for (Order order : orderList)
16+
{
17+
order.execute();
18+
}
19+
orderList.clear();
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class BuyStock implements Order
2+
{
3+
private Stock ibmStock;
4+
5+
public BuyStock(Stock ibmStock)
6+
{
7+
this.ibmStock = ibmStock;
8+
}
9+
10+
public void execute()
11+
{
12+
ibmStock.buy();
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Main
2+
{
3+
public static void main(String[] args)
4+
{
5+
Stock abcStock = new Stock();
6+
7+
BuyStock buyStockOrder = new BuyStock(abcStock);
8+
SellStock sellStockOrder = new SellStock(abcStock);
9+
10+
Broker broker = new Broker();
11+
broker.takeOrder(buyStockOrder);
12+
broker.takeOrder(sellStockOrder);
13+
14+
broker.placeOrders();
15+
}
16+
}

0 commit comments

Comments
 (0)