其他分享
首页 > 其他分享> > 【设计模式】装饰器模式

【设计模式】装饰器模式

作者:互联网

from abc import ABC, abstractmethod


class PriceComputer(ABC):

    @abstractmethod
    def compute_price(self):
        pass


class Goods(PriceComputer):

    def __init__(self, price):
        self.price = price

    def compute_price(self):
        return self.price


class Discount(PriceComputer):

    def __init__(self, price_computer, discount):
        self.price_computer = price_computer
        self.discount = discount

    def compute_price(self):
        return self.price_computer.compute_price() * self.discount


class RedPaper(PriceComputer):

    def __init__(self, price_computer, red_equity):
        self.price_computer = price_computer
        self.red_equity = red_equity

    def compute_price(self):
        return self.price_computer.compute_price() - self.red_equity


class Coupon(PriceComputer):

    def __init__(self, price_computer, coupon_equity):
        self.price_computer = price_computer
        self.coupon_equity = coupon_equity

    def compute_price(self):
        return self.price_computer.compute_price() - self.coupon_equity

if __name__ == '__main__':
    goods = Coupon(RedPaper(Discount(Goods(100), 0.8), 5), 2)
    print(goods.compute_price())

标签:__,compute,price,模式,self,computer,设计模式,装饰,def
来源: https://blog.csdn.net/qq_44647926/article/details/122491478