The builder pattern -golang

In this post, we will implement practice builder mode.

Examples wherein substantially novice replica tutorial example, where implemented using golang. For specific description we can go to rookie tutorial to learn (http://www.runoob.com/design-pattern/builder-pattern.html)


package main

import "fmt"

func main() {
   menu = Menu {}
   HotDog := PackageItem{Name:"hotdog",Price:10.00}
   Coke := PackageItem{Name:"code",Price:20.00}
   m := Package{}
   m.Name = "组合1"
   m.Pi = append(m.Pi, HotDog)
   m.Pi = append(m.Pi, Coke)
   m.AllPrice()
   menu.P = append(menu.P,m)

   mm := Package{}
   mm.Name = "组合2"
   mm.Pi = append(mm.Pi, HotDog)
   mm.Pi = append(mm.Pi, HotDog)
   mm.Pi = append(mm.Pi, Coke)
   mm.AllPrice()
   menu.P = append(menu.P,mm)


   menu.Show()
}

// menu
 of the type Menu struct {
   P []Package
}

// Menu Display
 FUNC (m Menu ) the Show () {
    for _ , F: = Range mP and {
      fmt.Print(f.Name)
      fmt.Print(":")
      fmt.Println(f.Price)
      f.Show()
   }
}

// course menu
 of the type Package Penalty for struct {
   Name string
   Price float32
   Pi []PackageItem
}

// Calculate the total price of the package
 FUNC (P * the Package ) AllPrice () {
    for _ , F: = Range p.Pi {
      p.Price = p.Price + f.Price
   }
}

// display the contents of the package
 FUNC (m the Package ) the Show () {
    for _ , F: = Range m.Pi {
      fmt.Print(f.Name)
      fmt.Print(":")
      fmt.Print(f.Price)
      fmt.Print(":")
      fmt. System.out.println (f.Pake. Use ())
   }
}

// Package contents
 of the type PackageItem struct {
   Name string
   Price float32
   Pake Wargger
}

// packaging
 of the type Wargger struct {
}

func (w Wargger) Pake() string {
   return "Wargger"
}




Published 48 original articles · won praise 17 · views 50000 +

Guess you like

Origin blog.csdn.net/aixinaxc/article/details/79312568