-
Notifications
You must be signed in to change notification settings - Fork 805
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb005ac
commit 5f234c2
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author james mu | ||
* @date 2020/4/16 19:55 | ||
*/ | ||
public class App { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); | ||
|
||
public static void main(String[] args) { | ||
|
||
// simple troll | ||
LOGGER.info("A simple looking troll approaches."); | ||
Troll troll = new SimpleTroll(); | ||
troll.attack(); | ||
troll.fleeBattle(); | ||
LOGGER.info("Simple troll power {}.\n", troll.getAttackPower()); | ||
|
||
// change the behavior of the simple troll by adding a decorator | ||
LOGGER.info("A troll with huge club surprises you."); | ||
ClubbedTroll clubbedTroll = new ClubbedTroll(troll); | ||
clubbedTroll.attack(); | ||
clubbedTroll.fleeBattle(); | ||
LOGGER.info("Clubbed troll power {}.\n", clubbedTroll.getAttackPower()); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
netty-design-patterns-decorator/src/main/java/ClubbedTroll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author james mu | ||
* @date 2020/4/16 19:53 | ||
*/ | ||
public class ClubbedTroll implements Troll { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(ClubbedTroll.class); | ||
|
||
private Troll decorated; | ||
|
||
public ClubbedTroll(Troll decorated) { | ||
this.decorated = decorated; | ||
} | ||
|
||
@Override | ||
public void attack() { | ||
decorated.attack(); | ||
LOGGER.info("The troll swings at you with a club!"); | ||
} | ||
|
||
@Override | ||
public int getAttackPower() { | ||
return decorated.getAttackPower() + 10; | ||
} | ||
|
||
@Override | ||
public void fleeBattle() { | ||
decorated.fleeBattle(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
netty-design-patterns-decorator/src/main/java/SimpleTroll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* @author james mu | ||
* @date 2020/4/16 19:49 | ||
*/ | ||
public class SimpleTroll implements Troll { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleTroll.class); | ||
|
||
@Override | ||
public void attack() { | ||
LOGGER.info("The troll tries to grab you!"); | ||
} | ||
|
||
@Override | ||
public int getAttackPower() { | ||
return 10; | ||
} | ||
|
||
@Override | ||
public void fleeBattle() { | ||
LOGGER.info("The troll shrieks in horror and runs away!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* @author james mu | ||
* @date 2020/4/16 19:44 | ||
*/ | ||
public interface Troll { | ||
|
||
void attack(); | ||
|
||
int getAttackPower(); | ||
|
||
void fleeBattle(); | ||
|
||
} |