`

第七章 策略模式

阅读更多

1.策略模式概述

 

  • 策略模式定义一系列算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。
  • 策略模式是处理算法的不同变体的一种成熟模式,策略模式通过接口或抽象类封装算法的标识,即在接口中定义一个抽象方法,实现该接口的类将实现接口中的抽象方法。

2.策略模式-结构

 

策略模式的结构中包括三种角色:
 

  • 策略(Strategy)
  •   具体策略(ConcreteStrategy) 
  •   上下文(Context)



 3.策略模式-场景描述

多个裁判给选手打分,然后根据不同的算法计算选手的最后得分
策略一:求平均分
策略二:利用几何平均值计算
策略三:去掉最大值和最小值然后求平均分

4.策略模式-场景分析

 

  • 抽象策略:Computable
  • 具体策略:
    StrategyOne
    StrategyTwo
    StrategyThree
  • 上下文
    GymnasticsGame



 5.代码实现

package patterns.strategy;

public interface Computable {
	public double computeScore(double scores[]);
}


package patterns.strategy;

public class StrategyOne implements Computable {

	public double computeScore(double[] scores) {
		double avg = 0;
		double sum = 0;
		if(scores!=null && scores.length!=0){
			for (double s:scores) {
				sum += s;
			}
			avg = sum/scores.length;
		}
		return avg;
	}

}


package patterns.strategy;

public class StrategyTwo implements Computable {

	public double computeScore(double[] scores) {
		double avg = 0;
		double sum = 1;
		if(scores!=null && scores.length!=0){
			for (double s:scores) {
				sum *= s;
			}
			avg = Math.pow(sum, 1.0/scores.length);
		}
		return avg;
	}

}



package patterns.strategy;

import java.util.Arrays;

public class StrategyThree implements Computable {

	public double computeScore(double[] scores) {
		double avg = 0;
		double sum = 0;
		if(scores!=null && scores.length>2){
			Arrays.sort(scores);
			for (int i=1;i<(scores.length-1);i++) {
				sum += scores[i];
			}
			avg = sum/(scores.length-2);
		}
		return avg;
	}

}


package patterns.strategy;

public class GymnasticsGame {
	private Computable comp = null;

	public void setComp(Computable comp) {
		this.comp = comp;
	}

	public double getPersonScore(double scores[]) {
		return comp.computeScore(scores);
	}

}


package patterns.strategy;

public class Client {
	public static void main(String[] args) {
		double scores[] = {92.21,93.23,98.91,97.32,99.02};
		GymnasticsGame game = new GymnasticsGame();
		game.setComp(new StrategyOne());
		double avg = game.getPersonScore(scores);
		System.out.println("第一种策略得分:"+avg);
		
		game.setComp(new StrategyTwo());
		avg = game.getPersonScore(scores);
		System.out.println("第二种策略得分:"+avg);
		
		game.setComp(new StrategyThree());
		avg = game.getPersonScore(scores);
		System.out.println("第三种策略得分:"+avg);
	}
}

 6.什么时候需要使用策略模式

 

  • 一个系统里面有许多类,他们之间的区别仅在于他们的行为,使用策略模式就是让使用其中行为中的一种
  • 一个系统需要动态的提供几种算法,根据不同的选择可以选定某种算法参与到系统应用中,也就是将具体算法类设置成都有统一的接口,根据多态的特点可以生成同算法实现的子类
  • 系统使用算法的数据不需要客户端知道,避免让客户端参与复杂的算法中去。
  • 如果一个对象有很多种行为,不使用模式的话,会根据不同的选择写很多条件判断代码来实现,这样就违反了OCP原则,且不易维护
  • 大小: 39.4 KB
  • 大小: 42.7 KB
0
0
分享到:
评论

相关推荐

    软件体系结构与设计模式-第七章策略模式ppt课件.ppt

    软件体系结构与设计模式-第七章策略模式ppt课件.ppt

    php设计模式介绍

    《PHP设计模式介绍》第七章 策略模式 《PHP设计模式介绍》第八章 迭代器模式 《PHP设计模式介绍》第九章 观测模式 《PHP设计模式介绍》第十章 规范模式 《PHP设计模式介绍》第十一章 章代理模式 《PHP设计模式...

    设计模式课程每章ppt(共27章).zip

    └── 设计模式 ├── 01_第1章_统一建模语言基础知识.ppt ├── 第10章_适配器模式.ppt ├── 第11章_桥接模式.ppt ├── 第12章_组合模式.ppt ...├── 第7章_建造者模式.ppt ├── 第8章_原型模式.ppt

    刘伟1..25章设计模式大集合

    01_第1章_统一建模语言基础知识.ppt 第11章_桥接模式.ppt 第12章_组合模式.ppt 第15章_享元模式.ppt 第18章_命令模式.ppt 第20章_迭代器模式.ppt ...第7章_建造者模式.ppt 第8章_原型模式.ppt 第9章_单例模式.ppt

    C#设计模式 C# Design Patterns:A Tutorial

    第7章 C#中的数组、文件和异常 第二部分 创建型模式 第8章 简单工厂模式 第9章 工厂方法模式 第10章 抽象工厂模式 第11章 单件模式 第12章 生成器模式 第13章 原型模式 第三部分 结构型模式 第14章 适配器模式 第15...

    研磨设计模式(完整带书签).part2.pdf

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    java设计模式

    第7章 单例模式 7.1 我是皇帝我独苗 7.2 单例模式的定义 7.3 单例模式的应用 7.4 单例模式的扩展 7.5 最佳实践 第8章 工厂方法模式 8.1 女娲造人的故事 8.2 工厂方法模式的定义 8.3 工厂方法模式的应用 8.3.1 工厂...

    设计模式解析-英文

    第1章 面向对象范型 2 第2章 UML 21 第二部分 传统面向对象设计的局限 第3章 对代码灵活性要求很高的问题 33 第4章 标准的面向对象解决方案 41 第三部分 设计模式 第5章 设计模式简介 53 第6章 Facade模式 65 第7章 ...

    研磨设计模式(完整带书签).part1.pdf

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    php程序设计模式chm文档

    本书为chm格式,共分为18章,主要以讲解代码的设计模式为主。 第一章 编程惯用法 第二章 值对象模式(The Value Object ...第七章 策略模式 ·· ··· ··· 有兴趣的自已下载看一看吧,比较高级,适合PHP开发人员

    [Java设计模式(第2版)(Design.Patterns.in.Java).John.Metsker

    第7章 职责型模式介绍 62 第8章 单例(singleton)模式 67 第9章 观察者(observer)模式 72 第10章 调停者(mediator)模式 85 第11章 代理(proxy)模式 97 第12章 职责链(chain of responsibility)模式 115 第13章 享元...

    Delphi模式编程第一分卷

    第7章 单例模式(Singleton) 7.1 模式解说 7.2 结构和用法 7.2.1 模式结构 7.2.2 代码模板 7.2.3 Delphi对象构造机制与单例模式 7.3 范例与实践 7.3.1 一个共享数据库连接的单例模式范例 7.3.2 范例小结 ...

    Delphi模式编程第二分卷

    第7章 单例模式(Singleton) 7.1 模式解说 7.2 结构和用法 7.2.1 模式结构 7.2.2 代码模板 7.2.3 Delphi对象构造机制与单例模式 7.3 范例与实践 7.3.1 一个共享数据库连接的单例模式范例 7.3.2 ...

    研磨设计模式-part2

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    研磨设计模式-part4

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    研磨设计模式-part3

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    ActionScript 3.0设计模式扫描版_方红琴译

    第7章 命令模式 什么是命令模式? 命令模式的最小化示例 命令模式中主要的OOP概念 简单的实例:宏命令 实例:控制数字的值 实例扩展:共享命令对象 实例扩展:实现恢复操作 实例:播客收音机 扩展实例:...

    研磨设计模式.part3(共4个)

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 ...

    研磨设计模式.part2(共4个)

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 ...

    研磨设计模式.part4(共4个)

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 ...

Global site tag (gtag.js) - Google Analytics