Bouncer Pattern

http://groovy-lang.org/design-patterns.html#_bouncer_pattern

保镖模式主要负责对函数的输入参数的合法性检查, 如果遇到非法输出,则停止函数后续执行。

groovy提供了 assert 机制, 语言级别内置功能。

The Bouncer Pattern describes usage of a method whose sole purpose is to either throw an exception (when particular conditions hold) or do nothing. Such methods are often used to defensively guard pre-conditions of a method.

When writing utility methods, you should always guard against faulty input arguments. When writing internal methods, you may be able to ensure that certain pre-conditions always hold by having sufficient unit tests in place. Under such circumstances, you may reduce the desirability to have guards on your methods.

Groovy differs from other languages in that you frequently use the assert method within your methods rather than having a large number of utility checker methods or classes.

例子

void doStuff(String name, Object value) { assert name != null, 'name should not be null' assert value != null, 'value should not be null' // do stuff }

或者, 合法性检查, 检测出非法性, 主动 抛出异常。

class NumberChecker { static final String NUMBER_PATTERN = "\\\\d+(\\\\.\\\\d+(E-?\\\\d+)?)?" static isNumber(str) { if (!str ==~ NUMBER_PATTERN) { throw new IllegalArgumentException("Argument '$str' must be a number") } } static isNotZero(number) { if (number == 0) { throw new IllegalArgumentException('Argument must not be 0') } } }
def stringDivide(String dividendStr, String divisorStr) { NumberChecker.isNumber(dividendStr) NumberChecker.isNumber(divisorStr) def dividend = dividendStr.toDouble() def divisor = divisorStr.toDouble() NumberChecker.isNotZero(divisor) dividend / divisor } println stringDivide('1.2E2', '3.0') // => 40.0

转载于:https://www.cnblogs.com/lightsong/p/8724285.html

Groovy 设计模式 -- 保镖模式相关推荐

  1. 设计模式 | 外观模式及典型应用

    前言 本文的主要内容: 介绍外观模式 示例 自己泡茶 到茶馆喝茶 外观模式总结 外观模式的典型应用 spring JDBC 中的外观模式 Mybatis中的外观模式 Tomcat 中的外观模式 SLF ...

  2. Python设计模式-建造者模式

    Python设计模式-建造者模式 代码基于3.5.2,代码如下; #coding:utf-8 #建造者模式 class Burger():name = ""price = 0.0d ...

  3. Python设计模式-状态模式

    Python设计模式-状态模式 代码基于3.5.2,代码如下; #coding:utf-8 #状态模式class state():def writeProgram(self,work):raise N ...

  4. Python设计模式-备忘录模式

    Python设计模式-备忘录模式 代码基于3.5.2,代码如下; #coding:utf-8 #备忘录模式 import randomclass gameCharacter():vitality = ...

  5. Python设计模式-解释器模式

    Python设计模式-解释器模式 代码基于3.5.2,代码如下; #coding:utf-8 #解释器模式class PlayContext():play_text = Noneclass Expre ...

  6. Python设计模式-命令模式

    Python设计模式-命令模式 代码基于3.5.2,代码如下; #coding:utf-8 #命令模式class barbecuer():def bakeButton(self):print(&quo ...

  7. Python设计模式-策略模式

    Python设计模式-策略模式 代码基于3.5.2,代码如下; #coding:utf-8 #策略模式class sendInterface():def send(self,value):raise ...

  8. Python设计模式-外观模式

    Python设计模式-外观模式 代码基于3.5.2,代码如下; #coding:utf-8 # 外观模式class AlarmSensor:def run(self):print("Alar ...

  9. Python设计模式-桥接模式

    Python设计模式-桥接模式 基于Python3.5.2,代码如下 #coding:utf-8class Shape():name = ""param = "" ...

最新文章

  1. 一、Axis2 WebService开发准备工作
  2. 【 C 】指针数组案例分析(const的作用)
  3. html标签库jar包,struts2的s标签库jar包
  4. Extjs4 actioncolumn只能显示图标,不能显示文字的暂时解决方法
  5. 【机器视觉】 dev_set_preferences算子
  6. hdu 1569 方格取数(2) 最大点权独立集
  7. oracle 容器切换,oracle12c 多租户管理四(容器连接切换)
  8. python语言输入两个数_python的函数输入两个参数吗
  9. 赞!图像生成PyTorch库火了,涵盖18+ SOTA GAN实现
  10. nginx做下载文件服务器
  11. java同步mysql数据
  12. 三维医学图像数据标注 3D Slicer
  13. android图片的透明度变化,Android如何实现改变图片的透明度
  14. mac android usb驱动 win10,macbook安装Win10后无法创建引导的USB驱动器解决方法
  15. Scala中的fold和reduce理解
  16. windows服务器双网卡链路聚合_服务器双网卡捆绑与交换机链路聚合排障1例
  17. Verilog-移位操作(算术右移与逻辑右移)
  18. 无线降噪耳机品牌推荐,值得入手的四款降噪蓝牙耳机
  19. 新款macbook pro发布了,但为什么不推荐购买
  20. MAC下 Android签名生成keystore

热门文章

  1. Go gin文件上传
  2. Linux创建文件系统的命令及xfs文件系统介绍
  3. k8s pod和service的关系及常用service类型:ClusterIP/NodePort/LoadBalancer
  4. Python3序列解包
  5. Redis集群理论知识
  6. springboot配置文件的加载顺序(./config目录优先级最高)
  7. access窗体中再制作查询窗体_Access
  8. mysql团队开发工具_最棒的10款MySQL GUI工具
  9. hibernate-jpa/hibernate-jpa-2.1-api-1.0.0.final.jar源代码下载地址
  10. Android中点击事件的四种写法