summaryrefslogtreecommitdiff
path: root/oop/dp
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2022-03-25 00:11:34 +0800
committerzhang <zch921005@126.com>2022-03-25 00:11:34 +0800
commit3ed1c5d0e4018fc70012a6209a859a059f7127b5 (patch)
tree2ca318fb2e23daf2964189a751fb61b20aa7515a /oop/dp
parent6f68e1818229e0d2dad760062e6b5bb137b88f5b (diff)
更新脚本
Diffstat (limited to 'oop/dp')
-rw-r--r--oop/dp/Decorator.py59
-rw-r--r--oop/dp/__init__.py0
2 files changed, 59 insertions, 0 deletions
diff --git a/oop/dp/Decorator.py b/oop/dp/Decorator.py
new file mode 100644
index 0000000..390935a
--- /dev/null
+++ b/oop/dp/Decorator.py
@@ -0,0 +1,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()
+
+
+
diff --git a/oop/dp/__init__.py b/oop/dp/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/oop/dp/__init__.py