Skip to content

Commit d849e6b

Browse files
author
William Kennedy
committed
Adding new listings for chapter 10.
1 parent dea4814 commit d849e6b

4 files changed

Lines changed: 436 additions & 0 deletions

File tree

‎chapter10/listing01/listing01.go‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Sample program demonstrating struct composition.
2+
package main
3+
4+
import "fmt"
5+
6+
// Board represents a surface we can work on.
7+
type Board struct {
8+
NailsNeeded int
9+
NailsDriven int
10+
}
11+
12+
// =============================================================================
13+
14+
// Mallet is a tool that pounds in nails.
15+
type Mallet struct{}
16+
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.")
22+
}
23+
24+
// Crowbar is a tool that removes nails.
25+
type Crowbar struct{}
26+
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.")
32+
}
33+
34+
// =============================================================================
35+
36+
// Toolbox can contains a Mallet and a Crowbar.
37+
type Toolbox struct {
38+
Mallet
39+
Crowbar
40+
41+
nails int
42+
}
43+
44+
// =============================================================================
45+
46+
// Contractor carries out the task of securing boards.
47+
type Contractor struct{}
48+
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)
53+
}
54+
}
55+
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)
60+
}
61+
}
62+
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]
67+
68+
fmt.Printf("Contractor: examining board #%d: %+v\n", i+1, b)
69+
70+
switch {
71+
case b.NailsDriven < b.NailsNeeded:
72+
c.Fasten(tb.Mallet, nailSupply, b)
73+
74+
case b.NailsDriven > b.NailsNeeded:
75+
c.Unfasten(tb.Crowbar, nailSupply, b)
76+
}
77+
}
78+
}
79+
80+
// =============================================================================
81+
82+
// main is the entry point for the application.
83+
func main() {
84+
boards := []Board{
85+
{NailsDriven: 3},
86+
{NailsNeeded: 2},
87+
}
88+
89+
tb := Toolbox{
90+
Mallet: Mallet{},
91+
Crowbar: Crowbar{},
92+
nails: 10,
93+
}
94+
95+
var c Contractor
96+
c.ProcessBoards(&tb, &tb.nails, boards)
97+
}

‎chapter10/listing02/listing02.go‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Sample program demonstrating decoupling with interfaces.
2+
package main
3+
4+
import "fmt"
5+
6+
// Board represents a surface we can work on.
7+
type Board struct {
8+
NailsNeeded int
9+
NailsDriven int
10+
}
11+
12+
// =============================================================================
13+
14+
// NailDriver represents behavior to drive nails into a board.
15+
type NailDriver interface {
16+
DriveNail(nailSupply *int, b *Board)
17+
}
18+
19+
// NailPuller represents behavior to remove nails into a board.
20+
type NailPuller interface {
21+
PullNail(nailSupply *int, b *Board)
22+
}
23+
24+
// =============================================================================
25+
26+
// Mallet is a tool that pounds in nails.
27+
type Mallet struct{}
28+
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.")
34+
}
35+
36+
// Crowbar is a tool that removes nails.
37+
type Crowbar struct{}
38+
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.")
44+
}
45+
46+
// =============================================================================
47+
48+
// Toolbox can contains a Mallet and a Crowbar.
49+
type Toolbox struct {
50+
Mallet
51+
Crowbar
52+
53+
nails int
54+
}
55+
56+
// =============================================================================
57+
58+
// Contractor carries out the task of securing boards.
59+
type Contractor struct{}
60+
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)
65+
}
66+
}
67+
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)
72+
}
73+
}
74+
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]
79+
80+
fmt.Printf("Contractor: examining board #%d: %+v\n", i+1, b)
81+
82+
switch {
83+
case b.NailsDriven < b.NailsNeeded:
84+
c.Fasten(tb.Mallet, nailSupply, b)
85+
86+
case b.NailsDriven > b.NailsNeeded:
87+
c.Unfasten(tb.Crowbar, nailSupply, b)
88+
}
89+
}
90+
}
91+
92+
// =============================================================================
93+
94+
// main is the entry point for the application.
95+
func main() {
96+
boards := []Board{
97+
{NailsDriven: 3},
98+
{NailsNeeded: 2},
99+
}
100+
101+
tb := Toolbox{
102+
Mallet: Mallet{},
103+
Crowbar: Crowbar{},
104+
nails: 10,
105+
}
106+
107+
var c Contractor
108+
c.ProcessBoards(&tb, &tb.nails, boards)
109+
}

