Skip to content

Commit 96200b8

Browse files
Code for chapter 10.
1 parent d849e6b commit 96200b8

7 files changed

Lines changed: 517 additions & 254 deletions

File tree

‎chapter10/listing01/listing01.go‎

Lines changed: 82 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,122 @@
11
// Sample program demonstrating struct composition.
22
package main
33

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")
519

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
1023
}
1124

1225
// =============================================================================
1326

14-
// Mallet is a tool that pounds in nails.
15-
type Mallet struct{}
27+
// Xenia is a system we need to pull data from.
28+
type Xenia struct{}
1629

17-
// DriveNail pounds a nail into the specified board.
18-
func (Mallet) DriveNail(nailSupply *int, b *Board) {
19-
*nailSupply--
20-
b.NailsDriven++
21-
fmt.Println("Mallet: pounded nail into the board.")
30+
// Pull knows how to pull data out of Xenia.
31+
func (Xenia) Pull(d *Data) error {
32+
switch rand.Intn(10) {
33+
case 1, 9:
34+
return EOD
35+
36+
case 5:
37+
return errors.New("Error reading data from Xenia")
38+
39+
default:
40+
d.Line = "Data"
41+
fmt.Println("In:", d.Line)
42+
return nil
43+
}
2244
}
2345

24-
// Crowbar is a tool that removes nails.
25-
type Crowbar struct{}
46+
// Pillar is a system we need to store data into.
47+
type Pillar struct{}
2648

27-
// PullNail yanks a nail out of the specified board.
28-
func (Crowbar) PullNail(nailSupply *int, b *Board) {
29-
b.NailsDriven--
30-
*nailSupply++
31-
fmt.Println("Crowbar: yanked nail out of the board.")
49+
// Store knows how to store data into Pillar.
50+
func (Pillar) Store(d Data) error {
51+
fmt.Println("Out:", d.Line)
52+
return nil
3253
}
3354

3455
// =============================================================================
3556

36-
// Toolbox can contains a Mallet and a Crowbar.
37-
type Toolbox struct {
38-
Mallet
39-
Crowbar
40-
41-
nails int
57+
// System wraps Xenia and Pillar together into a single system.
58+
type System struct {
59+
Xenia
60+
Pillar
4261
}
4362

4463
// =============================================================================
4564

46-
// Contractor carries out the task of securing boards.
47-
type Contractor struct{}
65+
// IO provides support to copy bulk data.
66+
type IO struct{}
4867

49-
// Fasten will drive nails into a board.
50-
func (Contractor) Fasten(m Mallet, nailSupply *int, b *Board) {
51-
for b.NailsDriven < b.NailsNeeded {
52-
m.DriveNail(nailSupply, b)
68+
// pull knows how to pull bulks of data from Xenia.
69+
func (IO) pull(x *Xenia, data []Data) (int, error) {
70+
for i := range data {
71+
if err := x.Pull(&data[i]); err != nil {
72+
return i, err
73+
}
5374
}
75+
76+
return len(data), nil
5477
}
5578

56-
// Unfasten will remove nails from a board.
57-
func (Contractor) Unfasten(cb Crowbar, nailSupply *int, b *Board) {
58-
for b.NailsDriven > b.NailsNeeded {
59-
cb.PullNail(nailSupply, b)
79+
// store knows how to store bulks of data from Pillar.
80+
func (IO) store(p *Pillar, data []Data) error {
81+
for _, d := range data {
82+
if err := p.Store(d); err != nil {
83+
return err
84+
}
6085
}
61-
}
6286

63-
// ProcessBoards works against boards.
64-
func (c Contractor) ProcessBoards(tb *Toolbox, nailSupply *int, boards []Board) {
65-
for i := range boards {
66-
b := &boards[i]
87+
return nil
88+
}
6789

68-
fmt.Printf("Contractor: examining board #%d: %+v\n", i+1, b)
90+
// Copy knows how to pull and store data from the System.
91+
func (io IO) Copy(sys *System, batch int) error {
92+
for {
93+
data := make([]Data, batch)
6994

70-
switch {
71-
case b.NailsDriven < b.NailsNeeded:
72-
c.Fasten(tb.Mallet, nailSupply, b)
95+
i, err := io.pull(&sys.Xenia, data)
96+
if i > 0 {
97+
if err := io.store(&sys.Pillar, data[:i]); err != nil {
98+
return err
99+
}
100+
}
73101

74-
case b.NailsDriven > b.NailsNeeded:
75-
c.Unfasten(tb.Crowbar, nailSupply, b)
102+
if err != nil {
103+
return err
76104
}
77105
}
78106
}
79107

80108
// =============================================================================
81109

82-
// main is the entry point for the application.
83110
func main() {
84-
boards := []Board{
85-
{NailsDriven: 3},
86-
{NailsNeeded: 2},
87-
}
88111

89-
tb := Toolbox{
90-
Mallet: Mallet{},
91-
Crowbar: Crowbar{},
92-
nails: 10,
112+
// Initialize the system for use.
113+
sys := System{
114+
Xenia: Xenia{},
115+
Pillar: Pillar{},
93116
}
94117

95-
var c Contractor
96-
c.ProcessBoards(&tb, &tb.nails, boards)
118+
var io IO
119+
if err := io.Copy(&sys, 3); err != EOD {
120+
fmt.Println(err)
121+
}
97122
}

‎chapter10/listing02/listing02.go‎

Lines changed: 88 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,134 @@
11
// Sample program demonstrating decoupling with interfaces.
22
package main
33

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")
519

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
1023
}
1124

1225
// =============================================================================
1326

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
1730
}
1831

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
2235
}
2336

2437
// =============================================================================
2538

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{}
2841

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+
}
3456
}
3557

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{}
3860

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
4465
}
4566

4667
// =============================================================================
4768

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
5473
}
5574

5675
// =============================================================================
5776

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{}
6079

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+
}
6586
}
87+
88+
return len(data), nil
6689
}
6790

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+
}
7297
}
73-
}
7498

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+
}
79101

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)
81106

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+
}
85113

86-
case b.NailsDriven > b.NailsNeeded:
87-
c.Unfasten(tb.Crowbar, nailSupply, b)
114+
if err != nil {
115+
return err
88116
}
89117
}
90118
}
91119

92120
// =============================================================================
93121

94-
// main is the entry point for the application.
95122
func main() {
96-
boards := []Board{
97-
{NailsDriven: 3},
98-
{NailsNeeded: 2},
99-
}
100123

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{},
105128
}
106129

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+
}
109134
}

0 commit comments

Comments
 (0)