欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 编程语言 > asp.net >内容正文

asp.net

groovy 设计模式 -尊龙游戏旗舰厅官网

发布时间:2024/10/12 asp.net 31 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 groovy 设计模式 -- 保镖模式 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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 设计模式 -- 保镖模式的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图