【编程思想】【设计模式】【行为模式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)
能够游走于聚合内的每一个元素,同时还可以提供多种不同的遍历方式. 基本概念: 就是提供一种方法顺序访问一个聚合对象中的各个元素,而不是暴露其内部的表示. 使用迭代器模式的优点: 遍历集合或者数 ...
随机推荐
- 微信小程序小窗无效
这里算是踩过一个坑吧 1.自己的调试版本库是否在这个版本或者以上 2.编辑器是不能看到小窗效果的,只能在真机运行 3.播放的内容是否有效,是否能播放 4.跳转页面时内容是否处于播放状态 5.当前页面是 ...
- VM的三种连接方式(转载)
概述: VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模 ...
- Part 29 AngularJS intellisense in visual studio
In the previous videos if you have noticed as we were typing the angular code in Script.js file we w ...
- c++学习笔记(十)
返回应用类型 返回引用 1.不要返回局部变量的引用 为了验证为什么不能返回局部变量的引用,我按照所学的例题自己做了一点小测试. #include<iostream> using names ...
- OOP 4.21晚 指针知识点
1.读法:int* ptr ptr是一个指针指向整型变量 2.指针类型:指针声明语句里的指针名字去掉,剩下的部分就是这个指针的类型; 3.指针所指向的类型:只须把指针声明语句中的指针名字和名字左边的指 ...
- .NET6运行时动态更新限流阈值
昨天博客园撑不住流量又崩溃了,很巧正在编写这篇文章,于是产生一个假想:如果博客园用上我这个限流组件会怎么样呢? 用户会收到几个429错误,并且多刷新几次就看到了内容,不会出现完全不可用. 还可以降低查 ...
- 【linux系统】命令学习(八)bash 编程实战学习
常见shell : bash sh zsh windows: git bash cygwin MAC : terminal iterm netstat 是linux下用于显示网络状态的命令.通 ...
- filter筛选数组
和map()类似,array的filter也接收一个函数 和map()不同的是,filter把传入的函数依次作用于每个函数,然后根据返回TRUE还是FALSE来做决定保留还是舍弃该元素 例如,删除一个 ...
- [loj3340]命运
容斥,强制若干条链不重要,即有$2^{n-1-s}$种(其中$s$为这些链的并所覆盖的边数),暴力将选中的链打标记,时间复杂度$o(m^{2}2^{m}+n\log_{2}n)$(预处理出这$2m$个 ...
- 测试平台系列(80) 封装Redis客户端
大家好~我是米洛! 我正在从0到1打造一个开源的接口测试平台, 也在编写一套与之对应的完整教程,希望大家多多支持. 欢迎关注我的公众号测试开发坑货,获取最新文章教程! 回顾 上一节我们编写了Redis ...