1、Context.java
package com.ssm.cts.pattern.strategy; public class Context { private Strategy straegy; public Context(Strategy _strategy) { this.straegy = _strategy; } public void operate() { this.straegy.operate(); } }
2、Strategy.java
package com.ssm.cts.pattern.strategy; public interface Strategy { public void operate(); }
3、TCStrategy.java
package com.ssm.cts.pattern.strategy; public class TCStrategy implements Strategy { @Override public void operate() { System.out.println("推送优惠券,拉动人气"); } }
4、SaleStrategy.java
package com.ssm.cts.pattern.strategy; public class SaleStrategy implements Strategy { @Override public void operate() { System.out.println("上线早餐、夜宵,提升营业额"); } }
5、Demo.java
package com.ssm.cts.pattern.strategy; public class Demo { // 测试 public static void main(String[] args) { Context context; // TC增长策略 System.out.println("TC增长策略"); context = new Context(new TCStrategy()); context.operate(); // 营业额增长策略 System.out.println("营业额增长策略"); context = new Context(new SaleStrategy()); context.operate(); } }