Guest User

Untitled

a guest
Jul 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. package com.example.chapter7.traits
  2.  
  3. class DecoratorTraits
  4.  
  5. abstract class Check {
  6. def check():String ="Checked application details"
  7. }
  8.  
  9. trait employmentCheck extends Check {
  10. override def check():String="Checked employment details " +super.check()
  11. }
  12.  
  13. trait creditCheck extends Check {
  14. override def check():String="Checked credit details " +super.check()
  15. }
  16.  
  17. trait crimeCheck extends Check {
  18. override def check():String="Checked crime details " +super.check()
  19. }
  20.  
  21. object DecoratorTraits extends Application
  22. {
  23. override def main(args:Array[String])
  24. {
  25. println("======================================================")
  26. println(notes())
  27. println("======================================================")
  28. println("Logic execution strace below")
  29. println("======================================================")
  30. val newEmpCheck = new Check with employmentCheck with crimeCheck
  31. val newTenantCheck = new Check with employmentCheck with crimeCheck with creditCheck
  32. println(newEmpCheck check)
  33. println(newTenantCheck check)
  34. }
  35.  
  36. def notes() =
  37. {
  38. """
  39. Wikipedia says "The decorator pattern can be used to make it possible to extend (decorate) the functionality of a
  40. certain object at runtime, independently of other instances of the same class, provided some groundwork is done at design time."
  41.  
  42. In example above multiple checks are added e.g. employment,credit,crime.
  43. newEmpCheck though uses decorator pattern you can see at design time we need to add super.check() in each trait to achieve
  44. expected result.
  45.  
  46. Points to Note:
  47. 1. Also each trait extends abstract class so these traits can ONLY be mixed with the class which extends same abstract class.
  48. 2. Also if abstract class Check has some concrete utility methods those can be utilized in trait's concrete check implementation.
  49. 3. straight from pragprog book to understand execution( late method bnding for this particular example )
  50.  
  51. "The rightmost trait picked up the call to check(). It then, upon the call to super.check(), passed the call over to the trait
  52. on its left. The leftmost traits invoked the check() on the actual instance."
  53.  
  54. """
  55. }
  56. }
Add Comment
Please, Sign In to add comment