Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. type (
  8. // ------------------
  9. // Interfaces
  10. // ------------------
  11. DatabaseLayerLargeInterface interface { /* Most likely defined in a separate package */
  12. Find(map[string]interface{}) []interface{}
  13. Create()
  14. Update()
  15. Delete()
  16. }
  17.  
  18. TemplateStorage interface {
  19. Find(map[string]interface{}) []interface{} /* matches DatabaseLayerLargeInterface.Find signature */
  20. }
  21.  
  22. TemplateFinder interface {
  23. Find(namespace, language string) string
  24. }
  25.  
  26. // ---------------
  27. // Implementations
  28. // ---------------
  29.  
  30. Database struct{} /* Most likely defined in a separate package */
  31. TemplateFinder1 struct{ storage DatabaseLayerLargeInterface }
  32. TemplateFinder2 struct{ storage TemplateStorage }
  33. )
  34.  
  35. func (db Database) Find(criteria map[string]interface{}) []interface{} { return []interface{}{} }
  36. func (db Database) Create() {}
  37. func (db Database) Update() {}
  38. func (db Database) Delete() {}
  39.  
  40. func NewTemplateFinder1(storage DatabaseLayerLargeInterface) TemplateFinder {
  41. return TemplateFinder1{storage}
  42. }
  43. func NewTemplateFinder2(storage TemplateStorage) TemplateFinder {
  44. return TemplateFinder2{storage}
  45. }
  46.  
  47. func (f TemplateFinder1) Find(namespace, language string) string {
  48. return "TemplateFinder1 will delegate the actual 'finding' to its (bloated) DatabaseLayerLargeInterface storage... this is not the actual template!"
  49. }
  50. func (f TemplateFinder2) Find(namespace, language string) string {
  51. return "TemplateFinder2 will delegate the actual 'finding' to its (slim) TemplateStorage storage... this is not the actual template!"
  52. }
  53.  
  54. func main() {
  55. db := Database{}
  56.  
  57. tf1 := NewTemplateFinder1(db)
  58. tf2 := NewTemplateFinder2(db)
  59.  
  60. namespace := "push.fact.loan"
  61. language := "br"
  62.  
  63. fmt.Println(tf1.Find(namespace, language))
  64. fmt.Println(tf2.Find(namespace, language))
  65.  
  66. // Questions
  67. //
  68. // When testing TemplateFinder1 ...
  69. // - how many methods do we need to mock in TemplateFinder1.storage?
  70. // - what if our DatabaseLayerLargeInterface expands?
  71. //
  72. // When testing TemplateFinder2 ...
  73. // - how many methods do we need to mock in TemplateFinder2.storage?
  74. // - what if our DatabaseLayerLargeInterface expands?
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement