java 设计模式实际应⽤场景实例
常⽤设计模式
策略模式、模板⽅法、⼯⼚模式、单例模式
业务场景
营销拉新活动。
实例⼀:⼯⼚模式+抽象
定义抽象业务接⼝
定义具体业务实现类
利⽤⼯⼚模式获取实例对象
业务⼊⼝处根据不同渠道执⾏不同的发放逻辑public  abstract  class  AwardAbstract {    public  abstract  Boolean award (String userId );}
1
2
3// 头条渠道发放奖励业务public  class  TouTiaoAwardService extends  AwardAbstract {    @Override    public  Boolean award (String userId ) {        log .info ("头条渠道⽤户{}奖励50元红包!", userId );        return  Boolean .TRUE ;    }}// 渠道发放奖励业务public  class  WeChatAwardService extends  AwardAbstract {    @Override    public  Boolean award (String userId ) {        log .info ("渠道⽤户{}奖励100元红包!", userId );        return  Boolean .TRUE ;    }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public  class  AwardFactory {    public  static  AwardAbstract getAwardInstance (String source ) {        if  ("toutiao".equals (source )) {            return  new  TouTiaoAwardService ();        } else  if  ("wx".equals (source )) {            return  new  WeChatAwardService ();        }        return  null ;    }}
1
2
3
4
5
6
7
8
9
10
实例⼆:策略模式+模板⽅法+⼯⼚模式+单例模式
还是以营销拉新为业务场景来说明,这个业务流程再增加⼀些复杂度,⽐如发放奖励之前要进⾏⾝份验证、风控验证等⼀些列的校验,此时你的业务流程该如何实现更清晰简洁呢?
定义业务策略接⼝
定义奖励发放模板流程
实现不同渠道的奖励业务@PostMapping("/reward2")public  void  reward2(String userId , String source ) {    AwardAbstract instance = AwardFactory .getAwardInstance (source );    if  (null != instance ) {        instance .award (userId );    }}
1
2
3
4
5
67/** 策略业务接⼝ */public  interface  AwardStrategy {    /**    * 奖励发放接⼝    */    Map <String , Boolean > awardStrategy (String userId );    /**    * 获取策略标识,即不同渠道的来源标识    */    String getSource ();}
1
2
3
4
5
6
7
8
9
10
11
12public  abstract  class  BaseAwardTemplate {    private  static  final  Logger log = LoggerFactory .getLogger (BaseAwardTemplate .class );    //奖励发放模板⽅法    public  Boolean awardTemplate (String userId ) {        this .authentication (userId );        this .risk (userId );        return  this .awardRecord (userId );    }    //⾝份验证    protected  void  authentication (String userId ) {        log .info ("{} 执⾏⾝份验证!", userId );    }    //风控    protected  void  risk (String userId ) {        log .info ("{} 执⾏风控校验!", userId );    }    //执⾏奖励发放    protected  abstract  Boolean awardRecord (String userId );}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
定义⼯⼚⽅法,对外统⼀暴露业务调⽤⼊⼝@Slf4j @Service public  class  ToutiaoAwardStrategyService extends  BaseAwardTemplate implements  AwardStrategy {    /**    * 奖励发放接⼝    */    @Override    public  Boolean awardStrategy (String userId ) {        return  super .awardTemplate (userId );    }    @Override    public  String getSource () {        return  "toutiao";    }    /**    * 具体的业务奖励发放实现    */    @Override    protected  Boolean awardRecord (String userId ) {        log .info ("头条渠道⽤户{}奖励50元红包!", userId );        return  Boolean .TRUE ;    }}@Slf4j @Service p
ublic  class  WeChatAwardStrategyService extends  BaseAwardTemplate implements  AwardStrategy {    /**    * 奖励发放接⼝    */    @Override    public  Boolean awardStrategy (String userId ) {        return  super .awardTemplate (userId );    }    @Override    public  String getSource () {        return  "wx";    }    /***    * 具体的业务奖励发放实现    */    @Override    protected  Boolean awardRecord (String userId ) {        log .info ("渠道⽤户{}奖励100元红包!", userId );        return  Boolean .TRUE ;    }}
1
2
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
java中常用的设计模式有哪些38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
业务⼊⼝⽅法@Component public  class  AwardStrategyFactory implements  ApplicationContextAware {    private  final  static  Map <String , AwardStrategy > MAP = new  HashMap <>();    @Override    public  void  setApplicationContext (ApplicationContext applicationContext ) throws  BeansException {        Map <String , AwardStrategy > beanTypeMap = applicationContext .getBeansOfType (AwardStrategy .class );        beanTypeMap .values ().forEach (strategyObj -> MAP .put (strategyObj .getSource (), strategyObj ));    }    /**    * 对外统⼀暴露的⼯⼚⽅法    */    public  Boolean getAwardResult (String userId , String source ) {        AwardStrategy strategy = MAP .get (source );        if  (Objects .isNull (strategy )) {            throw  new  RuntimeException ("渠道异常!");        }        return  strategy .awardStrategy (userId );    }    /**    * 静态内部类创建单例⼯⼚对象    */    private  static  class  CreateFactorySingleton {        private  static  AwardStrategyFactory factory = new  AwardStrategyFactory ();    }    public  static  AwardStrategyFactory getInstance () {        return  CreateFactorySingleton .factory ;    }}
1
2
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@RestController @RequestMapping("/activity")public  class  ActivityController {    @PostMapping("/reward3")    public  void  reward3(String userId , String source ) {        AwardStrategyFactory .getInstance ().getAwardResult (userId , source );    }}1
2
3
4
5
6
7
8
9

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。