Advertisement
AlejoAsd

Interface Composition

Jul 16th, 2020
1,780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.14 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. type RI interface {
  8.     Create(in interface{}) interface{}
  9.     Get(in interface{}) interface{}
  10.     Update(in interface{}) interface{}
  11.     Delete(in interface{}) interface{}
  12. }
  13.  
  14. type R struct{}
  15.  
  16. func (r *R) Create(in interface{}) interface{} { return "R Create" }
  17. func (r *R) Get(in interface{}) interface{}    { return "R Get" }
  18. func (r *R) Update(in interface{}) interface{} { return "R Update" }
  19. func (r *R) Delete(in interface{}) interface{} { return "R Delete" }
  20.  
  21. type RA struct{
  22.     RI
  23. }
  24.  
  25. func (r *RA) Create(in interface{}) interface{} { return "RA Create" }
  26. func (r *RA) Custom(in interface{}) interface{} { return "RA Custom" }
  27.  
  28. func newRA() *RA {
  29.     return &RA{RI: &R{}}
  30. }
  31.  
  32. type SI interface {
  33.     getR() RI
  34.     Create(in interface{}) interface{}
  35.     Get(in interface{}) interface{}
  36.     Update(in interface{}) interface{}
  37.     Delete(in interface{}) interface{}
  38. }
  39.  
  40. type S struct {
  41.     RI
  42. }
  43.  
  44. func (s *S) getR() RI { return s.RI }
  45. func (s *S) Create(in interface{}) interface{} { return "S Create " + s.RI.Create(in).(string) }
  46. func (s *S) Get(in interface{}) interface{}    { return "S Get " + s.RI.Get(in).(string) }
  47. func (s *S) Update(in interface{}) interface{} { return "S Update " + s.RI.Update(in).(string) }
  48. func (s *S) Delete(in interface{}) interface{} { return "S Delete " + s.RI.Delete(in).(string) }
  49.  
  50. func newS(r RI) *S {
  51.         if r == nil {
  52.         r = &R{}
  53.     }
  54.     return &S{RI: r}
  55. }
  56.  
  57. type SA struct {
  58.     SI
  59. }
  60.  
  61. func (s *SA) Create(in interface{}) interface{} { return "SA Create " + s.SI.getR().Create(in).(string) }
  62.  
  63. func newSA() *SA {
  64.     return &SA{SI: newS(newRA())}
  65. }
  66.  
  67. type SB struct {
  68.     SI
  69. }
  70.  
  71. func (s *SB) Get(in interface{}) interface{} { return "SB Get " + s.SI.getR().Create(in).(string) }
  72.  
  73. func newSB() *SB {
  74.     return &SB{SI: newS(nil)}
  75. }
  76.  
  77. type SC struct {
  78.     SI
  79. }
  80.  
  81. func (s *SC) Delete(in interface{}) interface{} { return "SC Delete " + s.SI.getR().Create(in).(string) }
  82.  
  83. func newSC() *SC {
  84.     return &SC{SI: newS(nil)}
  85. }
  86.  
  87. func process(s SI) {
  88.     fmt.Println(s.Create(123))
  89.     fmt.Println(s.Get(123))
  90.     fmt.Println(s.Update(123))
  91.     fmt.Println(s.Delete(123))
  92. }
  93.  
  94. func main() {
  95.     process(newSA())
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement