Skip to content

Commit 75279d9

Browse files
committed
add go for composite
1 parent befaa30 commit 75279d9

32 files changed

+596
-8
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package src
2+
3+
import "fmt"
4+
5+
// 实现部件的树枝构件1
6+
type CompanyComposite struct {
7+
Name string
8+
children []OrganizationComponent
9+
}
10+
11+
func (c *CompanyComposite) Add(component OrganizationComponent) {
12+
c.children = append(c.children, component)
13+
}
14+
15+
func (c *CompanyComposite) Remove(component OrganizationComponent) {
16+
// 根据名称或删除成员
17+
for i := 0; i < len(c.children); i++ {
18+
com := c.children[i]
19+
if com.GetName() == component.GetName() {
20+
// 名称相同则从切片移除掉
21+
c.children = append(c.children[:i], c.children[i+1:]...)
22+
break
23+
}
24+
}
25+
}
26+
27+
func (c *CompanyComposite) GetChild(index int) OrganizationComponent {
28+
return c.children[index]
29+
}
30+
31+
func (c *CompanyComposite) SetName(name string) {
32+
c.Name = name
33+
}
34+
35+
func (c *CompanyComposite) GetName() string {
36+
return c.Name
37+
}
38+
39+
func (c *CompanyComposite) Operation() {
40+
fmt.Println("CompanyComposite::Operation() " + c.Name)
41+
for _, component := range c.children {
42+
component.Operation()
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package src
2+
3+
import "fmt"
4+
5+
// 实现部件的树枝构件2
6+
type DepartmentComposite struct {
7+
Name string
8+
children []OrganizationComponent
9+
}
10+
11+
func (d *DepartmentComposite) Add(component OrganizationComponent) {
12+
d.children = append(d.children, component)
13+
}
14+
15+
func (d *DepartmentComposite) Remove(component OrganizationComponent) {
16+
// 根据名称或删除成员
17+
for i := 0; i < len(d.children); i++ {
18+
com := d.children[i]
19+
if com.GetName() == component.GetName() {
20+
// 名称相同则从切片移除掉
21+
d.children = append(d.children[:i], d.children[i+1:]...)
22+
break
23+
}
24+
}
25+
}
26+
27+
func (d *DepartmentComposite) GetChild(index int) OrganizationComponent {
28+
return d.children[index]
29+
}
30+
31+
func (d *DepartmentComposite) SetName(name string) {
32+
d.Name = name
33+
}
34+
35+
func (d *DepartmentComposite) GetName() string {
36+
return d.Name
37+
}
38+
39+
func (d *DepartmentComposite) Operation() {
40+
fmt.Println("DepartmentComposite::Operation() " + d.Name)
41+
for _, component := range d.children {
42+
component.Operation()
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package src
2+
3+
import "fmt"
4+
5+
// 实现部件的叶子节点,叶子节点不能再含有子节点
6+
type EmployeeLeaf struct {
7+
Name string
8+
}
9+
10+
// 叶子节点不能再增加内容
11+
func (e *EmployeeLeaf) Add(component OrganizationComponent) {
12+
fmt.Println("Leaf can't Add.")
13+
}
14+
15+
// 叶子节点没有移除内容
16+
func (e *EmployeeLeaf) Remove(component OrganizationComponent) {
17+
fmt.Println("Leaf can't Remove.")
18+
}
19+
20+
// 叶子节点无获取子节点
21+
func (e *EmployeeLeaf) GetChild(index int) OrganizationComponent {
22+
fmt.Println("Leaf can't GetChild.")
23+
return nil
24+
}
25+
26+
func (e *EmployeeLeaf) SetName(name string) {
27+
e.Name = name
28+
}
29+
30+
func (e *EmployeeLeaf) GetName() string {
31+
return e.Name
32+
}
33+
34+
func (e *EmployeeLeaf) Operation() {
35+
fmt.Println("EmployeeLeaf::Operation() " + e.Name)
36+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package src
2+
3+
// 定义部件接口或抽象类,分支和叶子节点遵循该类约定
4+
type OrganizationComponent interface {
5+
Add(component OrganizationComponent)
6+
7+
Remove(component OrganizationComponent)
8+
9+
GetChild(index int) OrganizationComponent
10+
11+
Operation()
12+
13+
GetName() string
14+
}

composite-pattern/go/src/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module src
2+
3+
go 1.15

composite-pattern/go/test/test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"../src"
7+
)
8+
9+
// main包下的main入口方法
10+
func main() {
11+
fmt.Println("test start:")
12+
/**
13+
* 组合模式依据树形结构来组合对象,用不同组件来构建整体对象。
14+
* 不同组件之间有相同的接口约束,有不同的具体实现。
15+
* 先定义顶级节点,然后陆续加入枝叶节点和叶子节点,这样不断添加,将零散的个体组成一个整体。ss
16+
*/
17+
18+
// 通过组合模型组合了一个部件,分支和节点可以随意增删
19+
var com = &src.CompanyComposite{Name: "西天旅游有限公司"}
20+
var com1 = &src.DepartmentComposite{Name: "总裁办"}
21+
var com2 = &src.DepartmentComposite{Name: "行动队"}
22+
var com3 = &src.DepartmentComposite{Name: "后勤组"}
23+
var leaf1 = &src.EmployeeLeaf{Name: "唐三藏"}
24+
var leaf2 = &src.EmployeeLeaf{Name: "孙悟空"}
25+
var leaf3 = &src.EmployeeLeaf{Name: "猪悟能"}
26+
var leaf4 = &src.EmployeeLeaf{Name: "沙悟净"}
27+
28+
com.Add(com1)
29+
com.Add(com2)
30+
com.Add(com3)
31+
32+
// leaf1属于com1
33+
com1.Add(leaf1)
34+
// leaf2, leaf3属于com2
35+
com2.Add(leaf2)
36+
com2.Add(leaf3)
37+
38+
// 添加再删除
39+
var dept1 = &src.DepartmentComposite{Name: "小分队"}
40+
com2.Add(dept1)
41+
var tmp1 = &src.EmployeeLeaf{Name: "临时工"}
42+
dept1.Add(tmp1)
43+
dept1.Remove(tmp1)
44+
45+
// leaf4属于com3
46+
com3.Add(leaf4)
47+
48+
// 执行全部节点动作
49+
com.Operation()
50+
51+
// 获取某个节点
52+
var employee = com.GetChild(1).GetChild(0)
53+
fmt.Println(employee.GetName())
54+
}
55+
56+
/**
57+
jarry@jarrys-MacBook-Pro go % go build src/*.go
58+
jarry@jarrys-MacBook-Pro go % go run test/test.go
59+
test start:
60+
CompanyComposite::Operation() 西天旅游有限公司
61+
DepartmentComposite::Operation() 总裁办
62+
EmployeeLeaf::Operation() 唐三藏
63+
DepartmentComposite::Operation() 行动队
64+
EmployeeLeaf::Operation() 孙悟空
65+
EmployeeLeaf::Operation() 猪悟能
66+
DepartmentComposite::Operation() 小分队
67+
DepartmentComposite::Operation() 后勤组
68+
EmployeeLeaf::Operation() 沙悟净
69+
孙悟空
70+
*/

decorator-pattern/go/src/Circle.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package src
2+
3+
import "fmt"
4+
5+
// 具体形状实现了基础形状接口
6+
type Circle struct {
7+
}
8+
9+
func (c *Circle) Draw() {
10+
fmt.Println("Circle::Draw()")
11+
}
12+
13+
func (c *Circle) GetName() string {
14+
return "Circle"
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package src
2+
3+
import "fmt"
4+
5+
// 具体装饰器1,实现基础装饰器接口
6+
type RedShapeDecorator struct {
7+
DecoratedShape Shape
8+
}
9+
10+
func (r *RedShapeDecorator) Draw() {
11+
r.DecoratedShape.Draw()
12+
r.SetRedColor(r.DecoratedShape)
13+
}
14+
15+
func (r *RedShapeDecorator) SetRedColor(decoratedShape Shape) {
16+
fmt.Println("RedShapeDecorator::setRedColor() " + decoratedShape.GetName())
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package src
2+
3+
import "fmt"
4+
5+
// 具体装饰器2,实现基础装饰器接口
6+
type ShadowShapeDecorator struct {
7+
DecoratedShape Shape
8+
}
9+
10+
func (s *ShadowShapeDecorator) Draw() {
11+
// 装饰器根据需要是否调用形状的Draw方法
12+
// s.DecoratedShape.Draw()
13+
s.SetShadow(s.DecoratedShape)
14+
}
15+
16+
func (s *ShadowShapeDecorator) SetShadow(decoratedShape Shape) {
17+
fmt.Println("ShadowShapeDecorator::SetShadow() " + decoratedShape.GetName())
18+
}

decorator-pattern/go/src/Shape.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package src
2+
3+
// 基础形状类,可定义公共方法
4+
type Shape interface {
5+
Draw()
6+
GetName() string
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package src
2+
3+
// 抽象装饰器接口
4+
type ShapeDecorator interface {
5+
Draw()
6+
}

decorator-pattern/go/src/Square.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package src
2+
3+
import "fmt"
4+
5+
// 具体形状实现了基础形状接口
6+
type Square struct {
7+
}
8+
9+
func (c *Square) Draw() {
10+
fmt.Println("Square::Draw()")
11+
}
12+
13+
func (c *Square) GetName() string {
14+
return "Square"
15+
}

decorator-pattern/go/src/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module src
2+
3+
go 1.15

decorator-pattern/go/test/test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"../src"
7+
)
8+
9+
// main包下的main入口方法
10+
func main() {
11+
fmt.Println("test start:")
12+
13+
/**
14+
* 装饰器模式是将一个对象放到一个装饰器对象中,执行装饰器类里的方法时,对象的行为能力得到增强。
15+
* 先声明具体对象,然后放到装饰器,得到一个带有装饰器的新对象,该对象具备了新的能力。
16+
*/
17+
18+
// 声明形状
19+
var circle = new(src.Circle)
20+
var square = new(src.Square)
21+
22+
// 增加红色装饰
23+
var redCircle = &src.RedShapeDecorator{
24+
DecoratedShape: circle,
25+
}
26+
var redSquare = &src.RedShapeDecorator{
27+
DecoratedShape: square,
28+
}
29+
circle.Draw()
30+
redCircle.Draw()
31+
redSquare.Draw()
32+
33+
// 增加影子装饰
34+
var shadowCircle = &src.ShadowShapeDecorator{
35+
DecoratedShape: circle,
36+
}
37+
var shadowSquare = &src.ShadowShapeDecorator{
38+
DecoratedShape: square,
39+
}
40+
shadowCircle.Draw()
41+
shadowSquare.Draw()
42+
}
43+
44+
/**
45+
jarry@jarrys-MacBook-Pro go % go build src/*.go
46+
jarry@jarrys-MacBook-Pro go % go run test/test.go
47+
test start:
48+
Circle::Draw()
49+
Circle::Draw()
50+
RedShapeDecorator::setRedColor() Circle
51+
Square::Draw()
52+
RedShapeDecorator::setRedColor() Square
53+
ShadowShapeDecorator::SetShadow() Circle
54+
ShadowShapeDecorator::SetShadow() Square
55+
*/

decorator-pattern/java/src/ShapeDecorator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// 抽象装饰类,是否实现Shape可选
44
public abstract class ShapeDecorator implements Shape {
5+
// public abstract class ShapeDecorator {
56
protected Shape decoratedShape;
67

78
public ShapeDecorator(Shape decoratedShape) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package src
2+
3+
type AbstractFacade interface {
4+
Encoding(id int)
5+
Encrypt(id int)
6+
}

facade-pattern/go/src/EncodeModule.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package src
2+
3+
import "fmt"
4+
5+
// 编码模块
6+
type EncodeModule struct {
7+
}
8+
9+
func (en *EncodeModule) Encoding() {
10+
fmt.Println("EncodeModule::Encoding() 进行编码处理。")
11+
}

0 commit comments

Comments
 (0)