File tree Expand file tree Collapse file tree 9 files changed +126
-0
lines changed
Expand file tree Collapse file tree 9 files changed +126
-0
lines changed Original file line number Diff line number Diff line change 1+ # 简介
2+ 策略模式属于对象的行为模式。将每一个算法封装到具有共同接口的独立类中,根据需要来绑定策略,使得具体实现和策略解耦。
3+
4+ # 基本结构
5+ 1 . 建立一个策略接口。
6+ 2 . 新建多个策略行为类,实现该策略接口。
7+ 3 . 建立一个抽象环境角色类,并将策略接口组合进来。是否需要抽象类可选。
8+ 4 . 建立多个环境角色类来继承该抽象类。
9+ 5 . 可以动态改变环境角色的策略行为。
10+
11+ # 作用
12+ - 策略算法可以自由切换,松耦合。
13+ - 避免使用多重条件判断,不同环境角色可以组装多个策略。
14+ - 扩展性良好,可以随时增删策略行为。
15+ - 体现了多用组合,少用继承。
Original file line number Diff line number Diff line change 1+ package src ;
2+
3+ public abstract class Context {
4+
5+ protected Strategy strategy ;
6+
7+ public void setStrategy (Strategy strategy ) {
8+ this .strategy = strategy ;
9+ }
10+
11+ public void action () {
12+ this .strategy .run ();
13+ }
14+
15+ }
Original file line number Diff line number Diff line change 1+ package src ;
2+ public class ContextCat extends Context {
3+
4+ public ContextCat () {
5+ // 使用某个策略
6+ System .out .println ("ContextCat::setStrategy(StrategyC)." );
7+ this .setStrategy (new StrategyC ());
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ package src ;
2+
3+ public class ContextDog extends Context {
4+
5+ public ContextDog () {
6+ // 使用某个策略
7+ System .out .println ("ContextDog::setStrategy(StrategyB)." );
8+ this .setStrategy (new StrategyB ());
9+ }
10+
11+ // 这样就是强制绑定了策略动作,不够灵活
12+ // @Override
13+ // public void action() {
14+ // new StrategyC().run();
15+ // }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package src ;
2+ public interface Strategy {
3+ public void run ();
4+ }
Original file line number Diff line number Diff line change 1+ package src ;
2+ public class StrategyA implements Strategy {
3+
4+ @ Override
5+ public void run () {
6+ System .out .println ("StrategyA::run()." );
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ package src ;
2+ public class StrategyB implements Strategy {
3+
4+ @ Override
5+ public void run () {
6+ System .out .println ("StrategyB::run()." );
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ package src ;
2+ public class StrategyC implements Strategy {
3+
4+ @ Override
5+ public void run () {
6+ System .out .println ("StrategyC::run()." );
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ package test ;
2+
3+ import src .*;
4+
5+ public class Test {
6+
7+ public static void start () {
8+
9+ // 实例化某个内容,策略已经绑定上
10+ Context contextCat = new ContextCat ();
11+ contextCat .action ();
12+
13+ // 重新设置策略
14+ System .out .println ("reset contextCat'strategy to StrategyA." );
15+ contextCat .setStrategy (new StrategyA ());
16+ contextCat .action ();
17+
18+ // 实例化某个内容,策略已经绑定上
19+ Context contextGog = new ContextDog ();
20+ contextGog .action ();
21+
22+
23+ }
24+ public static void main (String [] args ) {
25+ System .out .println ("test start:" );
26+ start ();
27+ }
28+
29+ }
30+
31+ /**
32+ * 测试
33+ jarry@jarrys-MacBook-Pro java % javac test/Test.java
34+ jarry@jarrys-MacBook-Pro java % java test/Test
35+ test start:
36+ ContextCat::setStrategy(StrategyC).
37+ StrategyC::run().
38+ reset contextCat'strategy to StrategyA.
39+ StrategyA::run().
40+ ContextDog::setStrategy(StrategyB).
41+ StrategyB::run().
42+ */
You can’t perform that action at this time.
0 commit comments