op8867555

Untitled

Dec 24th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. package lab;
  2.  
  3. import static java.lang.System.out;
  4.  
  5. public class ChrismasTreeDemo {
  6.     public static void main (String[] args) {
  7.         ChrismasTreeComponent t =
  8.             new Gift( new Bell( new Candy(new ChrismasTree()) ) );
  9.         t.sing();
  10.     }
  11. }
  12.  
  13. abstract class ChrismasTreeComponent {
  14.     abstract public void sing();
  15. }
  16.  
  17. class ChrismasTree extends ChrismasTreeComponent {
  18.     @Override
  19.     public void sing() {
  20.         out.println("I am a Chrismas tree");
  21.     }
  22. }
  23.  
  24. abstract class ChrismasTreeDecorator extends ChrismasTreeComponent {
  25.  
  26.     ChrismasTreeComponent c;
  27.  
  28.     public ChrismasTreeDecorator(ChrismasTreeComponent c) {
  29.         this.c = c;
  30.     }
  31.     @Override
  32.     public void sing() {
  33.         this.decoratorsSing();
  34.         out.print(", ");
  35.         c.sing();
  36.     }
  37.     abstract void decoratorsSing();
  38. }
  39.  
  40. class Bell extends ChrismasTreeDecorator {
  41.     public Bell(ChrismasTreeComponent c) {
  42.         super(c);
  43.     }
  44.  
  45.     @Override
  46.     void decoratorsSing() {
  47.         out.print("I have a bell");
  48.     }
  49. }
  50.  
  51. class Candy extends ChrismasTreeDecorator {
  52.     public Candy(ChrismasTreeComponent c) {
  53.         super(c);
  54.     }
  55.     @Override
  56.     void decoratorsSing() {
  57.         out.print("I have a bell");
  58.     }
  59. }
  60.  
  61. class Gift extends ChrismasTreeDecorator {
  62.     public Gift(ChrismasTreeComponent c) {
  63.         super(c);
  64.     }
  65.     @Override
  66.     void decoratorsSing() {
  67.         out.print("I have a gift");
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment