File tree Expand file tree Collapse file tree 7 files changed +137
-0
lines changed Expand file tree Collapse file tree 7 files changed +137
-0
lines changed Original file line number Diff line number Diff line change
1
+ package proxydp
2
+
3
+ // AssembleGit 装配(对象实例化和注入)
4
+ type AssembleGit struct {
5
+ }
6
+
7
+ // API 装配层
8
+ func (assemble * AssembleGit ) API () error {
9
+ client := CleintGit {
10
+ IGit : ProxyExtendGit {},
11
+ }
12
+ client .ClientClone ("https://www.baidu.com" )
13
+
14
+ client = CleintGit {
15
+ IGit : ProxyImplGit {},
16
+ }
17
+ client .ClientClone ("https://www.163.com" )
18
+
19
+ return nil
20
+ }
Original file line number Diff line number Diff line change
1
+ package proxydp
2
+
3
+ import (
4
+ "log"
5
+ "testing"
6
+ )
7
+
8
+ func TestAPI (t * testing.T ) {
9
+ err := (& AssembleGit {}).API ()
10
+ if err != nil {
11
+ log .Printf ("New Testflight Success!" )
12
+ } else {
13
+ log .Printf ("New Testflight Failed!" )
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ package proxydp
2
+
3
+ // CleintGit Git的调用者,依赖接口
4
+ type CleintGit struct {
5
+ IGit
6
+ }
7
+
8
+ // ClientClone 高层用户接口层
9
+ func (client * CleintGit ) ClientClone (url string ) error {
10
+ client .IGit .Clone (url )
11
+ return nil
12
+ }
Original file line number Diff line number Diff line change
1
+ package proxydp
2
+
3
+ // IGit git interface
4
+ type IGit interface {
5
+ Clone (url string ) error
6
+ Push (url string ) error
7
+ }
Original file line number Diff line number Diff line change
1
+ package proxydp
2
+
3
+ import (
4
+ "fmt"
5
+ "strings"
6
+ )
7
+
8
+ // Git IGet Implementation
9
+ type Git struct {
10
+ }
11
+
12
+ // Clone 克隆
13
+ func (git * Git ) Clone (url string ) error {
14
+ if strings .HasPrefix (url , "https" ) {
15
+ fmt .Println ("clone from " + url )
16
+ return nil
17
+ }
18
+ fmt .Println ("failed to clone from " + url )
19
+ return fmt .Errorf ("failed to clone from %s" , url )
20
+ }
21
+
22
+ // Push 推送
23
+ func (git * Git ) Push (url string ) error {
24
+ if strings .HasPrefix (url , "https" ) {
25
+ fmt .Println ("Push to " + url )
26
+ return nil
27
+ }
28
+ fmt .Println ("failed Push to " + url )
29
+ return fmt .Errorf ("failed Push to %s" , url )
30
+ }
Original file line number Diff line number Diff line change
1
+ package proxydp
2
+
3
+ import "log"
4
+
5
+ // ProxyExtendGit 继承原始类Git,重写原来的函数
6
+ type ProxyExtendGit struct {
7
+ Git
8
+ }
9
+
10
+ // Clone Override Git{}.Clone()
11
+ func (extend ProxyExtendGit ) Clone (url string ) error {
12
+ log .Println ("StartExtend Git-Clone" )
13
+ // 调用父类
14
+ err := extend .Git .Clone (url )
15
+ log .Println ("EndExtend Git-Clone" )
16
+ return err
17
+ }
18
+
19
+ // Push Override Git{}.PuPush()
20
+ func (extend ProxyExtendGit ) Push (url string ) error {
21
+ log .Println ("StartExtend Git-Push " )
22
+ // 调用父类
23
+ err := extend .Git .Push (url )
24
+ log .Println ("EndExtend Git-Push" )
25
+ return err
26
+ }
Original file line number Diff line number Diff line change
1
+ package proxydp
2
+
3
+ import (
4
+ "log"
5
+ )
6
+
7
+ // ProxyImplGit 与原始类实现相同的接口
8
+ type ProxyImplGit struct {
9
+ }
10
+
11
+ // Clone implementation
12
+ func (git ProxyImplGit ) Clone (url string ) error {
13
+ log .Println ("StartProxy Clone Git" )
14
+ g := Git {}
15
+ err := g .Clone (url )
16
+ log .Println ("EndProxy Clone Git" )
17
+ return err
18
+ }
19
+
20
+ // Push implementation
21
+ func (git ProxyImplGit ) Push (url string ) error {
22
+ log .Println ("StartProxy Push Git" )
23
+ g := Git {}
24
+ err := g .Clone (url )
25
+ log .Println ("EndProxy Push Git" )
26
+ return err
27
+ }
You can’t perform that action at this time.
0 commit comments