Chapter 2.策略模式
首先贴一段代码:
package xiao;
import java.util.Scanner;
class CashSuper{
private int num;
private double price;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double acceptCash(double money){
return money;
}
}
class CashNormal extends CashSuper{
}
class CashRebate extends CashSuper{
private double rebate;
public CashRebate(double rebate){
this.rebate = rebate;
}
public double getRebate() {
return rebate;
}
public void setRebate(double rebate) {
this.rebate = rebate;
}
public double acceptCash(double money){
return money*rebate;
}
}
class CashReturn extends CashSuper{
private double moneyCondition;
private double moneyReturn;
public CashReturn(double moneyCondition,double moneyReturn){
this.moneyCondition = moneyCondition;
this.moneyReturn = moneyReturn;
}
public double getMoneyCondition() {
return moneyCondition;
}
public void setMoneyCondition(double moneyCondition) {
this.moneyCondition = moneyCondition;
}
public double getMoneyReturn() {
return moneyReturn;
}
public void setMoneyReturn(double moneyReturn) {
this.moneyReturn = moneyReturn;
}
public double acceptCash(double money){
if(money > moneyCondition){
return money - Math.floor(money/moneyCondition)*moneyReturn;
}else{
return money;
}
}
}
class CashFactory{
public static CashSuper creatCash(String select){
CashSuper cash = new CashSuper();
Scanner in = new Scanner(System.in);
switch(select){
case"normal":
cash = new CashNormal();
break;
case"rebate":
System.out.print("please enter the rebate: ");
double rebat = in.nextDouble();
cash = new CashRebate(rebat);
break;
case"return":
System.out.print("please enter the moneyCondition: ");
double moneyCondition = in.nextDouble();
System.out.print("please enter the moneyReturn: ");
double moneyReturn = in.nextDouble();
cash = new CashReturn(moneyCondition,moneyReturn);
break;
}
in.close();
return cash;
}
}
class CashContext{
CashSuper cs =null;
Scanner in = new Scanner(System.in);
public CashContext(String type){
switch(type){
case"normal":
CashNormal cash0 = new CashNormal();
cs = cash0;
break;
case"rebate":
System.out.print("please enter the rebate: ");
double rebat = in.nextDouble();
CashRebate cash1 = new CashRebate(rebat);
cs = cash1;
break;
case"return":
System.out.print("please enter the moneyCondition: ");
double moneyCondition = in.nextDouble();
System.out.print("please enter the moneyReturn: ");
double moneyReturn = in.nextDouble();
CashReturn cash2 = new CashReturn(moneyCondition,moneyReturn);
cs = cash2;
break;
}
}
public double GetResult(double money){
return cs.acceptCash(money);
}
}
public class TestDemo{
public static void main(String[] args)throws Exception{
Scanner in = new Scanner(System.in);
System.out.print("please enter the price: ");
double price = in.nextDouble();
System.out.print("please enter the num: ");
int num = in.nextInt();
System.out.print("please enter the select: ");
String select = in.next();
CashContext csuper = new CashContext(select);
System.out.println(csuper.GetResult(num*price));
in.close();
}
}
策略模式:一种定义一系列算法的方法。所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。
策略模式是用来封装算法的,只要在分析过程中听到需要再不同时间应用不同的业务规则,就可以考虑。
Chapter 2.策略模式的更多相关文章
- C++ 之 策略模式
1 会飞的鸭子 Duck 基类,包含两个成员函数 swim() 和 display():派生类 MallardDuck,RedheadDuck 和 RubberDuck,各自重写 display() ...
- Python设计模式: 最佳的"策略"模式实践代码
Python设计模式: 最佳的"策略"模式实践代码 今天抽空看了下流畅的python,发现里面介绍了不少python自带的库的使用实例,用起来非常的优雅. 平时用Python来写爬 ...
- javascript设计模式:策略模式
前言 策略模式有效利用组合.委托.多态等技术和思想,可以有效避免多重条件选择语句. 策略模式对开放-封闭原则提供了很好的支持,将算法封装在strategy中,使得他们易于切换.理解.扩展. 策略模式中 ...
- StrategyPattern (策略模式)
/** * 策略模式 * @author TMAC-J * 根据环境的不同选择不同的策略,把策略用接口抽象出来 */ public class StrategyPattern { interface ...
- JAVA 设计模式之策略模式
定义:定义一组算法,将每个算法都封装起来,并且使他们之间可以互换. 类型:行为类模式 策略模式是对算法的封装,把一系列的算法分别封装到对应的类中,并且这些类实现相同的接口,相互之间可以替换.在前面说过 ...
- Java设计模式之策略模式(Strategy)
前言: 最近一直在学习基于okHttp网络请求,学习的过程中就想起了之前项目中有这么一个需求不同的接口要采用不同的加密方式,比如登录之前要采用RSA加密,登录之后要采用AES加密,当时是采用靠传递一个 ...
- 设计模式(一):“穿越火线”中的“策略模式”(Strategy Pattern)
在前段时间呢陆陆续续的更新了一系列关于重构的文章.在重构我们既有的代码时,往往会用到设计模式.在之前重构系列的博客中,我们在重构时用到了“工厂模式”.“策略模式”.“状态模式”等.当然在重构时,有的地 ...
- 《Head First 设计模式》之策略模式
作者:Grey 原文地址:http://www.cnblogs.com/greyzeng/p/5915202.html 模式名称 策略模式(Strategy Pattern) 需求 模拟鸭子游戏,游戏 ...
- 学C#之设计模式系列笔记(1)策略模式
一.借鉴说明 1.<Head First Design Patterns>(中文名<深入浅出设计模式>) 2.维基百科,策略模式,https://zh.wikipedia.or ...
随机推荐
- wsdl透明解析
1.逐个分析wsdl文件中的元素: <types>:数据类型定义的容器,一般使用 xml schema类型系统. <message>:通信消息的数据结构的抽象化定义,使用< ...
- inline-block 垂直居中
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- $.ajax和vue-resource实现OAuth
Vue.js——使用$.ajax和vue-resource实现OAuth的注册.登录.注销和API调用 概述 上一篇我们介绍了如何使用vue resource处理HTTP请求,结合服务端的REST A ...
- SQL server 数据库视频总结
用了半个多月的时间把,浙江大学耿建玲老师 数据库视频看了一遍.在看视频之前,曾经接收了一个学生信息管理系统,在学习 学生信息管理系统的时候,对于数据库的部分,总是那么一知半解.带着疑惑来看耿建玲老师 ...
- [置顶] WEBSOKET服务器搭建
简单介绍一下tomcat的webSocketAPI使用: 在这里啰嗦几句:[ 很多朋友听说webSocket不知道是什么.知道是什么不知道怎么用,知道怎么用不知道具体实现.其实我当初也是这样. 实际上 ...
- Mybatis 简单的CRUD 基于XML文件配置
全部的ORM框架学习曲线都是先来一个CRUD爽一爽,以下我们就来CRUD一下,全部的配置都是基于上一篇的配置.废话不多说,直接上代码. <?xml version="1.0" ...
- ajax的简单操作
项目需要,简单研究了下ajax 需要在html中引入js文件 编写js函数 function testAjax() { $.ajax({ type: 'get', //请求方式 get/post ur ...
- iOS开发之第三方登录QQ -- 史上最全最新第三方登录QQ方式实现
项目地址 : https://github.com/zhonggaorong/QQLoginDemo/tree/master 最新版本的qq登录实现步骤实现: 1. 首先,你需要去向腾讯申请账号. ...
- 解决了clang: error: linker command failed with exit code 1 (use -v to see invocation) 解决方法
1.”Build Settings”->”Enable Bitcode”设置为NO 2.TARGETS --> Build Settings --> Architectures - ...
- Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...