【编程思想】【设计模式】【行为模式Behavioral】Specification
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的更多相关文章
- 面向对象编程思想(前传)--你必须知道的javascript
在写面向对象编程思想-设计模式中的js部分的时候发现很多基础知识不了解的话,是很难真正理解和读懂js面向对象的代码.为此,在这里先快速补上.然后继续我们的面向对象编程思想-设计模式. 什么是鸭子类型 ...
- 面向对象编程思想(前传)--你必须知道的javascript(转载)
原文地址:http://www.cnblogs.com/zhaopei/p/6623460.html阅读目录 什么是鸭子类型 javascript的面向对象 封装 继承 多态 原型 this指向 ...
- Java编程思想重点笔记(Java开发必看)
Java编程思想重点笔记(Java开发必看) Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而 ...
- PHP设计模式-策略模式 转
策略模式(Strategy Pattern) 策略模式是对象的行为模式,用意是对一组算法的封装.动态的选择需要的算法并使用. 策略模式指的是程序中涉及决策控制的一种模式.策略模式功能非常强大,因为这个 ...
- .NET设计模式: 工厂模式
.NET设计模式: 工厂模式(转) 转自:http://www.cnblogs.com/bit-sand/archive/2008/01/25/1053207.html .NET设计模式(1): ...
- 面向对象编程思想(OOP)
本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性.七大设计原则和23种设计模式之间的关系. 软件开发中疑难问题: 软件复杂庞 ...
- java编程思想
Java编程思想,Java学习必读经典,不管是初学者还是大牛都值得一读,这里总结书中的重点知识,这些知识不仅经常出现在各大知名公司的笔试面试过程中,而且在大型项目开发中也是常用的知识,既有简单的概念理 ...
- Java编程思想(11~17)
[注:此博客旨在从<Java编程思想>这本书的目录结构上来检验自己的Java基础知识,只为笔记之用] 第十一章 持有对象 11.1 泛型和类型安全的容器>eg: List<St ...
- 设计模式 --迭代器模式(Iterator)
能够游走于聚合内的每一个元素,同时还可以提供多种不同的遍历方式. 基本概念: 就是提供一种方法顺序访问一个聚合对象中的各个元素,而不是暴露其内部的表示. 使用迭代器模式的优点: 遍历集合或者数 ...
随机推荐
- 【Java】String和List类型转换
String & List String ==>List //方法一 List<String> list = Arrays.asList(str.split(",& ...
- NCF 中如何将Function升级到FunctionRender
简介 历史的车轮在不断的向前推进,NCF也在不断的迭代更新,只为成为更好的NCF 如果你之前没有用过NCF可以跳过这个文档,直接去下载最新的NCF源码进行实践. NCF仓库地址:https://git ...
- 基于I2C的AHT20温湿度传感器的数据采集
关于:IC( Inter-- Integrated Circuit)总线是一种由 PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备.它是由数据线SDA和时钟SCL构成的串行总线,可 ...
- 删除html标签,取其中的文本
public String removeHtmlTags() { String str = "<p><b> welcome to test</b>< ...
- 2021 祥云杯 wp
52 web ezyii https://pan.baidu.com/s/1j7IJm9xiea5FvBhPMkPNoQ 提取码GAME <?php include("closure/ ...
- 南大《软件分析》课程笔记——Intermediate Representation
南大<软件分析>--Intermediate Representation @(静态分析) Content 编译器和静态分析的关系 AST vs IR IR:3-地址代码(3AC) 实际静 ...
- [atARC121F]Logical Operations on Tree
(特判$n=1$的情况) 当确定权值和操作后,如何判定是否合法-- 考虑一个度为1的节点,对其权值即其对应边的边操作分类讨论: $1\or$,显然只需要最后选择这条边即可,一定合法 $1\and$或$ ...
- [bzoj1068]压缩
用f[i][j][0/1]表示区间[i,j],i之前有没有M的最少需要多少个字符,然后分两种情况:1.可以分为两个,转移到dp[l][mid][0]+1:2.枚举断点,但当前面有M时,后面的这个不能重 ...
- ant命令
ant -help 帮助(ant -h) ant -projecthelp 列举xml中重要的部分 (ant -p) ant -version 查看版本 ant -diagnostics 打印所有环境 ...
- 基于nexus私服配置项目pom.xml和maven settings.xml文件
备注:搭建nexus私服请参考上一篇文章基于Docker搭建Maven私服Nexus,Nexus详解 一:将jar发送到nexus私服务器 1.pom.xml文件添加配置 pom.xml文件中的这个版 ...