zoukankan      html  css  js  c++  java
  • 【编程思想】【设计模式】【行为模式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转载版
  • 相关阅读:
    Oracle数据库的左连接和右连接(+)
    Web文件上传模块 Plupload
    增加反向链接的35个技巧
    google map api 与jquery结合使用(1)控件,监听器[转帖]
    教你在windows 7/xp 下安装使用mencoder
    Oracle 全文索引
    提高关键词排名的28个SEO技巧
    二叉树遍历及C语言实现
    小额担保业务管理系统详细设计介绍
    C#与数据结构二叉树的遍历
  • 原文地址:https://www.cnblogs.com/demonzk/p/9035662.html
Copyright © 2011-2022 走看看