This program use a stack object (used as LIFO or FIFO). Push 1 place 1 to top of stack. Data 1 place 1 to bottom of stack. Read A reads always from top of stack.
So in this program if we change Push with Data in methods placeOrder and placeOrderFromQueue then those orders which can't placed return to stack to the bottom (so used as queue). Using Push we get stack as LIFO. So at the execution using queue (FIFO) a missing Sell order miss turn and requeue to bottom, changing the order of orders placing. Using Push (as the program have) always the orders placing with the initial order (which use Dara, so as FIFO), but if a Sell order can't placed then the program wait until that order placed, and then continue to next order (until the orders queue get empty)
\\ command pattern
class Order {
function execute {
error "abstract"
}
}
\\ receiver class
class StockTrade {
module buy {
print "You want to buy stocks"
}
module sell {
print "You want to sell stocks "
}
}
\\ Invoker
class Agent {
private:
ordersQueue=stack
public:
module placeOrder (order as *Order) {
Stack .ordersQueue {
Data order
Read order1
if not order1=>execute() then push order1
}
}
function placeOrderFromQueue {
\\ false means exit from outer loop
Stack .ordersQueue {
if empty then exit
=true
Read order1
if not order1=>execute() then push order1
if empty then =false
}
}
}
\\ ConcreteCommand Class
class BuyStockOrder as Order {
private:
StockTrade stock
public:
function execute() {
.stock.buy
=true
}
class:
module BuyStockOrder (aStock as StockTrade) {
.stock<=aStock
}
}
\\ ConcreteCommand Class
class SellStockOrder as Order {
private:
StockTrade stock
public:
function execute {
factor=random(1,10)<3
if factor then .stock.sell : = True else Print "can't sell now, wait"
}
class:
module SellStockOrder (aStock as StockTrade) {
.stock<=aStock
}
}
agent=Agent()
stock=StockTrade()
bsc->BuyStockOrder(stock)
ssc-> SellStockOrder(stock)
agent.placeOrder bsc
agent.placeOrder ssc
while agent.placeOrderFromQueue()
wait 100
end while
class Order {
function execute {
error "abstract"
}
}
\\ receiver class
class StockTrade {
module buy {
print "You want to buy stocks"
}
module sell {
print "You want to sell stocks "
}
}
\\ Invoker
class Agent {
private:
ordersQueue=stack
public:
module placeOrder (order as *Order) {
Stack .ordersQueue {
Data order
Read order1
if not order1=>execute() then push order1
}
}
function placeOrderFromQueue {
\\ false means exit from outer loop
Stack .ordersQueue {
if empty then exit
=true
Read order1
if not order1=>execute() then push order1
if empty then =false
}
}
}
\\ ConcreteCommand Class
class BuyStockOrder as Order {
private:
StockTrade stock
public:
function execute() {
.stock.buy
=true
}
class:
module BuyStockOrder (aStock as StockTrade) {
.stock<=aStock
}
}
\\ ConcreteCommand Class
class SellStockOrder as Order {
private:
StockTrade stock
public:
function execute {
factor=random(1,10)<3
if factor then .stock.sell : = True else Print "can't sell now, wait"
}
class:
module SellStockOrder (aStock as StockTrade) {
.stock<=aStock
}
}
agent=Agent()
stock=StockTrade()
bsc->BuyStockOrder(stock)
ssc-> SellStockOrder(stock)
agent.placeOrder bsc
agent.placeOrder ssc
while agent.placeOrderFromQueue()
wait 100
end while
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
You can feel free to write any suggestion, or idea on the subject.