ITPub博客

首页 > 应用开发 > Java > 策略模式

策略模式

原创 Java 作者:壹頁書 时间:2017-03-21 10:42:16 0 删除 编辑
http://www.importnew.com/12690.html
http://www.importnew.com/12853.html

这个倒是看着不复杂



策略模式应用
Java.util.Collections#sort(List list, Comparator < ? super T > c)
java.util.Arrays#sort(T[], Comparator < ? super T > c)
HashMap中对hashcode的计算
  1. class Context {  
  2.     // 持有一个具体策略的对象  
  3.     private Strategy strategy;  
  4.   
  5.     /** 
  6.      * 构造函数,传入一个具体策略对象 
  7.      *  
  8.      * @param strategy 
  9.      *            具体策略对象 
  10.      */  
  11.     public Context(Strategy strategy) {  
  12.         this.strategy = strategy;  
  13.     }  
  14.   
  15.     /** 
  16.      * 策略方法 
  17.      */  
  18.     public void contextInterface() {  
  19.   
  20.         strategy.strategyInterface();  
  21.     }  
  22.   
  23. }  
  24.   
  25. interface Strategy {  
  26.     /** 
  27.      * 策略方法 
  28.      */  
  29.     public void strategyInterface();  
  30. }  
  31.   
  32. class ConcreteStrategyA implements Strategy {  
  33.   
  34.     @Override  
  35.     public void strategyInterface() {  
  36.         // 相关的业务  
  37.     }  
  38.   
  39. }  
  40.   
  41. class ConcreteStrategyB implements Strategy {  
  42.   
  43.     @Override  
  44.     public void strategyInterface() {  
  45.         // 相关的业务  
  46.     }  
  47.   
  48. } 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2135735/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 强化责任链模式
下一篇: 模板方法模式
请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339926