1. 概念
定义一系列的算法,把他们一个个封装起来,并使他们可以互相替换,本模式使得算法可以独立于使用它们的客户。
2. 案例
package org.demo.strategy.demo01; abstract class Strategy{ abstract public void method() ; } class StrategyImpl01 extends Strategy{ @Override public void method() { System.out.println("算法一"); } } class StrategyImpl02 extends Strategy{ @Override public void method() { System.out.println("算法二"); } } class Context{ private Strategy strategy ; public Context(Strategy strategy){ this.strategy = strategy ; } public void doMethod(){ strategy.method() ; } } public class Demo01 { public static void main(String[] args) { Strategy strategy = new StrategyImpl01() ; Context c = new Context(strategy) ; c.doMethod() ; strategy = new StrategyImpl02() ; c = new Context(strategy) ; c.doMethod() ; } }