package com.eyugame.modle; /** * 策略模式 * @author JYC506 * */ /*策略一*/ public class Strategy1 implements IStrategy{ @Override public void show() { System.out.println("使用策略1"); } public static void main(String[] args) { Context context; /*使用策略一*/ context=new Context(new Strategy1()); context.execute(); /*使用策略二*/ context=new Context(new Strategy2()); context.execute(); } } /*策略二*/ class Strategy2 implements IStrategy{ @Override public void show() { System.out.println("使用策略2"); } } interface IStrategy{ void show(); } /*调用者*/ class Context{ IStrategy strategy; public Context(IStrategy strategy){ this.strategy=strategy; } public void execute(){ this.strategy.show(); } }
时间: 2024-10-18 13:19:23