summaryrefslogtreecommitdiff
path: root/oop/dp/Decorator.py
blob: 390935a2a8e1cccfae11cc314b0f85ec7299daed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

from abc import ABCMeta, abstractmethod


class Person(metaclass=ABCMeta):
    def __init__(self, name):
        self._name = name

    @abstractmethod
    def wear(self):
        print('着装: ')


class Engineer(Person):

    def __init__(self, name, skill):
        super().__init__(name)
        self._skill = skill

    def wear(self):
        print(f'我是 {self._name} 工程师, 我会 {self._skill}')
        super().wear()


class ClothingDecorator(Person):
    def __init__(self, person: Person):
        self._decoratored = person

    def wear(self):
        self._decoratored.wear()
        self.decorate()

    @abstractmethod
    def decorate(self):
        pass


class CasualPantDecorator(ClothingDecorator):
    def __init__(self, person: Person):
        super().__init__(person)
    def decorate(self):
        print('一条卡其色裤子')


class BeltDecorator(ClothingDecorator):
    def __init__(self, person: Person):
        super().__init__(person)

    def decorate(self):
        print('一条黑色腰带')

if __name__ == '__main__':
    tony = Engineer('Tony', '算法')
    pant = CasualPantDecorator(tony)
    belt = BeltDecorator(pant)
    belt.wear()