重构组合模式示例,统一接口定义,增强代码可读性和一致性
This commit is contained in:
+34
-25
@@ -7,25 +7,30 @@ import "fmt"
|
||||
汤宝宝成为了上市集团老总,他们集团有好多好多家分公司...
|
||||
*/
|
||||
|
||||
// 抽象接口
|
||||
type Company interface {
|
||||
// 显示公司名称
|
||||
type component interface {
|
||||
Show(prefix string)
|
||||
// 开设分公司
|
||||
Add(company Company)
|
||||
// 分公司关门
|
||||
Remove(company Company)
|
||||
// 获取分公司
|
||||
GetChild(i int) Company
|
||||
getName() string
|
||||
}
|
||||
|
||||
// 具体实现类
|
||||
type ConcreteCompany struct {
|
||||
type Department struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (d *Department) Show(prefix string) {
|
||||
fmt.Print(prefix)
|
||||
fmt.Println(d.name)
|
||||
}
|
||||
|
||||
func (d *Department) getName() string {
|
||||
return d.name
|
||||
}
|
||||
|
||||
type Company struct {
|
||||
name string
|
||||
children []Company
|
||||
children []component
|
||||
}
|
||||
|
||||
func (c *ConcreteCompany) Show(prefix string) {
|
||||
func (c *Company) Show(prefix string) {
|
||||
fmt.Print(prefix)
|
||||
fmt.Println(c.name)
|
||||
for _, child := range c.children {
|
||||
@@ -33,22 +38,26 @@ func (c *ConcreteCompany) Show(prefix string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ConcreteCompany) Add(company Company) {
|
||||
c.children = append(c.children, company)
|
||||
fmt.Printf("在%s下开设了分公司%s\n", c.name, company.(*ConcreteCompany).name)
|
||||
func (c *Company) getName() string {
|
||||
return c.name
|
||||
}
|
||||
|
||||
func (c *ConcreteCompany) Remove(company Company) {
|
||||
func (c *Company) Add(company component) {
|
||||
c.children = append(c.children, company)
|
||||
fmt.Printf("在%s下开设了%s\n", c.name, company.getName())
|
||||
}
|
||||
|
||||
func (c *Company) Remove(company component) {
|
||||
for i, child := range c.children {
|
||||
if child == company {
|
||||
c.children = append(c.children[:i], c.children[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Printf("在%s下关闭了分公司%s\n", c.name, company.(*ConcreteCompany).name)
|
||||
fmt.Printf("在%s下裁撤了%s\n", c.name, company.getName())
|
||||
}
|
||||
|
||||
func (c *ConcreteCompany) GetChild(i int) Company {
|
||||
func (c *Company) GetChild(i int) component {
|
||||
if i < 0 || i >= len(c.children) {
|
||||
return nil
|
||||
}
|
||||
@@ -58,12 +67,12 @@ func (c *ConcreteCompany) GetChild(i int) Company {
|
||||
func main() {
|
||||
fmt.Println("============= 组合模式 =============")
|
||||
|
||||
headOffice := &ConcreteCompany{name: "汤氏总公司"}
|
||||
headOffice.Add(&ConcreteCompany{name: "汤氏A分公司"})
|
||||
headOffice.Add(&ConcreteCompany{name: "汤氏B分公司"})
|
||||
headOffice.Add(&ConcreteCompany{name: "汤氏C分公司"})
|
||||
headOffice.GetChild(0).Add(&ConcreteCompany{name: "汤氏A分公司-子公司1"})
|
||||
headOffice.GetChild(0).Add(&ConcreteCompany{name: "汤氏A分公司-子公司2"})
|
||||
headOffice := &Company{name: "汤氏总公司"}
|
||||
headOffice.Add(&Company{name: "汤氏A分公司"})
|
||||
headOffice.Add(&Department{name: "人事部"})
|
||||
headOffice.Add(&Department{name: "研发部"})
|
||||
headOffice.GetChild(0).(*Company).Add(&Department{name: "市场部"})
|
||||
headOffice.GetChild(0).(*Company).Add(&Department{name: "财务部"})
|
||||
headOffice.Remove(headOffice.GetChild(2))
|
||||
fmt.Println()
|
||||
headOffice.Show("~ ")
|
||||
|
||||
Reference in New Issue
Block a user