Updated decorator implementation and class diagram. Added comments.
This commit is contained in:
parent
e760858bb6
commit
1168f6e41f
Binary file not shown.
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 32 KiB |
@ -1,17 +1,25 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
* Decorator pattern is more flexible alternative to
|
||||
* subclassing. The decorator class implements the same
|
||||
* interface as the target and uses composition to
|
||||
* "decorate" calls to the target.
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
|
||||
System.out.println("A simple looking troll approaches.");
|
||||
Troll troll = new Troll();
|
||||
Hostile troll = new Troll();
|
||||
troll.attack();
|
||||
troll.fleeBattle();
|
||||
|
||||
System.out.println("\nA smart looking troll surprises you.");
|
||||
Troll smart = new SmartTroll(new Troll());
|
||||
Hostile smart = new SmartTroll(new Troll());
|
||||
smart.attack();
|
||||
smart.fleeBattle();
|
||||
}
|
||||
|
8
decorator/src/main/java/com/iluwatar/Hostile.java
Normal file
8
decorator/src/main/java/com/iluwatar/Hostile.java
Normal file
@ -0,0 +1,8 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public interface Hostile {
|
||||
|
||||
void attack();
|
||||
void fleeBattle();
|
||||
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class SmartTroll extends Troll {
|
||||
public class SmartTroll implements Hostile {
|
||||
|
||||
private Troll decorated;
|
||||
private Hostile decorated;
|
||||
|
||||
public SmartTroll(Troll decorated) {
|
||||
public SmartTroll(Hostile decorated) {
|
||||
this.decorated = decorated;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class Troll {
|
||||
public class Troll implements Hostile {
|
||||
|
||||
public void attack() {
|
||||
System.out.println("The troll swings at you with a club!");
|
||||
|
Loading…
x
Reference in New Issue
Block a user