定义一系列的算法,把每一个算法封装起来,并且使它们可相互替换。策略模式使 得算法可独立于使用它的客户而独立变化。
策略接口
public interface FightingStrategy {
public void fighting();
}
具体策略实现
public class WeakRivalStrategy implements FightingStrategy {
@Override
public void fighting() {
System.out.println("遇到了较弱的对手,张无忌使用太极剑");
}
}
public class CommonRivalStrategy implements FightingStrategy {
@Override
public void fighting() {
System.out.println("遇到了普通的对手,张无忌使用圣火令神功");
}
}
public class StrongRivalStrategy implements FightingStrategy {
@Override
public void fighting() {
System.out.println("遇到了强大的对手,张无忌使用乾坤大挪移");
}
}
上下文角色
通过传进来不同的具体策略来调用不同策略的 fighting 方法。
public class Context {
private FightingStrategy fightingStrategy;
public Context(FightingStrategy fightingStrategy) {
this.fightingStrategy = fightingStrategy;
}
public void fighting(){
fightingStrategy.fighting();
}
}
客户端调用
public static void main(String[] args) {
Context context;
//张无忌遇到对手宋青书,采用对较弱对手的策略
context = new Context(new WeakRivalStrategy());
context.fighting();
//张无忌遇到对手灭绝师太,采用对普通对手的策略
context = new Context(new CommonRivalStrategy());
context.fighting();
//张无忌遇到对手成昆,采用对强大对手的策略
context = new Context(new StrongRivalStrategy());
context.fighting();
}
使用场景:
优点:
缺点:
目标json字符串:{"error":"","data":[{"fid":"1","forums": [{"fid":"1","name":"Discuz!","threads":"0","posts":"0","todayposts":"0","type":"group","lastpost":""}, {"fid":"2","name":"默认版块","threads":"1","posts"
fs = 44100; % sample ratedt = 1/fs;T16 = 0.125;t16 = [0:dt:T16];[temp k] = size(t16);t4 = linspace(0,4*T16,4*k);t8 = linspace(0,2*T16,2*k);[temp i] = size(t4);[temp j] = size(t8);% Modification functi...
有很多同学看了本系列的前几篇之后建议我暂时先不用TS,于是小肆之后将把TS换成JS继续下面的文章。今天给大家带来项目中非常重要的一环,配置Axios,一起来看看吧。axios 简介首先要明白的是axios是什么:axios是基于promise(诺言)用于浏览器和node.js是http客户端。axios的作用是什么呢:axios主要是用于向后台发起请求的,还有在请求中做更多是可控功能。...
回收键盘之UITextField和UITextViewUITextField回收的代理方法- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textFieldresignFirstResponder];//取消textFild的第一响应 第一响应者通常是用户据当前正在交互的对象 retu
Rotating Sentences In ``Rotating Sentences,'' you are asked to rotate a series of input sentences 90 degrees clockwise.So instead of displaying the input sentences from left to right and t
创建股票数据库#!/usr/bin/env python# -*- coding: utf-8 -*-# @Date : 2018-09-04 14:34:59# @Author : Michael Li # @Version : $V2.0$ import pandas as pdimport numpy as npimport datetimeimport randomimport pymssqlfrom sqlalchemy import create_engi.
一、依赖<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version></dependency> swagger3跟2不同只需导入一个这个依赖就行,不需要另外ui等依赖二、配置启动类添加@Enabl.
主席树学习笔记说在前边:之前了解过主席树的基础的思想,但是没有系统学习过,所以打算通过一些题目重新学习。POJ2104题意:静态区间查询 k-th number思路:对每个位置开一颗权值线段树,维护前缀区间每个数字出现的次数,这样只需实现两棵线段树相减,利用简单的二分思想即可求出 k-th number。问题:内存不足以开n颗线段树,时间自然也不够。解决方法:注意...
深入理解socket中的recv函数和send函数
RewriteEngine OnRewriteBase /# Rewrite规则RewriteRule ^index.htm$ index.phpRewriteRule ^jobs/jobs-show-([0-9]+).htm$ jobs/jobs-show.php?id=$1RewriteRule ^jobs/jobs-list-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*...
在本系列的前三篇自动驾驶中间件之一:AUTOSAR正在被“边缘化”?自动驾驶中间件之二:通信中间件,DDS与SOME/IP 谁主沉浮?自动驾驶中间件之三:活跃玩家盘点中,我们已简单介绍了...
最近SSH肤浅的学了一遍后,也算自己向全栈迈出了重要的一步。现在学SSH框架大多以网站开发为例。像我这种做APP出身的,就特想实现跟服务器的通信。 要解决的问题就是如何接受APP传来的数据(一般为json格式)和返回数据(一般为json格式)。下面就进行一下说明。 如何接受APP传来的数据: 我是让action实现了ServletRequestAware这个接口,然后重写了其方法获取reque