public class Decorator {
public static void main(String agrs[]) { Greeting1 g = new AfterGreeting(new BeforeGreeting(new Greeting1Impl())); g.sayHello("king"); }
}
interface Greeting1 {
void sayHello(String name);
}
class Greeting1Impl implements Greeting1 {
public void sayHello(String name) { System.out.println("hello " + name); }
}
abstract class DecoratorGreeting implements Greeting1 {
Greeting1 greeting1; public DecoratorGreeting(Greeting1 g) { greeting1 = g; } @Override public void sayHello(String name) { greeting1.sayHello(name); }
}
class BeforeGreeting extends DecoratorGreeting {
public BeforeGreeting(Greeting1 g) { super(g); } @Override public void sayHello(String name) { before(); super.sayHello(name); } private void before() { System.out.println("before..."); }
}
class AfterGreeting extends DecoratorGreeting {
public AfterGreeting(Greeting1 g) { super(g); } @Override public void sayHello(String name) { super.sayHello(name); after(); } private void after() { System.out.println("after..."); }
}