Python版

https://github.com/faif/python-patterns/blob/master/behavioral/specification.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- """
@author: Gordeev Andrey <gordeev.and.and@gmail.com> *TL;DR80
Provides recombination business logic by chaining together using boolean logic.
""" from abc import abstractmethod class Specification(object): def and_specification(self, candidate):
raise NotImplementedError() def or_specification(self, candidate):
raise NotImplementedError() def not_specification(self):
raise NotImplementedError() @abstractmethod
def is_satisfied_by(self, candidate):
pass class CompositeSpecification(Specification): @abstractmethod
def is_satisfied_by(self, candidate):
pass def and_specification(self, candidate):
return AndSpecification(self, candidate) def or_specification(self, candidate):
return OrSpecification(self, candidate) def not_specification(self):
return NotSpecification(self) class AndSpecification(CompositeSpecification):
_one = Specification()
_other = Specification() def __init__(self, one, other):
self._one = one
self._other = other def is_satisfied_by(self, candidate):
return bool(self._one.is_satisfied_by(candidate) and
self._other.is_satisfied_by(candidate)) class OrSpecification(CompositeSpecification):
_one = Specification()
_other = Specification() def __init__(self, one, other):
self._one = one
self._other = other def is_satisfied_by(self, candidate):
return bool(self._one.is_satisfied_by(candidate) or
self._other.is_satisfied_by(candidate)) class NotSpecification(CompositeSpecification):
_wrapped = Specification() def __init__(self, wrapped):
self._wrapped = wrapped def is_satisfied_by(self, candidate):
return bool(not self._wrapped.is_satisfied_by(candidate)) class User(object): def __init__(self, super_user=False):
self.super_user = super_user class UserSpecification(CompositeSpecification): def is_satisfied_by(self, candidate):
return isinstance(candidate, User) class SuperUserSpecification(CompositeSpecification): def is_satisfied_by(self, candidate):
return getattr(candidate, 'super_user', False) if __name__ == '__main__':
print('Specification')
andrey = User()
ivan = User(super_user=True)
vasiliy = 'not User instance' root_specification = UserSpecification().\
and_specification(SuperUserSpecification()) print(root_specification.is_satisfied_by(andrey))
print(root_specification.is_satisfied_by(ivan))
print(root_specification.is_satisfied_by(vasiliy)) ### OUTPUT ###
# Specification
# False
# True
# False

Python转载版

【编程思想】【设计模式】【行为模式Behavioral】Specification的更多相关文章

  1. 面向对象编程思想(前传)--你必须知道的javascript

    在写面向对象编程思想-设计模式中的js部分的时候发现很多基础知识不了解的话,是很难真正理解和读懂js面向对象的代码.为此,在这里先快速补上.然后继续我们的面向对象编程思想-设计模式. 什么是鸭子类型 ...

  2. 面向对象编程思想(前传)--你必须知道的javascript(转载)

    原文地址:http://www.cnblogs.com/zhaopei/p/6623460.html阅读目录   什么是鸭子类型 javascript的面向对象 封装 继承 多态 原型 this指向 ...

  3. Java编程思想重点笔记(Java开发必看)

    Java编程思想重点笔记(Java开发必看)   Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而 ...

  4. PHP设计模式-策略模式 转

    策略模式(Strategy Pattern) 策略模式是对象的行为模式,用意是对一组算法的封装.动态的选择需要的算法并使用. 策略模式指的是程序中涉及决策控制的一种模式.策略模式功能非常强大,因为这个 ...

  5. .NET设计模式: 工厂模式

    .NET设计模式: 工厂模式(转) 转自:http://www.cnblogs.com/bit-sand/archive/2008/01/25/1053207.html   .NET设计模式(1): ...

  6. 面向对象编程思想(OOP)

    本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性.七大设计原则和23种设计模式之间的关系. 软件开发中疑难问题: 软件复杂庞 ...

  7. java编程思想

    Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而且在大型项目开发中也是常用的知识,既有简单的概念理 ...

  8. Java编程思想(11~17)

    [注:此博客旨在从<Java编程思想>这本书的目录结构上来检验自己的Java基础知识,只为笔记之用] 第十一章 持有对象 11.1 泛型和类型安全的容器>eg: List<St ...

  9. 设计模式 --迭代器模式(Iterator)

    能够游走于聚合内的每一个元素,同时还可以提供多种不同的遍历方式.   基本概念: 就是提供一种方法顺序访问一个聚合对象中的各个元素,而不是暴露其内部的表示.   使用迭代器模式的优点: 遍历集合或者数 ...

随机推荐

  1. JavaScript 对象:String & Array 及其常见应用

    String对象 split 功能:把字符串分割为字符串数组.官方文档已经描述的够清楚,不多赘述.主要说一下需要注意的情况以及应用 1.省略分割参数 var str="How are you ...

  2. dart系列之:dart语言中的函数

    目录 简介 函数的参数 main函数 匿名函数 闭包 函数的返回值 总结 简介 函数是所有编程语言都有的内容,不管是面向对象还是面向过程,函数都是非常重要的一部分.dart中的函数和java中的函数有 ...

  3. 第三天 while循环 及其用法

    (1)语法格式:while  条件: ..... 语法二:while  条件: break  # 跳出当前循环 语法三:while 条件: else  # 当while循环正常结束时执行该语句:只有程 ...

  4. 部署一个支持Dapr 的Kubernetes APISIX Ingress

    在这篇文章中,我将展示如何创建一个 APISIX控制器,该控制器在 Kubernetes 集群中公开启用 Dapr 的应用程序. 本质上,APISIX控制器将配置相同的标准 Dapr annotati ...

  5. 全面的Docker快速入门教程

    前言: 都2021年了,你还在为了安装一个开发或者部署环境.软件而花费半天的时间吗?你还在解决开发环境能够正常访问,而发布测试环境无法正常访问的问题吗?你还在为持续集成和持续交付(CI / CD)工作 ...

  6. .NET6运行时动态更新限流阈值

    昨天博客园撑不住流量又崩溃了,很巧正在编写这篇文章,于是产生一个假想:如果博客园用上我这个限流组件会怎么样呢? 用户会收到几个429错误,并且多刷新几次就看到了内容,不会出现完全不可用. 还可以降低查 ...

  7. web页面自动化总结。selenium

    web自动化测试终篇:总结我理解的ui自动化,整理归纳: https://blog.csdn.net/CCGGAAG/article/details/89669592 web页面自动化知识点 1.we ...

  8. 【Design Patterns】(1)概述

    设计模式 -- 概述 2019-07-17  22:43:32  by冲冲 1. 简介 ① 设计模式 是软件开发人员在软件开发过程中,针对一般问题的最佳解决方案,该方案能够被程序员反复应用于解决类似问 ...

  9. Electron快速入门之debug

    view->toggleDevelpper Tools 本地桌面调试 浏览器debug "start": "electron --inspect=5858 .&qu ...

  10. es的rest风格的api文档

    rest风格的api put http://127.0.0.1:9200/索引名称/类型名称/文档id (创建文档,指定文档id) post http://127.0.0.1:9200/索引名称/类型 ...