注:本文已发布超过一年,请注意您所使用工具的相关版本是否适用
序
笔记

代码实现
公司的人员组织就是一个典型的树状的结构,现在假设我们现在有部分,和员工,两种角色,一个部门下面可以存在子部门和员工,员工下面不能再包含其他节点。
我们现在要实现一个统计一个部门下员工数量的功能
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package composite// IOrganization 组织接口,都实现统计人数的功能 type IOrganization interface { Count() int } // Employee 员工 type Employee struct { Name string } // Count 人数统计 func (Employee) Count() int { return 1 } // Department 部门 type Department struct { Name string SubOrganizations []IOrganization } // Count 人数统计 func (d Department) Count() int { c := 0 for _, org := range d.SubOrganizations { c += org.Count() } return c } // AddSub 添加子节点 func (d *Department) AddSub(org IOrganization) { d.SubOrganizations = append(d.SubOrganizations, org) } // NewOrganization 构建组织架构 demo func NewOrganization() IOrganization { root := &Department{Name: "root"} for i := 0; i < 10; i++ { root.AddSub(&Employee{}) root.AddSub(&Department{Name: "sub", SubOrganizations: []IOrganization{&Employee{}}}) } return root }
|
单元测试
1 2 3 4 5 6 7 8 9 10 11 12
| package compositeimport ( "testing" "github.com/stretchr/testify/assert" ) func TestNewOrganization(t *testing.T) { got := NewOrganization().Count() assert.Equal(t, 20, got) }
|
关注我获取更新
猜你喜欢