策略设计模式

package com.jmj.pattern.strategy;

public interface Strategy {
    void show();
}
package com.jmj.pattern.strategy;

public class StrategyA implements Strategy{
    @Override
    public void show() {
        System.out.println("买一送一");
    }
}
package com.jmj.pattern.strategy;

public class StrategyB implements Strategy{
    @Override
    public void show() {
        System.out.println("买两百减五十");
    }
}
package com.jmj.pattern.strategy;

public class StrategyC implements Strategy{
    @Override
    public void show() {
        System.out.println("满1000加一元");
    }
}
package com.jmj.pattern.strategy;

public class SalesMan {
    private Strategy strategy;

    public SalesMan(Strategy strategy) {
        this.strategy = strategy;
    }

    public void salesManshow(){
        strategy.show();
    }

}
package com.jmj.pattern.strategy;

public class Client {
    public static void main(String[] args) {
        SalesMan salesMan = new SalesMan(new StrategyA());
        salesMan.salesManshow();
    }
}

策略设计模式_第1张图片策略设计模式_第2张图片

你可能感兴趣的:(设计模式,设计模式,java,servlet)