PyPattyrn-一个简单而有效的python库,用于实现常见的设计模式

PyPattyrn是一个python软件包,旨在使您更轻松,更快地将设计模式实现到您自己的项目中。

设计模式本质上不能直接转换为代码,因为它们只是对如何解决特定问题的描述。但是,许多常见的设计模式都具有在该模式的所有实现中通用的样板代码。该程序包捕获了该通用代码并使其易于使用,因此您不必在所有项目中都自己编写它。

设计模式

在软件工程中, 设计模式是解决软件设计中常见问题的通用可重复解决方案。设计模式不是可以直接转换为代码的最终设计。它是关于如何解决可以在许多不同情况下使用的问题的描述或模板。

  

设计模式的用途

设计模式可以通过提供经过测试的,成熟的开发范例来加快开发过程。有效的软件设计需要考虑可能直到实施稍后才能看到的问题。重用设计模式有助于防止可能引起重大问题的细微问题,并提高熟悉模式的编码人员和架构师的代码可读性。

通常,人们只了解如何将某些软件设计技术应用于某些问题。这些技术很难应用于更广泛的问题。设计模式提供了通用的解决方案,以不需要特定细节的格式记录下来。

另外,模式允许开发人员使用众所周知的,易于理解的名称进行软件交互。常见的设计模式可以随着时间的流逝而得到改进,使其比临时设计更健壮。

PyPattyrn代码示例

责任链模式

沿对象链传递请求,直到处理该请求。

from pypattyrn.behavioral.chain import Chain, ChainLinkclass ConcreteChainLinkThree(ChainLink): # This object is a ChainLink

    def handle(self, request): # Implement the handle method.
        if request == 'handle_three':            return "Handled in chain link three"
        else:            return self.successor_handle(request) # If this ChainLink can't handle the request, 
                                                  # ask its successor to handle it. 
                                                  # (Has no successor so will raise AttributeError)
                                                  # (This exception is caught and will call a Chains fail method)class ConcreteChainLinkTwo(ChainLink): # This object is a ChainLink

    def __init__(self): # Override init to set a successor on initialization.
        super().__init__() # first call ChainLinks init
        self.set_successor(ConcreteChainLinkThree()) # Set the successor of this chain link 
                                                     # to a ConcreteChainLinkThree instance.
    def handle(self, request): # Implement the handle method.
        if request == 'handle_two':            return "Handled in chain link two"
        else:            return self.successor_handle(request) # If this ChainLink can't handle a request 
                                                  # ask its successor to handle it 
                                                  # (the ConcreteChainLinkThree instance).class ConcreteChainLinkOne(ChainLink): # This object is a ChainLink

    def __init__(self): 
        super().__init__()        self.set_successor(ConcreteChainLinkTwo()) # Set the successor of this ChainLink
                                                   # to a ConcreteChainLinkTwo instance.
    def handle(self, request): # Implement the handle method.
        if request == 'handle_one':            return "Handled in chain link one"
        else:            return self.successor_handle(request) # If this ChainLink can't handle a request 
                                                  # ask its successor to handle it 
                                                  # (the ConcreteChainLinkTwo instance).class ConcreteChain(Chain): # This object is a Chain

    def __init__(self): # Override init to initialize a Chain with the starting chain link.
        super().__init__(ConcreteChainLinkOne()) # Initialize this Chain with a start chain link.
                                                 # (a ConcreteChainLinkOne instance)

    def fail(self): # Implement the fail method, this is called if no chain links could handle a request.
        return 'Fail'chain = ConcreteChain()assert "Handled in chain link one" == chain.handle("handle_one")assert "Handled in chain link two" == chain.handle("handle_two")assert "Handled in chain link three" == chain.handle("handle_three")assert "Fail" == chain.handle('handle_four')

PyPattyrn安装

pip install pypattyrn 或者下载pypattyrn的网盘备份版本

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值