1. package main
  2. import "fmt"
  3. type Phone interface {
  4. ShowBrand()
  5. }
  6. type IPhone struct {
  7. name string
  8. version string
  9. }
  10. func (phone IPhone) ShowBrand() {
  11. fmt.Println("[Phone Brand]: " + phone.name + " version:" + phone.version)
  12. }
  13. type HPhone struct {
  14. }
  15. func (phone HPhone) ShowBrand() {
  16. fmt.Println("[Phone Brand]: Huawei")
  17. }
  18. type XPhone struct {
  19. }
  20. func (phone XPhone) ShowBrand() {
  21. fmt.Println("[Phone Brand]: Xiaomi")
  22. }
  23. type PhoneFactory struct {
  24. }
  25. func (factory PhoneFactory) CreatePhone(brand string) Phone {
  26. switch brand {
  27. case "HW":
  28. return new(HPhone)
  29. case "XM":
  30. return new(XPhone)
  31. case "PG":
  32. a := new(IPhone)
  33. //a := IPhone{"iiiiii", "4.0"}
  34. a.name = "Apple"
  35. return a
  36. default:
  37. return nil
  38. }
  39. }
  40. func main() {
  41. var phone Phone
  42. factory := new(PhoneFactory)
  43. phone = factory.CreatePhone("HW")
  44. phone.ShowBrand()
  45. phone = factory.CreatePhone("XM")
  46. phone.ShowBrand()
  47. phone = factory.CreatePhone("PG")
  48. phone.ShowBrand()
  49. }

分类: web

标签:   golang