‎chapter10/listing03/listing03.go‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Sample program demonstrating interface composition.
2+
package main
3+
4+
import "fmt"
5+
6+
// Board represents a surface we can work on.
7+
type Board struct {
8+
NailsNeeded int
9+
NailsDriven int
10+
}
11+
12+
// =============================================================================
13+
14+
// NailDriver represents behavior to drive nails into a board.
15+
type NailDriver interface {
16+
DriveNail(nailSupply *int, b *Board)
17+
}
18+
19+
// NailPuller represents behavior to remove nails into a board.
20+
type NailPuller interface {
21+
PullNail(nailSupply *int, b *Board)
22+
}
23+
24+
// NailDrivePuller represents behavior to drive and remove nails into a board.
25+
type NailDrivePuller interface {
26+
NailDriver
27+
NailPuller
28+
}
29+
30+
// =============================================================================
31+
32+
// Mallet is a tool that pounds in nails.
33+
type Mallet struct{}
34+
35+
// DriveNail pounds a nail into the specified board.
36+
func (Mallet) DriveNail(nailSupply *int, b *Board) {
37+
*nailSupply--
38+
b.NailsDriven++
39+
fmt.Println("Mallet: pounded nail into the board.")
40+
}
41+
42+
// Crowbar is a tool that removes nails.
43+
type Crowbar struct{}
44+
45+
// PullNail yanks a nail out of the specified board.
46+
func (Crowbar) PullNail(nailSupply *int, b *Board) {
47+
b.NailsDriven--
48+
*nailSupply++
49+
fmt.Println("Crowbar: yanked nail out of the board.")
50+
}
51+
52+
// =============================================================================
53+
54+
// Toolbox can contains a Mallet and a Crowbar.
55+
type Toolbox struct {
56+
Mallet
57+
Crowbar
58+
59+
nails int
60+
}
61+
62+
// =============================================================================
63+
64+
// Contractor carries out the task of securing boards.
65+
type Contractor struct{}
66+
67+
// Fasten will drive nails into a board.
68+
func (Contractor) Fasten(d NailDriver, nailSupply *int, b *Board) {
69+
for b.NailsDriven < b.NailsNeeded {
70+
d.DriveNail(nailSupply, b)
71+
}
72+
}
73+
74+
// Unfasten will remove nails from a board.
75+
func (Contractor) Unfasten(p NailPuller, nailSupply *int, b *Board) {
76+
for b.NailsDriven > b.NailsNeeded {
77+
p.PullNail(nailSupply, b)
78+
}
79+
}
80+
81+
// ProcessBoards works against boards.
82+
func (c Contractor) ProcessBoards(dp NailDrivePuller, nailSupply *int, boards []Board) {
83+
for i := range boards {
84+
b := &boards[i]
85+
86+
fmt.Printf("Contractor: examining board #%d: %+v\n", i+1, b)
87+
88+
switch {
89+
case b.NailsDriven < b.NailsNeeded:
90+
c.Fasten(dp, nailSupply, b)
91+
92+
case b.NailsDriven > b.NailsNeeded:
93+
c.Unfasten(dp, nailSupply, b)
94+
}
95+
}
96+
}
97+
98+
// =============================================================================
99+
100+
// main is the entry point for the application.
101+
func main() {
102+
boards := []Board{
103+
{NailsDriven: 3},
104+
{NailsNeeded: 2},
105+
}
106+
107+
tb := Toolbox{
108+
Mallet: Mallet{},
109+
Crowbar: Crowbar{},
110+
nails: 10,
111+
}
112+
113+
var c Contractor
114+
c.ProcessBoards(&tb, &tb.nails, boards)
115+
}

0 commit comments

Comments
 (0)