Skip to content

Commit d507164

Browse files
authored
Merge pull request #24 from bvwells/mediator
Implement mediator design pattern #13
2 parents d69f990 + 49ca2d5 commit d507164

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Name | Description | Status
6565
[`Command`](./behavioral/command.go) | Encapsulate a request as an object, thereby allowing for the parameterization of clients with different requests, and the queuing or logging of requests. It also allows for the support of undoable operations. | Implemented
6666
[`Interpreter`](./behavioral/interpreter.go) | Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. |
6767
[`Iterator`](./behavioral/iterator.go) | Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. | Implemented
68-
[`Mediator`](./behavioral/mediator.go) | Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it allows their interaction to vary independently. |
68+
[`Mediator`](./behavioral/mediator.go) | Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it allows their interaction to vary independently. | Implemented
6969
[`Memento`](./behavioral/memento.go) | Without violating encapsulation, capture and externalize an object's internal state allowing the object to be restored to this state later. |
7070
[`Observer`](./behavioral/observer.go) | Define a one-to-many dependency between objects where a state change in one object results in all its dependents being notified and updated automatically. | Implemented
7171
[`State`](./behavioral/state.go) | Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. | Implemented

behavioral/mediator.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,73 @@
11
package behavioral
2+
3+
import "fmt"
4+
5+
// WildStallion describes an interface for a Wild Stallion band member.
6+
type WildStallion interface {
7+
SetMediator(mediator Mediator)
8+
}
9+
10+
// Bill describes Bill S. Preston, Esquire.
11+
type Bill struct {
12+
mediator Mediator
13+
}
14+
15+
// SetMediator sets the mediator.
16+
func (b *Bill) SetMediator(mediator Mediator) {
17+
b.mediator = mediator
18+
}
19+
20+
// Respond responds.
21+
func (b *Bill) Respond() {
22+
fmt.Fprintf(outputWriter, "Bill: What?\n")
23+
b.mediator.Communicate("Bill")
24+
}
25+
26+
// Ted describes Ted "Theodore" Logan.
27+
type Ted struct {
28+
mediator Mediator
29+
}
30+
31+
// SetMediator sets the mediator.
32+
func (t *Ted) SetMediator(mediator Mediator) {
33+
t.mediator = mediator
34+
}
35+
36+
// Talk talks through mediator.
37+
func (t *Ted) Talk() {
38+
fmt.Fprintf(outputWriter, "Ted: Bill?\n")
39+
t.mediator.Communicate("Ted")
40+
}
41+
42+
// Respond responds.
43+
func (t *Ted) Respond() {
44+
fmt.Fprintf(outputWriter, "Ted: Strange things are afoot at the Circle K.\n")
45+
}
46+
47+
// Mediator describes the interface for communicating between Wild Stallion band members.
48+
type Mediator interface {
49+
Communicate(who string)
50+
}
51+
52+
// ConcreateMediator describes a mediator between Bill and Ted.
53+
type ConcreateMediator struct {
54+
Bill
55+
Ted
56+
}
57+
58+
// NewMediator creates a new ConcreateMediator.
59+
func NewMediator() *ConcreateMediator {
60+
mediator := &ConcreateMediator{}
61+
mediator.Bill.SetMediator(mediator)
62+
mediator.Ted.SetMediator(mediator)
63+
return mediator
64+
}
65+
66+
// Communicate communicates between Bill and Ted.
67+
func (m *ConcreateMediator) Communicate(who string) {
68+
if who == "Ted" {
69+
m.Bill.Respond()
70+
} else if who == "Bill" {
71+
m.Ted.Respond()
72+
}
73+
}

behavioral/mediator_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package behavioral
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestMediator(t *testing.T) {
11+
12+
bufferOutputWriter := outputWriter
13+
outputWriter = new(bytes.Buffer)
14+
defer func() { outputWriter = bufferOutputWriter }()
15+
16+
mediator := NewMediator()
17+
mediator.Ted.Talk()
18+
19+
assert.Equal(t, "Ted: Bill?\n"+
20+
"Bill: What?\n"+
21+
"Ted: Strange things are afoot at the Circle K.\n", outputWriter.(*bytes.Buffer).String())
22+
}

0 commit comments

Comments
 (0)