forked from go/golangs_learn
VivimanZhang
4 years ago
8 changed files with 159 additions and 0 deletions
@ -0,0 +1,41 @@ |
|||
package builder |
|||
|
|||
type Builder interface { |
|||
Build() |
|||
} |
|||
|
|||
func NewDirector(b Builder) Director { |
|||
return Director{build: b} |
|||
} |
|||
|
|||
type Director struct { |
|||
build Builder |
|||
} |
|||
|
|||
func (d *Director) Construct() { |
|||
d.build.Build() |
|||
} |
|||
|
|||
type ConcreateBuilder struct { |
|||
Built bool |
|||
} |
|||
|
|||
func NewConcreateBuilder() ConcreateBuilder { |
|||
return ConcreateBuilder{Built: false} |
|||
} |
|||
|
|||
func (b *ConcreateBuilder) Build() { |
|||
b.Built = true |
|||
} |
|||
|
|||
type Product struct { |
|||
Built bool |
|||
} |
|||
|
|||
func NewProduct() Product { |
|||
return Product{Built: false} |
|||
} |
|||
|
|||
func (b *ConcreateBuilder) GetResult() Product { |
|||
return Product{b.Built} |
|||
} |
@ -0,0 +1,14 @@ |
|||
package builder |
|||
|
|||
import ( |
|||
"fmt" |
|||
"testing" |
|||
) |
|||
|
|||
func TestConcreateBuilder_GetResult(t *testing.T) { |
|||
concreateBuilder := NewConcreateBuilder() |
|||
director := NewDirector(&concreateBuilder) |
|||
director.Construct() |
|||
result := concreateBuilder.GetResult() |
|||
fmt.Println(result) |
|||
} |
@ -0,0 +1,6 @@ |
|||
|
|||
```go |
|||
``` |
|||
* 建造者模式 |
|||
|
|||
* 将整体封装起来,对外仅仅展示一个 调用接口 |
@ -0,0 +1,39 @@ |
|||
package facade |
|||
|
|||
import "fmt" |
|||
|
|||
type CarModel struct { |
|||
} |
|||
|
|||
func NewCarModel() *CarModel { |
|||
return &CarModel{} |
|||
} |
|||
func (c *CarModel) SetModel() { |
|||
fmt.Println("SetModel测试...", c) |
|||
} |
|||
|
|||
type CarEngine struct { |
|||
} |
|||
|
|||
func (c *CarEngine) SetEngine() { |
|||
fmt.Println("SetEngine测试...", c) |
|||
} |
|||
|
|||
type CarBody struct { |
|||
} |
|||
|
|||
func (c *CarBody) SetBody() { |
|||
fmt.Println("SetBody测试...", c) |
|||
} |
|||
|
|||
type CarFacade struct { |
|||
model CarModel |
|||
engine CarEngine |
|||
body CarBody |
|||
} |
|||
|
|||
func (c *CarFacade) CreateCompleteCar() { |
|||
c.model.SetModel() |
|||
c.engine.SetEngine() |
|||
c.body.SetBody() |
|||
} |
@ -0,0 +1,8 @@ |
|||
package facade |
|||
|
|||
import "testing" |
|||
|
|||
func TestCarFacade_CreateCompleteCar(t *testing.T) { |
|||
facade := CarFacade{} |
|||
facade.CreateCompleteCar() |
|||
} |
@ -0,0 +1,48 @@ |
|||
|
|||
```go |
|||
package facade |
|||
|
|||
import "fmt" |
|||
|
|||
type CarModel struct { |
|||
|
|||
} |
|||
|
|||
func NewCarModel() *CarModel { |
|||
return &CarModel{} |
|||
} |
|||
func (c *CarModel) SetModel() { |
|||
fmt.Println("SetModel测试...", c) |
|||
} |
|||
|
|||
type CarEngine struct { |
|||
|
|||
} |
|||
|
|||
func (c *CarEngine) SetEngine() { |
|||
fmt.Println("SetEngine测试...", c) |
|||
} |
|||
|
|||
type CarBody struct { |
|||
|
|||
} |
|||
|
|||
func (c *CarBody) SetBody() { |
|||
fmt.Println("SetBody测试...", c) |
|||
} |
|||
|
|||
type CarFacade struct { |
|||
model CarModel |
|||
engine CarEngine |
|||
body CarBody |
|||
} |
|||
|
|||
func (c *CarFacade) CreateCompleteCar() { |
|||
c.model.SetModel() |
|||
c.engine.SetEngine() |
|||
c.body.SetBody() |
|||
} |
|||
``` |
|||
* 外观模式 |
|||
|
|||
* 将整体封装起来,对外仅仅展示一个 调用接口 |
Loading…
Reference in new issue