daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. type ComputerPart interface {
  8.     Accept(ComputerVisitor)
  9. }
  10.  
  11. type ComputerVisitor interface {
  12.     VisitCPU(*CPU)
  13.     VisitDisk(*Disk)
  14.     VisitMemory(*Memory)
  15. }
  16.  
  17. type CPU struct {
  18.     UUID      string
  19.     Frequency float64
  20. }
  21.  
  22. func (c *CPU) Accept(v ComputerVisitor) {
  23.     v.VisitCPU(c)
  24. }
  25.  
  26. type Disk struct {
  27.     UUID string
  28.     Size uint64
  29. }
  30.  
  31. func (d *Disk) Accept(v ComputerVisitor) {
  32.     v.VisitDisk(d)
  33. }
  34.  
  35. type Memory struct {
  36.     UUID string
  37.     Size uint64
  38. }
  39.  
  40. func (m *Memory) Accept(v ComputerVisitor) {
  41.     v.VisitMemory(m)
  42. }
  43.  
  44. type Laptop struct {
  45.     Parts []ComputerPart
  46. }
  47.  
  48. func (l *Laptop) Accept(v ComputerVisitor) {
  49.     for _, p := range l.Parts {
  50.         p.Accept(v)
  51.     }
  52. }
  53.  
  54. type SpecsVisitor struct{}
  55.  
  56. func (v *SpecsVisitor) VisitCPU(c *CPU) {
  57.     fmt.Printf("cpu uuid=%s freq=%.2f GHz\n", c.UUID, c.Frequency)
  58. }
  59.  
  60. func (v *SpecsVisitor) VisitDisk(d *Disk) {
  61.     fmt.Printf("disk uuid=%s size=%d TB\n", d.UUID, d.Size)
  62. }
  63.  
  64. func (v *SpecsVisitor) VisitMemory(m *Memory) {
  65.     fmt.Printf("ram uuid=%s size=%d GB\n", m.UUID, m.Size)
  66. }
  67.  
  68. func main() {
  69.     l := new(Laptop)
  70.     l.Parts = []ComputerPart{
  71.         &CPU{"dbe0e589-0eba-404e-bf8e-6664e1bc37c4", 3.6},
  72.         &CPU{"fd4db97c-596d-4fe7-9a1a-20cbaf0f4e8e", 3.6},
  73.         &Disk{"1679dc4e-dea9-4da3-bcc9-e6f608f65d56", 3},
  74.         &Disk{"c15fbae8-68e9-455e-8bb0-687c1e9580f5", 3},
  75.         &Memory{"7c4cbdef-005a-44cc-9104-478a2e2b2441", 4},
  76.         &Memory{"15734c16-f73b-4921-805b-7cd6163a9678", 4},
  77.         &Memory{"d3a6fe65-8fe0-47e5-bb7f-34fcd35b0e94", 4},
  78.         &Memory{"f286b96a-d18e-4dbf-b333-371a3ab6ee88", 4},
  79.     }
  80.     visitor := new(SpecsVisitor)
  81.     l.Accept(visitor)
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top