Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. // The Sensor interface class
  2. public interface Sensor{
  3. public void draw(); // Draws the Window
  4. public String getDescription(); // Returns a description of the Window
  5. }
  6.  
  7. // Extension of a simple Window without any scrollbars
  8. class SensorPH implements Sensor {
  9. public void leitura() {
  10. }
  11.  
  12. public String getDescription() {
  13. return "ph";
  14. }
  15. }
  16.  
  17. // abstract decorator class - note that it implements Sensor
  18. abstract class SensorDecorator implements Sensor{
  19. protected Sensor sensorToBeDecorated; // the Window being decorated
  20.  
  21. public Sensor (Sensor sensorToBeDecorated) {
  22. this.sensorToBeDecorated= sensorToBeDecorated;
  23. }
  24. public void leitura() {
  25. windowToBeDecorated.draw(); //Delegation
  26. }
  27. public String getDescription() {
  28. return windowToBeDecorated.getDescription(); //Delegation
  29. }
  30. }
  31.  
  32. // The first concrete decorator which adds vertical scrollbar functionality
  33. class SensorNew1Decorator extends SensorDecorator{
  34. public SensorNew1Decorator (Window sensorToBeDecorated) {
  35. super(sensorToBeDecorated);
  36. }
  37.  
  38. @Override
  39. public void leitura() {
  40. super.draw();
  41. newLeitura();
  42. }
  43.  
  44. private void newLeitura() {
  45. //do
  46. }
  47.  
  48. @Override
  49. public String getDescription() {
  50. return super.getDescription() + ", including new leitura";
  51. }
  52. }
  53.  
  54. // The second concrete decorator which adds new sensor functionality
  55. class SensorNew2Decorator extends SensorDecorator{
  56. public SensorNew2Decorator (Window sensorToBeDecorated) {
  57. super(sensorToBeDecorated);
  58. }
  59.  
  60. @Override
  61. public void leitura() {
  62. super.draw();
  63. newLeitura();
  64. }
  65.  
  66. private void newLeitura() {
  67. //do
  68. }
  69.  
  70. @Override
  71. public String getDescription() {
  72. return super.getDescription() + ", including new leitura";
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement