irmantas_radavicius

Untitled

May 7th, 2026
117
0
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. // Decorator design pattern in Java
  2. // 2026-05-07
  3. // tested on Win with javac 21.0.2
  4.  
  5. import java.util.*;
  6.  
  7. // interface was added second to have an abstraction
  8. // an not to overload the Word class
  9. interface Gettable {
  10.     public String get();
  11.    
  12. }
  13. // this was the first class to add,
  14. // we wanted to have a word which we decided
  15. // to be able to modify late
  16. class Word implements Gettable {
  17.     private String word;
  18.     public Word(String word){
  19.         this.word = word;
  20.     }
  21.     public String get(){
  22.         return word;
  23.     }
  24. }
  25.  
  26. // third, now we can add the class to implement
  27. // the Decorator pattern, i.e. WordDecorator acts
  28. // like a Proxy and also satisfies the Gettable interface
  29. // (note both the composition and the inheritence).
  30. // We make this class abstract because it is designed
  31. // to be inherited by the derived classes (decorators) below
  32. abstract class WordDecorator implements Gettable {
  33.     protected Gettable gettable;
  34.     public WordDecorator(Gettable gettable){
  35.         this.gettable = gettable;
  36.     }
  37.     abstract public String get();
  38. }
  39. // we add exclamation mark in the end regardless of the word
  40. class ExclDecorator extends WordDecorator {
  41.     public ExclDecorator(Gettable gettable){
  42.         super(gettable);
  43.     }
  44.     public String get(){
  45.         return gettable.get() + "!";
  46.     }
  47. }
  48. // we add quotes around the word regardless of the word
  49. class QuoteDecorator extends WordDecorator {
  50.     public QuoteDecorator(Gettable gettable){
  51.         super(gettable);
  52.     }
  53.     public String get(){
  54.         return  "\"" + gettable.get() + "\"";
  55.     }
  56. }
  57.  
  58. public class Main {
  59.     // sample delegates to simplify the call to
  60.     // System.out.print and System.out.println
  61.     public static void print(String text){
  62.         System.out.print(text);
  63.     }
  64.     public static void println(String text){
  65.         System.out.println(text);
  66.     }
  67.     public static void main(String[] args){
  68.         // and here is the testing scenario:
  69.         // simple word first, nothing unusual
  70.         Gettable w = new Word("Hello");
  71.         println(w.get());
  72.         // we decorate the word
  73.         Gettable d1 = new ExclDecorator(w);
  74.         println(d1.get());
  75.         // we also decorate the word, but differently
  76.         Gettable d2 = new QuoteDecorator(w);
  77.         println(d2.get());
  78.         // and now the power of the Decorator:
  79.         // we can stack the decorators on top of each other
  80.         // which allows to dynamically change the functionality.
  81.         // Each Decorator adds their own thing and builds
  82.         // on the layer below.
  83.         Gettable d3 = new QuoteDecorator(d1);
  84.         println(d3.get());
  85.     }      
  86. }
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment