-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver_null.go
More file actions
58 lines (46 loc) · 1.51 KB
/
observer_null.go
File metadata and controls
58 lines (46 loc) · 1.51 KB
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
52
53
54
55
56
57
58
package observer
import (
"context"
"fmt"
)
// ----------------------------------------------------------------------------
// Types
// ----------------------------------------------------------------------------
// NullObserver is a simple example of the Subject interface.
// It is mainly used for testing.
type NullObserver struct {
ID string
IsSilent bool
}
// ----------------------------------------------------------------------------
// Interface methods
// ----------------------------------------------------------------------------
/*
The GetObserverID method returns the unique identifier of the observer.
Use by the subject to manage the list of Observers.
Input
- ctx: A context to control lifecycle.
*/
func (observer *NullObserver) GetObserverID(ctx context.Context) string {
_ = ctx
return observer.ID
}
/*
The UpdateObserver method processes the message sent by the Subject.
The subject invokes UpdateObserver as a goroutine.
Input
- ctx: A context to control lifecycle.
- message: The string to propagate to all registered Observers.
*/
func (observer *NullObserver) UpdateObserver(ctx context.Context, message string) {
_ = ctx
if !observer.IsSilent {
outputf("Observer: %s; Message: %s\n", observer.ID, message)
}
}
// ----------------------------------------------------------------------------
// Private functions
// ----------------------------------------------------------------------------
func outputf(format string, message ...any) {
fmt.Printf(format, message...) //nolint
}