本文的设计模式列表来源于github上人气比较高的iluwatar/java-design-patterns,包含了23种经典设计模式和很多实际应用中涉及到的模式。
文中涉及到的示例代码可能只抽取了部分进行讲解,如果有本地模拟需求,请参考源码。
“设计模式是程序员在设计应用程序时能够解决场景问题的最佳实现,通过经测试和验证的开发范例,可以提高开发效率。重用设计模式,可有效避免可能因细微问题而导致的重大隐患,同时有助于提升熟悉设计模式的编码人员和架构师对代码的可读性。”
作用
装饰器模式(Decorator,也可称作Wrapper),动态地将附加的职责添加到对象中,装饰器给类对象提供了一种灵活的扩展功能。
适用性
- 动态和透明地为单个对象添加功能,且不影响其他对象调用;
- 当无法通过继承的方式去扩展对象时。
典型用例
代码示例
以魔兽世界中的巨魔和加强型巨魔为例,在不改变原始巨魔本身动作的情况下,衍生出加强型巨魔。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 
 | public interface Troll {
 void attack();
 int getAttackPower();
 void fleeBattle();
 }
 
 public class SimpleTroll implements Troll {
 @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!");
 }
 }
 
 public class ClubbedTroll implements Troll {
 
 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();
 }
 }
 
 public static void main(String[] args) {
 
 LOGGER.info("A simple looking troll approaches.");
 Troll troll = new SimpleTroll();
 troll.attack();
 troll.fleeBattle();
 LOGGER.info("Simple troll power {}.\n", troll.getAttackPower());
 
 LOGGER.info("A troll with huge club surprises you.");
 troll = new ClubbedTroll(troll);
 troll.attack();
 troll.fleeBattle();
 LOGGER.info("Clubbed troll power {}.\n", troll.getAttackPower());
 }
 
 | 
类图