Guest User

Untitled

a guest
Mar 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. SpecialEmployee emp = new SpecialEmpoyee();
  2.  
  3. interface IBird
  4. {
  5. public void fly();
  6. public void makeSound();
  7. }
  8.  
  9. class Sparrow : IBird
  10. {
  11. public void fly()
  12. {
  13. // Print "Fly"
  14. }
  15. public void makeSound()
  16. {
  17. // Print "Sound"
  18. }
  19. }
  20.  
  21. interface ToyDuck
  22. {
  23. // toyducks dont fly they just make
  24. // squeaking sound
  25. public void squeak();
  26. }
  27.  
  28. class PlasticToyDuck implements ToyDuck
  29. {
  30. public void squeak()
  31. {
  32. //Print "Squeak"
  33. }
  34. }
  35.  
  36. class BirdAdapter implements ToyDuck
  37. {
  38. // You need to implement the interface your
  39. // client expects to use.
  40. // Instead of using implementation we are programming against interface
  41. // here, by using IBird instead of "new Sparrow" or something like it.
  42. // Further, it is an example of constructor dependency injection where we are inserting dependency "IBird" through constructor.
  43. IBird bird;
  44. public BirdAdapter(IBird bird)
  45. {
  46. // we need reference to the object we
  47. // are adapting
  48. this.bird = bird;
  49. }
  50.  
  51. public void squeak()
  52. {
  53. // translate the methods appropriately
  54. bird.makeSound();
  55. }
  56. }
Add Comment
Please, Sign In to add comment