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

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关于特性的两个基本设计模式)