Skip to content

Commit a2cef14

Browse files
committed
proxy
0 parents  commit a2cef14

File tree

7 files changed

+137
-0
lines changed

7 files changed

+137
-0
lines changed

proxydp/assemble.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

proxydp/assemble_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

proxydp/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

proxydp/interface.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package proxydp
2+
3+
// IGit git interface
4+
type IGit interface {
5+
Clone(url string) error
6+
Push(url string) error
7+
}

proxydp/origin.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

proxydp/proxy_extend.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

proxydp/proxy_impl.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)