直接上代码。 不解释了。菜鸟中。 大牛可多多指正
```go
package main
import "fmt"
func main() {
a := &LinkmgrImplBuilder{}
b,err := a.
SetSender("1").
SetOm(2).
SetDrInfo(1,2,3).
Build()
if err != nil {
return
}
b.Test()
}
type drinfo struct {
dr1 int32
dr2 int32
dr3 int32
}
type LinkMgr interface {
Test()
}
type linkmgrImpl struct {
drinfo
om int32
sender string
dbp string
}
func (l *linkmgrImpl) Test() {
fmt.Println("建造成功")
fmt.Println(l.dr3)
fmt.Println(l.om)
}
type LinkmgrImplBuilder struct {
lIl linkmgrImpl
}
func (l *LinkmgrImplBuilder) Build() (LinkMgr,error) {
return newLinkMgr(l)
}
func (l *LinkmgrImplBuilder) SetOm(dr1 int32) *LinkmgrImplBuilder {
l.lIl.om = dr1
return l
}
func (l *LinkmgrImplBuilder) SetSender(dr1 string) *LinkmgrImplBuilder {
l.lIl.sender = dr1
return l
}
func (l *LinkmgrImplBuilder) SetDrInfo(dr1, dr2, dr3 int32) *LinkmgrImplBuilder {
l.lIl.dr1 = dr1
l.lIl.dr2 = dr2
l.lIl.dr3 = dr3
return l
}
func newLinkMgr(lBuilder *LinkmgrImplBuilder) (LinkMgr,error) {
if lBuilder.lIl.om == 0 {
return nil,NewErr("cuowu")
}
l := &linkmgrImpl{
drinfo: lBuilder.lIl.drinfo,
om: lBuilder.lIl.om,
sender: lBuilder.lIl.sender,
dbp: lBuilder.lIl.dbp,
}
return l,nil
}
type myerr struct {
info string
}
func (e *myerr)Error()string{
return e.info
}
func NewErr(errinfo string)*myerr{
return &myerr{
info:errinfo,
}
}
```
有疑问加站长微信联系(非本文作者)