组合模式-golang

此篇中主要是练习组合模式,具体可以看菜鸟教程(http://www.runoob.com/design-pattern/composite-pattern.html)


package main

import "fmt"

func main() {
   ceo := Employee{name: "小张", dept: "ceo", salary: 10000}
   headSales := Employee{name: "小王", dept: "head sales", salary: 10000}
   headMarketing := Employee{name: "小李", dept: "head Marketing", salary: 10000}
   clerk1 := Employee{name: "小红", dept: "Marketing", salary: 10000}
   headMarketing.e = append(headMarketing.e, clerk1)
   ceo.e = append(ceo.e, headSales)
   ceo.e = append(ceo.e, headMarketing)


   fmt.Println("ceo",ceo)
   for _,t := range ceo.getSubordinates() {
      fmt.Println("head",t)
      for _,tt := range t.getSubordinates() {
         fmt.Println(tt)
      }
   }
}

type Employee struct {
   name   string
   dept   string
   salary int
   e      []Employee
}

func (e *Employee) add(em Employee) {
   e.e = append(e.e, em)
}

func (e *Employee) getSubordinates() []Employee {
   return e.e
}

你可能感兴趣的:(golang,设计模式)