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. Screenshot 库和Collections 库

    一.screenShot 是 robot framework的标准类库,用于截取当前窗口,需要手动加载. 示例: 运行结果: 二.Collections 库 Collections 库同样为 Robo ...

  2. ABAP——系统状态&用户状态修改、查询

    前言:在ABAP开发中有时候会涉及到状态的变更,比如销售订单的系统状态变更未审批->已审批.设备的在运->报废等,在这里就需要用到标准函数I_CHANGE_STATUS.STATUS_CH ...

  3. Unity——技能系统(三)

    Unity技能系统(三) Unity技能系统(一) Unity技能系统(二) Demo展示 六.Buff系统 buff分为增益和减益buff,应该区分开来: /// <summary> / ...

  4. Rancher 下图形界面 搭建 K8S 集群

    首先我们准备4台 2核3G 的 centos 7 温馨提示:先安装好一台 CentOS 的虚拟机,并且安装好 docker,永久关闭防火墙. 再这个基础上我们分别克隆出四台 Rancher.K8S1. ...

  5. redis sentinel搭建

    /usr/local/bin /usr/local/etc https://www.centos.bz/2017/08/redis-3-x-sentinel-ha-service/ https://w ...

  6. mongodb(一)

    文档是 MongoDB 的核心,类似于 SQLite 数据库(关系数据库)中的每一行数据.多个键及其关联的值放在一起就是文档.在 Mongodb 中使用一种类 json 的 bson 存储数据,bso ...

  7. php多域名跳转nginx

    当web服务器nginx已经配置好了网站根目录时,需要增加另外的域名.但是由于限制必须在原来的网站根目录下面,nginx已经无法实现.只能通过php index页面进行调试.如下面: define(' ...

  8. Docker 之 Dockerfile 常用语法与实战

    1. 概述 老话说的好:超越别人,不如超越自我,每天比昨天的自己更强就好. 言归正传,之前聊了 Docker 的相关知识,今天来聊聊如何编辑 Dockerfile 脚本,来创建我们自己的镜像. 2. ...

  9. asList和subList的缺陷

    概述 在开发中我们经常使用asList去把一个数组转换为List.也存在通过subList.subMap.来对List.Map.Set来进行类似使用subString方法来对String对象进行分割处 ...

  10. js防止重复提交代码

    if (checkSubmitFlg == true) { console.log("禁止频繁操作.."); layer.close(ide); return false; } c ...