美文网首页
python关于特性的两个基本设计模式

python关于特性的两个基本设计模式

作者: 第八共同体 | 来源:发表于2018-01-06 16:32 被阅读0次

1 .主动模式

每当更新特性值时,其他相关特性值都会立即被重新计算

class Hand_Eager(Hand):

    def __init__(self, dealer_card, *cards):
        self.dealer_card = dealer_card
        self._cards = list()
        self.total = 0
        self._delta_soft = 0
        self._hard_total = 0
        for c in cards:
            self.card = c


    @property
    def card(self):
        return self._cards

    @card.setter
    def card(self, aCard):
        self._cards.append(aCard)
        self._delta_soft = max(aCard.soft-aCard.hard, self._delta_soft)
        self._hard_total += aCard.hard
        self._set_total()

    @card.deleter
    def card(self):
        removed = self._cards.pop(-1)
        self._hard_total -= removed.hard
        self._delta_soft = max(c.soft-c.hard for c in self._cards)
        self._set_total()


    def _set_total(self):
        if self._hard_total + self._delta_soft <=21:
            self.total = self._hard_total + self._delta_soft
        else:
            self.total = self._hard_total

2.延迟计算

仅当访问特性时,才会触发计算过程

class HandLazy(Hand):

    def __init__(self, dealer_card, *cards):
        self.dealer_card = dealer_card
        self._cards = list(cards)

    @property
    def total(self):
        delta_soft = max(c.soft - c.hand for c in self._cards)
        hard_total = sum(c.hard for c in self._cards)
        if hard_total + delta_soft <=12: return hard_total + delta_soft
        return hard_total

    @property
    def card(self):
        return  self._cards
    @card.setter
    def card(self, aCard):
        self._cards.append(aCard)

    @card.deleter
    def card(self):
        self._cards.pop(-1)

父类Hand

class Hand:

    def __str__(self):
        return ", ".join(map(str, self.card))
    def __repr__(self):
        return "{__class__.__name__}({dealer_card!r}, {_cards_str})".\
            format(___class__=self.__class__,
                   _cards_str=", ".join(map(repr, self.card)),
                   **self.__dict__)

相关文章

  • python关于特性的两个基本设计模式

    1 .主动模式 每当更新特性值时,其他相关特性值都会立即被重新计算 2.延迟计算 仅当访问特性时,才会触发计算过程...

  • 2018-04-28

    24种设计模式: 创建型模式: 结构型模式: 行为型模式: 软件开发中疑难问题: 四大基本特性: 七大设计原则

  • Fluent Python笔记--一等函数与设计模式

    关于设计模式的讨论,已经有了许多非常好的文章阐述和示例代码实现。但是,鉴于Python的独特语言特性,还是有一些需...

  • python - OOP进阶

    前言 python的class有很多高级特性,除了OOP的三大特性,还在设计模式,自定制类和内存优化等都下了很多功...

  • iOS最新大厂面试题整理

    iOS开发面试梳理(一) OC的理解与特性简述内存管理基本原则如何理解MVC设计模式如何理解MVVM设计模式Obj...

  • 设计模式演变过程

    基本设计模式之MVC模式 基本设计模式之MVP模式 基本设计模式之MVVM模式 SPA和MPA SPA:单页面应用...

  • day2 认识python,python的基础语法和变量

    一、认识python 1、python的特性 a 可移植 b 可扩展 c 互动模式 2、python的优缺...

  • php设计模式(一)单例、工厂

    我们首先讲,单例、工厂模式,两个最最基本的设计模式 设计模式中常用的魔术方法 __get/__set 访问不存在的...

  • 二十三种设计模式及其python实现

    参考文献: 《大话设计模式》——吴强 《Python设计模式》——pythontip.com 《23种设计模式》—...

  • 基础-单例模式

    单例模式总结-Python实现 面试里每次问设计模式,必问单例模式 来自《Python设计模式》(第2版) 1.理...

网友评论

      本文标题:python关于特性的两个基本设计模式

      本文链接:https://www.haomeiwen.com/subject/srbynxtx.html