-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.go
46 lines (33 loc) · 1.01 KB
/
adapter.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
package main
import "fmt"
/*
Convert the interface of a class to another interface that the client wants.
The adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces.
Client--->requests for sth.--->Adapter converts request to Service language--->Service processes request.
*/
type aPlayer interface {
PlayMusic(file string)
}
type MP3Player struct{}
func (p *MP3Player) PlayMusic(file string) {
fmt.Printf("MP3 is playing music %s\n", file)
}
type GameSoundPlayer struct{}
func (g *GameSoundPlayer) PlayGameSound(file string) {
fmt.Printf("GameSoundPlayer is playing %s\n", file)
}
type SoundAdapter struct {
GameSoundPlayer
}
func (a *SoundAdapter) PlayMusic(file string) {
fmt.Printf("convert music file into GameSoundPlayer file\n")
a.GameSoundPlayer.PlayGameSound(file)
}
func Play(p aPlayer, file string) {
p.PlayMusic(file)
}
func RunAdapter() {
musicFile := "grow.mp3"
Play(&MP3Player{}, musicFile)
Play(&SoundAdapter{}, musicFile)
}