// Decorator // Increment "wraps" Dubble "wraps" Increment "wraps" Core interface LCD { static final StringBuffer indent = new StringBuffer(); void processNumber( int in ); } class Core implements LCD { public void processNumber( int in ) { System.out.println( indent.toString() + "Core ------ " + in ); } } abstract class Decorator implements LCD { private LCD inner; public Decorator( LCD in ) { inner = in; } public void processNumber( int in ) { indent.append( " " ); inner.processNumber( in ); indent.setLength( indent.length() - 2 ); } } class Increment extends Decorator { public Increment( LCD inner ) { super( inner ); } public void processNumber( int in ) { System.out.println( indent.toString() + "Increment - " + in ); super.processNumber( in + 1 ); System.out.println( indent.toString() + "Increment - " + in ); } } class Dubble extends Decorator { public Dubble( LCD inner ) { super( inner ); } public void processNumber( int in ) { System.out.println( indent.toString() + "Dubble ---- " + in ); super.processNumber( in * 2 ); System.out.println( indent.toString() + "Dubble ---- " + in ); } } public class DecoratorDemo { public static void main( String[] args ) { LCD layers = new Increment( new Dubble( new Increment( new Dubble( new Core() )))); for (int i=5; i < 7; i++) layers.processNumber( i ); } } /************ Increment - 5 Dubble ---- 6 Increment - 12 Dubble ---- 13 Core ------ 26 Dubble ---- 13 Increment - 12 Dubble ---- 6 Increment - 5 Increment - 6 Dubble ---- 7 Increment - 14 Dubble ---- 15 Core ------ 30 Dubble ---- 15 Increment - 14 Dubble ---- 7 Increment - 6 ************/