SHARE
TWEET

Trader decorator

YaroslavPodorvanov Feb 5th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. type CreateAttempt struct {
  4.     // ...
  5. }
  6.  
  7. type CancelAttempt struct {
  8.     // ...
  9. }
  10.  
  11. type MoveAttempt struct {
  12.     // ...
  13. }
  14.  
  15. type Stats struct {
  16. }
  17.  
  18. type Trader interface {
  19.     CreateOrder(CreateAttempt)
  20.     CancelOrder(CancelAttempt)
  21.     MoveOrder(MoveAttempt)
  22.     Stats() Stats
  23. }
  24.  
  25. type Queue interface {
  26.     Add(CreateAttempt)
  27.     Iterator() <-chan CreateAttempt
  28. }
  29.  
  30. type loggerTraderDecorator struct {
  31.     Trader
  32. }
  33.  
  34. func (l *loggerTraderDecorator) CreateOrder(attempt CreateAttempt) {
  35.     // logging receive create
  36.  
  37.     l.Trader.CreateOrder(attempt)
  38. }
  39.  
  40. func (l *loggerTraderDecorator) CancelOrder(attempt CancelAttempt) {
  41.     // logging receive cancel
  42.  
  43.     l.Trader.CancelOrder(attempt)
  44. }
  45.  
  46. func (l *loggerTraderDecorator) MoveOrder(attempt MoveAttempt) {
  47.     // logging receive move
  48.  
  49.     l.Trader.MoveOrder(attempt)
  50. }
  51.  
  52. func WrapLoggerDecorator(source Trader) Trader {
  53.     return &loggerTraderDecorator{source}
  54. }
  55.  
  56. type queueTraderDecorator struct {
  57.     Trader
  58.     queue Queue
  59. }
  60.  
  61. func (q *queueTraderDecorator) CreateOrder(attempt CreateAttempt) {
  62.     q.queue.Add(attempt)
  63. }
  64.  
  65. func (q *queueTraderDecorator) run() {
  66.     for attempt := range q.queue.Iterator() {
  67.         q.Trader.CreateOrder(attempt)
  68.     }
  69. }
  70.  
  71. func WrapQueueDecorator(source Trader) Trader {
  72.     queueDecorated := &queueTraderDecorator{
  73.         source,
  74.         nil, //new(Queue),
  75.     }
  76.  
  77.     go queueDecorated.run()
  78.  
  79.     return queueDecorated
  80. }
  81.  
  82. type hitTrader struct {
  83. }
  84.  
  85. func (hitTrader) CreateOrder(CreateAttempt) {
  86.     panic("implement me")
  87. }
  88.  
  89. func (hitTrader) CancelOrder(CancelAttempt) {
  90.     panic("implement me")
  91. }
  92.  
  93. func (hitTrader) MoveOrder(MoveAttempt) {
  94.     panic("implement me")
  95. }
  96.  
  97. func (hitTrader) Stats() Stats {
  98.     panic("implement me")
  99. }
  100.  
  101. func newTrader(
  102. // ... client connection
  103. ) *hitTrader {
  104.     return &hitTrader{}
  105. }
  106.  
  107. func main() {
  108.     trader := WrapLoggerDecorator(WrapQueueDecorator(newTrader(
  109.     // ...
  110.     )))
  111.  
  112.     _ = trader
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top