Skip to content

Commit 957832b

Browse files
committed
add facade pattern
1 parent 0e56780 commit 957832b

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

.gitignore

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
### https://raw.github.com/github/gitignore/23aad6abc4ed1aafb425f6e09334c4e35ad146ac/Global/Emacs.gitignore
2+
3+
# -*- mode: gitignore; -*-
4+
*~
5+
\#*\#
6+
/.emacs.desktop
7+
/.emacs.desktop.lock
8+
*.elc
9+
auto-save-list
10+
tramp
11+
.\#*
12+
13+
# Org-mode
14+
.org-id-locations
15+
*_archive
16+
17+
# flymake-mode
18+
*_flymake.*
19+
20+
# eshell files
21+
/eshell/history
22+
/eshell/lastdir
23+
24+
# elpa packages
25+
/elpa/
26+
27+
# reftex files
28+
*.rel
29+
30+
# AUCTeX auto folder
31+
/auto/
32+
33+
# cask packages
34+
.cask/
35+
36+
37+
### https://raw.github.com/github/gitignore/23aad6abc4ed1aafb425f6e09334c4e35ad146ac/Go.gitignore
38+
39+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
40+
*.o
41+
*.a
42+
*.so
43+
44+
# Folders
45+
_obj
46+
_test
47+
48+
# Architecture specific extensions/prefixes
49+
*.[568vq]
50+
[568vq].out
51+
52+
*.cgo1.go
53+
*.cgo2.c
54+
_cgo_defun.c
55+
_cgo_gotypes.go
56+
_cgo_export.*
57+
58+
_testmain.go
59+
60+
*.exe
61+
*.test
62+
*.prof
63+
64+
-3.27 MB
Binary file not shown.

01_facade/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 外观模式
2+
3+
API 为facade 模块的外观接口,大部分代码使用此接口简化对facade类的访问。
4+
5+
facade模块同时暴露了a和b 两个Module 的NewXXX和interface,其它代码如果需要使用细节功能时可以直接调用。

01_facade/facade.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package facade
2+
3+
import "fmt"
4+
5+
func NewAPI() API {
6+
return &apiImpl{
7+
a: NewAModuleAPI(),
8+
b: NewBModuleAPI(),
9+
}
10+
}
11+
12+
//API is facade interface of facade package
13+
type API interface {
14+
Test() string
15+
}
16+
17+
//facade implement
18+
type apiImpl struct {
19+
a AModuleAPI
20+
b BModuleAPI
21+
}
22+
23+
func (a *apiImpl) Test() string {
24+
aRet := a.a.TestA()
25+
bRet := a.b.TestB()
26+
return fmt.Sprintf("%s\n%s", aRet, bRet)
27+
}
28+
29+
//NewAModuleAPI return new AModuleAPI
30+
func NewAModuleAPI() AModuleAPI {
31+
return &aModuleImpl{}
32+
}
33+
34+
//AModuleAPI ...
35+
type AModuleAPI interface {
36+
TestA() string
37+
}
38+
39+
type aModuleImpl struct{}
40+
41+
func (*aModuleImpl) TestA() string {
42+
return "A module running"
43+
}
44+
45+
//NewBModuleAPI return new BModuleAPI
46+
func NewBModuleAPI() BModuleAPI {
47+
return &bModuleImpl{}
48+
}
49+
50+
//BModuleAPI ...
51+
type BModuleAPI interface {
52+
TestB() string
53+
}
54+
55+
type bModuleImpl struct{}
56+
57+
func (*bModuleImpl) TestB() string {
58+
return "B module running"
59+
}

01_facade/facade_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package facade
2+
3+
import "testing"
4+
5+
var expect = "A module running\nB module running"
6+
7+
// TestFacadeAPI ...
8+
func TestFacadeAPI(t *testing.T) {
9+
api := NewAPI()
10+
ret := api.Test()
11+
if ret != expect {
12+
t.Fatalf("expect %s, return %s", expect, ret)
13+
}
14+
}

0 commit comments

Comments
 (0)