|
1 | 1 | // Sample program demonstrating decoupling with interfaces. |
2 | 2 | package main |
3 | 3 |
|
4 | | -import "fmt" |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "math/rand" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func init() { |
| 12 | + rand.Seed(time.Now().UnixNano()) |
| 13 | +} |
| 14 | + |
| 15 | +// ============================================================================= |
| 16 | + |
| 17 | +// EOD represents the end of the data stream. |
| 18 | +var EOD = errors.New("EOD") |
5 | 19 |
|
6 | | -// Board represents a surface we can work on. |
7 | | -type Board struct { |
8 | | - NailsNeeded int |
9 | | - NailsDriven int |
| 20 | +// Data is the structure of the data we are copying. |
| 21 | +type Data struct { |
| 22 | + Line string |
10 | 23 | } |
11 | 24 |
|
12 | 25 | // ============================================================================= |
13 | 26 |
|
14 | | -// NailDriver represents behavior to drive nails into a board. |
15 | | -type NailDriver interface { |
16 | | - DriveNail(nailSupply *int, b *Board) |
| 27 | +// Puller declares behavior for pulling data. |
| 28 | +type Puller interface { |
| 29 | + Pull(d *Data) error |
17 | 30 | } |
18 | 31 |
|
19 | | -// NailPuller represents behavior to remove nails into a board. |
20 | | -type NailPuller interface { |
21 | | - PullNail(nailSupply *int, b *Board) |
| 32 | +// Storer declares behavior for storing data. |
| 33 | +type Storer interface { |
| 34 | + Store(d Data) error |
22 | 35 | } |
23 | 36 |
|
24 | 37 | // ============================================================================= |
25 | 38 |
|
26 | | -// Mallet is a tool that pounds in nails. |
27 | | -type Mallet struct{} |
| 39 | +// Xenia is a system we need to pull data from. |
| 40 | +type Xenia struct{} |
28 | 41 |
|
29 | | -// DriveNail pounds a nail into the specified board. |
30 | | -func (Mallet) DriveNail(nailSupply *int, b *Board) { |
31 | | - *nailSupply-- |
32 | | - b.NailsDriven++ |
33 | | - fmt.Println("Mallet: pounded nail into the board.") |
| 42 | +// Pull knows how to pull data out of Xenia. |
| 43 | +func (Xenia) Pull(d *Data) error { |
| 44 | + switch rand.Intn(10) { |
| 45 | + case 1, 9: |
| 46 | + return EOD |
| 47 | + |
| 48 | + case 5: |
| 49 | + return errors.New("Error reading data from Xenia") |
| 50 | + |
| 51 | + default: |
| 52 | + d.Line = "Data" |
| 53 | + fmt.Println("In:", d.Line) |
| 54 | + return nil |
| 55 | + } |
34 | 56 | } |
35 | 57 |
|
36 | | -// Crowbar is a tool that removes nails. |
37 | | -type Crowbar struct{} |
| 58 | +// Pillar is a system we need to store data into. |
| 59 | +type Pillar struct{} |
38 | 60 |
|
39 | | -// PullNail yanks a nail out of the specified board. |
40 | | -func (Crowbar) PullNail(nailSupply *int, b *Board) { |
41 | | - b.NailsDriven-- |
42 | | - *nailSupply++ |
43 | | - fmt.Println("Crowbar: yanked nail out of the board.") |
| 61 | +// Store knows how to store data into Pillar. |
| 62 | +func (Pillar) Store(d Data) error { |
| 63 | + fmt.Println("Out:", d.Line) |
| 64 | + return nil |
44 | 65 | } |
45 | 66 |
|
46 | 67 | // ============================================================================= |
47 | 68 |
|
48 | | -// Toolbox can contains a Mallet and a Crowbar. |
49 | | -type Toolbox struct { |
50 | | - Mallet |
51 | | - Crowbar |
52 | | - |
53 | | - nails int |
| 69 | +// System wraps Xenia and Pillar together into a single system. |
| 70 | +type System struct { |
| 71 | + Xenia |
| 72 | + Pillar |
54 | 73 | } |
55 | 74 |
|
56 | 75 | // ============================================================================= |
57 | 76 |
|
58 | | -// Contractor carries out the task of securing boards. |
59 | | -type Contractor struct{} |
| 77 | +// IO provides support to copy bulk data. |
| 78 | +type IO struct{} |
60 | 79 |
|
61 | | -// Fasten will drive nails into a board. |
62 | | -func (Contractor) Fasten(d NailDriver, nailSupply *int, b *Board) { |
63 | | - for b.NailsDriven < b.NailsNeeded { |
64 | | - d.DriveNail(nailSupply, b) |
| 80 | +// pull knows how to pull bulks of data from any Puller. |
| 81 | +func (IO) pull(p Puller, data []Data) (int, error) { |
| 82 | + for i := range data { |
| 83 | + if err := p.Pull(&data[i]); err != nil { |
| 84 | + return i, err |
| 85 | + } |
65 | 86 | } |
| 87 | + |
| 88 | + return len(data), nil |
66 | 89 | } |
67 | 90 |
|
68 | | -// Unfasten will remove nails from a board. |
69 | | -func (Contractor) Unfasten(p NailPuller, nailSupply *int, b *Board) { |
70 | | - for b.NailsDriven > b.NailsNeeded { |
71 | | - p.PullNail(nailSupply, b) |
| 91 | +// store knows how to store bulks of data from any Storer. |
| 92 | +func (IO) store(s Storer, data []Data) error { |
| 93 | + for _, d := range data { |
| 94 | + if err := s.Store(d); err != nil { |
| 95 | + return err |
| 96 | + } |
72 | 97 | } |
73 | | -} |
74 | 98 |
|
75 | | -// ProcessBoards works against boards. |
76 | | -func (c Contractor) ProcessBoards(tb *Toolbox, nailSupply *int, boards []Board) { |
77 | | - for i := range boards { |
78 | | - b := &boards[i] |
| 99 | + return nil |
| 100 | +} |
79 | 101 |
|
80 | | - fmt.Printf("Contractor: examining board #%d: %+v\n", i+1, b) |
| 102 | +// Copy knows how to pull and store data from the System. |
| 103 | +func (io IO) Copy(sys *System, batch int) error { |
| 104 | + for { |
| 105 | + data := make([]Data, batch) |
81 | 106 |
|
82 | | - switch { |
83 | | - case b.NailsDriven < b.NailsNeeded: |
84 | | - c.Fasten(tb.Mallet, nailSupply, b) |
| 107 | + i, err := io.pull(&sys.Xenia, data) |
| 108 | + if i > 0 { |
| 109 | + if err := io.store(&sys.Pillar, data[:i]); err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + } |
85 | 113 |
|
86 | | - case b.NailsDriven > b.NailsNeeded: |
87 | | - c.Unfasten(tb.Crowbar, nailSupply, b) |
| 114 | + if err != nil { |
| 115 | + return err |
88 | 116 | } |
89 | 117 | } |
90 | 118 | } |
91 | 119 |
|
92 | 120 | // ============================================================================= |
93 | 121 |
|
94 | | -// main is the entry point for the application. |
95 | 122 | func main() { |
96 | | - boards := []Board{ |
97 | | - {NailsDriven: 3}, |
98 | | - {NailsNeeded: 2}, |
99 | | - } |
100 | 123 |
|
101 | | - tb := Toolbox{ |
102 | | - Mallet: Mallet{}, |
103 | | - Crowbar: Crowbar{}, |
104 | | - nails: 10, |
| 124 | + // Initialize the system for use. |
| 125 | + sys := System{ |
| 126 | + Xenia: Xenia{}, |
| 127 | + Pillar: Pillar{}, |
105 | 128 | } |
106 | 129 |
|
107 | | - var c Contractor |
108 | | - c.ProcessBoards(&tb, &tb.nails, boards) |
| 130 | + var io IO |
| 131 | + if err := io.Copy(&sys, 3); err != EOD { |
| 132 | + fmt.Println(err) |
| 133 | + } |
109 | 134 | } |
0 commit comments