Improved decorator example.
This commit is contained in:
parent
10d4e3cadd
commit
9336284814
@ -6,18 +6,23 @@ package com.iluwatar;
|
||||
* class implements the same interface as the target and uses composition to
|
||||
* "decorate" calls to the target.
|
||||
*
|
||||
* Using decorator pattern it is possible to change class behavior during
|
||||
* runtime, as the example shows.
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// simple troll
|
||||
System.out.println("A simple looking troll approaches.");
|
||||
Hostile troll = new Troll();
|
||||
troll.attack();
|
||||
troll.fleeBattle();
|
||||
|
||||
// change the behavior of the simple troll by adding a decorator
|
||||
System.out.println("\nA smart looking troll surprises you.");
|
||||
Hostile smart = new SmartTroll(new Troll());
|
||||
Hostile smart = new SmartTroll(troll);
|
||||
smart.attack();
|
||||
smart.fleeBattle();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user