策略模式
来源:互联网 发布:香港 收音机软件 推荐 编辑:程序博客网 时间:2024/06/08 04:44
定义了算法家族, 分别封装起来, 让它们之间可以互相替换.
//举例,假设有多个锦囊,没个锦囊中有不同的计谋/** * 计谋接口 * @author fan * */public interface Scheme { public String execute();}/** * 绿色计谋 * @author fan * */public class GreenSilk implements Scheme{ @Override public String execute() { // TODO Auto-generated method stub return "run away"; }}/** * 红色计谋 * @author fan * */public class RedSilk implements Scheme{ @Override public String execute() { // TODO Auto-generated method stub return "kill"; }}/** * 计谋持有者 锦囊 * @author fan * */public class SilkBox { private Scheme scheme; public SilkBox(Scheme scheme){ this.scheme=scheme; } public String execute() { // TODO Auto-generated method stub return this.scheme.execute(); }}//测试下public class Test { public static void main(String[] args) { SilkBox box = new SilkBox(new GreenSilk()); System.out.println(box.execute()); box = new SilkBox(new RedSilk()); System.out.println(box.execute()); }}//输出run awaykill
阅读全文
0 0
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- 策略模式
- Android自定义控件及属性
- os.path.abspath(sys.argv[0])学习
- Codeforces 500A New Year Transportation 题解
- 解决微信页面中ios音乐不能自动播放问题
- php日周月排行版
- 策略模式
- Fibonacci从m-n输出
- vue2 中 computed 和 watch 的异同?
- Java设计person类,有姓名,年龄,性别。要求:该类至多只能创建一男,一女两个对象。
- 让tomcat支持软连接,实现快速回滚【转】
- 通过Scanner从控制台获取数据
- TCP连接的状态转移
- jquery一些对元素的操作
- UIScrollView实现图片轮播器及其无限循环效果