forked from ardanlabs/gotraining
/
example2.go
67 lines (50 loc) · 1.81 KB
/
example2.go
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
59
60
61
62
63
64
65
66
67
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// This is an example that removes the interface pollution by
// removing the interface and using the concrete type directly.
package main
// Server is our Server implementation.
type Server struct {
host string
// PRETEND THERE ARE MORE FIELDS.
}
// NewServer returns an interface value of type Server
// with a server implementation.
func NewServer(host string) *Server {
// SMELL - Storing an unexported type pointer in the interface.
return &Server{host}
}
// Start allows the server to begin to accept requests.
func (s *Server) Start() error {
// PRETEND THERE IS A SPECIFIC IMPLEMENTATION.
return nil
}
// Stop shuts the server down.
func (s *Server) Stop() error {
// PRETEND THERE IS A SPECIFIC IMPLEMENTATION.
return nil
}
// Wait prevents the server from accepting new connections.
func (s *Server) Wait() error {
// PRETEND THERE IS A SPECIFIC IMPLEMENTATION.
return nil
}
func main() {
// Create a new Server.
srv := NewServer("localhost")
// Use the API.
srv.Start()
srv.Stop()
srv.Wait()
}
// =============================================================================
// NOTES:
// Here are some guidelines around interface pollution:
// * Use an interface:
// * When users of the API need to provide an implementation detail.
// * When API’s have multiple implementations that need to be maintained.
// * When parts of the API that can change have been identified and require decoupling.
// * Question an interface:
// * When its only purpose is for writing testable API’s (write usable API’s first).
// * When it’s not providing support for the API to decouple from change.
// * When it's not clear how the interface makes the code better.