Skip to content

Commit cab2063

Browse files
committed
update inter
1 parent 9e4e4bf commit cab2063

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

interface.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# interface
2-
3-
## iface 和 eface
1+
# iface 和 eface
42

53
Go 使用 iface 和 eface 来表达 interface。iface 其实就是 interface,这种接口内部是有函数的。
64

@@ -35,17 +33,21 @@ type eface struct {
3533
所以我们可以比较容易地推断出来下面两种 interface 在 Go 内部是怎么表示的:
3634

3735
iface:
36+
3837
```go
3938
type Reader interface {
4039
Read([]byte) (int, error)
4140
}
4241
```
4342

4443
eface:
44+
4545
```go
4646
var x interface{}
4747
```
4848

49+
## iface
50+
4951
### iface 适用场景
5052

5153
在 Go 语言中,我们可以借助 iface,实现传统 OOP 编程中的 DIP(dependency inversion principle),即依赖反转。
@@ -88,4 +90,29 @@ func CreateOrder(order OrderStruct) {
8890

8991
业务逻辑不应该知道底层的实现,不应该有形如 db 的 tag,不应该知道配置是从 ETCD 中取出的。
9092

93+
```
94+
┌──────────────────────────────────────────────────────────────┐
95+
│ │
96+
│ │
97+
│ business logic │
98+
├────────────────────┬────────────────────┬────────────────────┤
99+
│ configInterface │ storeInterface │ getInterface │
100+
└────────────────────┴────────────────────┴────────────────────┘
101+
▲ ▲ ▲
102+
│ │ │
103+
│ │ │
104+
│ │ │
105+
│ │ │
106+
│ │ │
107+
│ │ │
108+
│ │ │
109+
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
110+
│ etcd Impl │ │ DATABASE │ │ RPC │
111+
└─────────────────┘ └─────────────────┘ └─────────────────┘
112+
```
113+
114+
## eface
115+
91116
### eface 适用场景
117+
118+
### eface 实现原理

0 commit comments

Comments
 (0)