【编程思想】【设计模式】【行为模式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 网络编程 - 总结概述
IP地址 IP地址IntAddress 唯一定位一台网络上的计算机 127.0.0.1:本地localhost IP地址的分类 ipV4/ipV6 ipV4:127.0.0.1,4个字节组成:0~25 ...
- 剑指 Offer 20. 表示数值的字符串
方法:分为几个部分判断 DA[.B][E/eC] D 其中D表示前后的空格,需要处理,跳过即可 A可以带正负号 有符号数 B无符号数 C可以为有符号数(带+-号) 小数点.后面必须是无符号数或者没有 ...
- 注解@RunWith的作用
@RunWith就是一个运行器 @RunWith(JUnit4.class)就是指用JUnit4来运行 @RunWith(SpringJUnit4ClassRunner.class),让测试运行于Sp ...
- 设计模式学习-使用go实现适配器模式
适配器模式 定义 代码实现 优点 缺点 适用范围 代理.桥接.装饰器.适配器4种设计模式的区别 参考 适配器模式 定义 适配器模式的英文翻译是Adapter Design Pattern.顾名思义,这 ...
- Part 28 AngularJS default route
At the moment the problem is that, if you try to navigate to a route that is not configured, you wil ...
- 不可忽视的Dubbo线程池
问题描述 线上突然出现Dubbo超时调用,时间刚好为Consumer端设置的超时时间. 有好几个不同的接口都报超时了 第1次调用超时,第2次(或第3次)重试调用非常快(正常水平) Dubbo调用超时的 ...
- 自定义 OpenShift s2i 镜像与模板——OracleJDK8
本文目标 由于 OpenShift 官方提供的镜像与模板(OpenJDK8)不完全满足业务需要: 不包含飞行记录功能.只有 OpenJDK11 以上才被 Oracle 开源 生成堆 dump 很大很慢 ...
- 同时在多个 Git 分支上工作,老板要榨干我
背景 上一篇文章 保持清洁的Git提交记录,三招就够了 ,大家看过后有私下留言说这是非常好用的功能,我突然想到工作中用到的另外一个 Git 功能那也是相当好用,必须全盘托出 作为程序员的我们应该都有一 ...
- 【Linux】(1)安装
VMware虚拟机安装Linux,IP地址显示为127.0.0.1的解决方案 ① 打开该虚拟机,点击导航栏"虚拟机(M)",选择"设置(S)..." ② 将&q ...
- ICCV2021 | Swin Transformer: 使用移位窗口的分层视觉Transformer
前言 本文解读的论文是ICCV2021中的最佳论文,在短短几个月内,google scholar上有388引用次数,github上有6.1k star. 本文来自公众号CV技术指南的论文分享系 ...