File tree 5 files changed +22
-6
lines changed
src/main/java/com/iluwatar 5 files changed +22
-6
lines changed Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
+ /**
4
+ *
5
+ * Decorator pattern is more flexible alternative to
6
+ * subclassing. The decorator class implements the same
7
+ * interface as the target and uses composition to
8
+ * "decorate" calls to the target.
9
+ *
10
+ */
3
11
public class App
4
12
{
5
13
public static void main ( String [] args )
6
14
{
7
15
8
16
System .out .println ("A simple looking troll approaches." );
9
- Troll troll = new Troll ();
17
+ Hostile troll = new Troll ();
10
18
troll .attack ();
11
19
troll .fleeBattle ();
12
20
13
21
System .out .println ("\n A smart looking troll surprises you." );
14
- Troll smart = new SmartTroll (new Troll ());
22
+ Hostile smart = new SmartTroll (new Troll ());
15
23
smart .attack ();
16
24
smart .fleeBattle ();
17
25
}
Original file line number Diff line number Diff line change
1
+ package com .iluwatar ;
2
+
3
+ public interface Hostile {
4
+
5
+ void attack ();
6
+ void fleeBattle ();
7
+
8
+ }
Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
- public class SmartTroll extends Troll {
3
+ public class SmartTroll implements Hostile {
4
4
5
- private Troll decorated ;
5
+ private Hostile decorated ;
6
6
7
- public SmartTroll (Troll decorated ) {
7
+ public SmartTroll (Hostile decorated ) {
8
8
this .decorated = decorated ;
9
9
}
10
10
Original file line number Diff line number Diff line change 1
1
package com .iluwatar ;
2
2
3
- public class Troll {
3
+ public class Troll implements Hostile {
4
4
5
5
public void attack () {
6
6
System .out .println ("The troll swings at you with a club!" );
You can’t perform that action at this time.
0 commit comments