-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommand.go
51 lines (41 loc) · 781 Bytes
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
)
type Command interface {
Execute()
}
type SomeCommand struct {
message string
}
func (s *SomeCommand) Execute() {
fmt.Println(s.message)
}
type SomeSpecialCommand struct {
message string
}
func (s *SomeSpecialCommand) Execute() {
message := "@" + s.message
fmt.Println(message)
}
type CommandInvoker struct {
queue []Command
processedItems int
}
func (c *CommandInvoker) processQueue() {
for i := range c.queue {
c.queue[i].Execute()
c.processedItems++
}
}
func (c *CommandInvoker) ProcessedItems() int {
return c.processedItems
}
func (c *CommandInvoker) addToQueue(i Command) {
fmt.Println("Appending command")
c.queue = append(c.queue, i)
if len(c.queue) == 3 {
c.processQueue()
c.queue = []Command{}
}
}