做多渠道适配。其他语言的时候,基本上都是起个 interface ,实现个抽象然后改抽象。
go 也按这么来了
// 定义个接口
type I interface {
Retrieve(s string) error
}
// 有个抽象实现
type Abstract struct {
}
func (self *Abstract) Retrieve(s string) error {
这里有坑
}
因为是多渠道。但是各个渠道只是 api 地址不一样接口大差不差。但是多多少少有差别,但是行业标准参数名起码都一样。 然后就
type Channel1 struct { *Abstract }
type Channel2 struct { *Abstract }
type Channel3 struct { *Abstract }
func GetChan(s string) {
switch s {
case "1":
return &Channel1{&Abstract{url, apikey, mode:ws, ....}
}
}
今天接了个需求。有 3 个渠道变了,人家比较牛逼,人家自己魔改了协议,需要重新适配。
但是,也只是修改了 2 个参数名和 URL 或者是编码。但是其他都一样。
所以。问题来了。我怎么在 Abstract.Retrieve 获取当前的实例名知道是谁被调用了(获取 str=Channel1) 😂
还是,干脆就重新写,相同的逻辑,尽管 c+v 吧。