File tree Expand file tree Collapse file tree 1 file changed +30
-3
lines changed Expand file tree Collapse file tree 1 file changed +30
-3
lines changed Original file line number Diff line number Diff line change 1- # interface
2-
3- ## iface 和 eface
1+ # iface 和 eface
42
53Go 使用 iface 和 eface 来表达 interface。iface 其实就是 interface,这种接口内部是有函数的。
64
@@ -35,17 +33,21 @@ type eface struct {
3533所以我们可以比较容易地推断出来下面两种 interface 在 Go 内部是怎么表示的:
3634
3735iface:
36+
3837``` go
3938type Reader interface {
4039 Read ([]byte ) (int , error )
4140}
4241```
4342
4443eface:
44+
4545``` go
4646var 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 实现原理
You can’t perform that action at this time.
0 commit comments