X Tutup
Skip to content

Commit 10a911b

Browse files
committed
iluwatar#107 Decorator example JavaDoc
1 parent 3526d96 commit 10a911b

File tree

4 files changed

+100
-91
lines changed

4 files changed

+100
-91
lines changed
Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
1-
package com.iluwatar.decorator;
2-
3-
/**
4-
*
5-
* Decorator pattern is more flexible alternative to subclassing. The decorator
6-
* class implements the same interface as the target and uses composition to
7-
* "decorate" calls to the target.
8-
*
9-
* Using decorator pattern it is possible to change class behavior during
10-
* runtime, as the example shows.
11-
*
12-
*/
13-
public class App {
14-
15-
public static void main(String[] args) {
16-
17-
// simple troll
18-
System.out.println("A simple looking troll approaches.");
19-
Hostile troll = new Troll();
20-
troll.attack();
21-
troll.fleeBattle();
22-
23-
// change the behavior of the simple troll by adding a decorator
24-
System.out.println("\nA smart looking troll surprises you.");
25-
Hostile smart = new SmartTroll(troll);
26-
smart.attack();
27-
smart.fleeBattle();
28-
}
29-
}
1+
package com.iluwatar.decorator;
2+
3+
/**
4+
*
5+
* Decorator pattern is a more flexible alternative to subclassing. The decorator
6+
* class implements the same interface as the target and uses composition to
7+
* "decorate" calls to the target.
8+
* <p>
9+
* Using decorator pattern it is possible to change class behavior during
10+
* runtime, as the example shows.
11+
*
12+
*/
13+
public class App {
14+
15+
/**
16+
* Program entry point
17+
* @param args command line args
18+
*/
19+
public static void main(String[] args) {
20+
21+
// simple troll
22+
System.out.println("A simple looking troll approaches.");
23+
Hostile troll = new Troll();
24+
troll.attack();
25+
troll.fleeBattle();
26+
27+
// change the behavior of the simple troll by adding a decorator
28+
System.out.println("\nA smart looking troll surprises you.");
29+
Hostile smart = new SmartTroll(troll);
30+
smart.attack();
31+
smart.fleeBattle();
32+
}
33+
}
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
package com.iluwatar.decorator;
2-
3-
/**
4-
* SmartTroll is a decorator for Hostile objects.
5-
* The calls to the Hostile interface are intercepted
6-
* and decorated. Finally the calls are delegated
7-
* to the decorated Hostile object.
8-
*
9-
*/
10-
public class SmartTroll implements Hostile {
11-
12-
private Hostile decorated;
13-
14-
public SmartTroll(Hostile decorated) {
15-
this.decorated = decorated;
16-
}
17-
18-
@Override
19-
public void attack() {
20-
System.out.println("The troll throws a rock at you!");
21-
decorated.attack();
22-
}
23-
24-
@Override
25-
public void fleeBattle() {
26-
System.out.println("The troll calls for help!");
27-
decorated.fleeBattle();
28-
}
29-
30-
}
1+
package com.iluwatar.decorator;
2+
3+
/**
4+
* SmartTroll is a decorator for {@link Hostile} objects.
5+
* The calls to the {@link Hostile} interface are intercepted
6+
* and decorated. Finally the calls are delegated
7+
* to the decorated {@link Hostile} object.
8+
*
9+
*/
10+
public class SmartTroll implements Hostile {
11+
12+
private Hostile decorated;
13+
14+
public SmartTroll(Hostile decorated) {
15+
this.decorated = decorated;
16+
}
17+
18+
@Override
19+
public void attack() {
20+
System.out.println("The troll throws a rock at you!");
21+
decorated.attack();
22+
}
23+
24+
@Override
25+
public void fleeBattle() {
26+
System.out.println("The troll calls for help!");
27+
decorated.fleeBattle();
28+
}
29+
30+
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package com.iluwatar.decorator;
2-
3-
/**
4-
*
5-
* Troll implements Hostile interface directly.
6-
*
7-
*/
8-
public class Troll implements Hostile {
9-
10-
public void attack() {
11-
System.out.println("The troll swings at you with a club!");
12-
}
13-
14-
public void fleeBattle() {
15-
System.out.println("The troll shrieks in horror and runs away!");
16-
}
17-
18-
}
1+
package com.iluwatar.decorator;
2+
3+
/**
4+
*
5+
* Troll implements {@link Hostile} interface directly.
6+
*
7+
*/
8+
public class Troll implements Hostile {
9+
10+
public void attack() {
11+
System.out.println("The troll swings at you with a club!");
12+
}
13+
14+
public void fleeBattle() {
15+
System.out.println("The troll shrieks in horror and runs away!");
16+
}
17+
18+
}
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
package com.iluwatar.decorator;
2-
3-
import org.junit.Test;
4-
5-
import com.iluwatar.decorator.App;
6-
7-
public class AppTest {
8-
9-
@Test
10-
public void test() {
11-
String[] args = {};
12-
App.main(args);
13-
}
14-
}
1+
package com.iluwatar.decorator;
2+
3+
import org.junit.Test;
4+
5+
import com.iluwatar.decorator.App;
6+
7+
/**
8+
*
9+
* Application test
10+
*
11+
*/
12+
public class AppTest {
13+
14+
@Test
15+
public void test() {
16+
String[] args = {};
17+
App.main(args);
18+
}
19+
}

0 commit comments

Comments
 (0)
X Tutup