From 562cdb0187dad918eb063687e663bf3de49b53bc Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Wed, 12 Jun 2024 19:41:11 +0800 Subject: [PATCH 1/2] update factory mode --- .../example.py" | 15 ++++ .../example_1.py" | 35 ++++++++ .../example_2.py" | 49 +++++++++++ .../outline.txt" | 16 ++++ .../example.py" | 41 +++++++++ .../outline.txt" | 9 ++ .../show/__pycache__/a_class.cpython-311.pyc" | Bin 0 -> 3540 bytes .../show/__pycache__/b_class.cpython-311.pyc" | Bin 0 -> 809 bytes .../show/__pycache__/factory.cpython-311.pyc" | Bin 0 -> 4046 bytes .../show/a_class.py" | 53 +++++++++++ .../show/b_class.py" | 6 ++ .../show/client.py" | 30 +++++++ .../show/factory.py" | 63 +++++++++++++ .../study/PersonUtils.py" | 80 +++++++++++++++++ .../study/example_1.py" | 24 +++++ .../study/example_2.py" | 83 ++++++++++++++++++ .../study/ouline.txt" | 14 +++ .../study/show_2.py" | 77 ++++++++++++++++ .../example_1.py" | 70 +++++++++++++++ .../example.py" | 33 +++++++ .../outline.txt" | 11 +++ .../file_type_class/__init__.py" | 0 .../file_type_class/file_type_object.py" | 9 ++ .../file_type_class/test.py" | 15 ++++ ...41\351\200\202\351\205\215\345\231\250.py" | 0 ...73\351\200\202\351\205\215\345\231\250.py" | 0 26 files changed, 733 insertions(+) create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/example.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_1.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_2.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/outline.txt" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/example.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/outline.txt" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/a_class.cpython-311.pyc" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/b_class.cpython-311.pyc" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/factory.cpython-311.pyc" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/a_class.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/b_class.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/client.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/factory.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/PersonUtils.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_1.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_2.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/ouline.txt" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/show_2.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\212\275\350\261\241\345\267\245\345\216\202\346\250\241\345\274\217/example_1.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/example.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/outline.txt" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/__init__.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/file_type_object.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/test.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\345\257\271\350\261\241\351\200\202\351\205\215\345\231\250.py" create mode 100644 "\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\347\261\273\351\200\202\351\205\215\345\231\250.py" diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/example.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/example.py" new file mode 100644 index 0000000..425868d --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/example.py" @@ -0,0 +1,15 @@ +""" +python风格的设计模式 +""" + +""" +首先类可以分为很多情况,有些类的效果能用元类实现。 + +创建不出实例的类:抽象类 +只能创建一个实例的类:单例模式 +不能被继承的类:final类 +必须被继承才能有作用的类:基类、元类 + +类创建出的实例具有自我复制能力:原型模式 + +""" \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_1.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_1.py" new file mode 100644 index 0000000..6dd2186 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_1.py" @@ -0,0 +1,35 @@ +""" +单例模式 +懒汉模式 +""" + + +class Singleton: + __instance = None + + @staticmethod + def getInstance(): + if Singleton.__instance is None: + Singleton() + return Singleton.__instance + + def __init__(self): + if Singleton.__instance is not None: + raise Exception("This class is a singleton!") + else: + Singleton.__instance = self + + def someMethod(self): + pass + + +def main(): + # Usage + s1 = Singleton.getInstance() + s2 = Singleton.getInstance() + + print(s1 == s2) # True + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_2.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_2.py" new file mode 100644 index 0000000..7f0e206 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/example_2.py" @@ -0,0 +1,49 @@ +from abc import ABCMeta, abstractmethod +from typing import Dict + + +class SingletonMeta(type): + _instances: Dict[type, object] = {} + + def __call__(cls, *args, **kwargs): + if cls not in cls._instances: + cls._instances[cls] = super(SingletonMeta, cls).__call__(*args, **kwargs) + return cls._instances[cls] + + +class Singleton(metaclass=SingletonMeta): + pass + + +class MyABC(metaclass=ABCMeta): + @abstractmethod + def f(self): + pass + + +class MyClassA(Singleton, MyABC): + def f(self): + print('A') + + +class MyClassB(Singleton, MyABC): + def f(self): + print('B') + + +def main(): + s1 = Singleton() + s2 = Singleton() + print(id(s1), id(s2)) # 打印两个对象的id,可以看到它们是同一个对象 + a1 = MyClassA() + a2 = MyClassA() + b1 = MyClassB() + b2 = MyClassB() + a1.f() # A + a2.f() # A + b1.f() # B + b2.f() # B + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/outline.txt" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/outline.txt" new file mode 100644 index 0000000..a2809eb --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\215\225\344\276\213\346\250\241\345\274\217/outline.txt" @@ -0,0 +1,16 @@ +用过程性、灵活性和创造性的方式来理解单例模式。《从全局变量到单例模式的自然演进过程》 + +0. globals.py 文件,里面定义了很多的全局变量。 +1. 大字典:发现这些全局变量太多,并且有嵌套结构,可以写一个大字典。 +2. 工具类:发现大字典里写函数很麻烦,并且key没有代码提示,于是把大字典改成了一个类。键值对改成了类属性 +3. 单例类:发现函数需要用到一些属性。于是把类属性变成了实例属性。这就导致了需要创建实例才能用,但只用一个实例就可以了。 +4. 更安全的单例类(防止外部调用产生新实例): + 可以把类名前面加下划线,君子协议 + 可以del删掉这个类名称本身,但类还是在内存中不会被垃圾回收机制清理掉。 + 更安全的方法:创建单例类元类,这样创建出来的类都是一个实例了 +5. 懒汉式单例模式: + 这个单例的创建过程非常消耗时间,没必要一开始就创建。可以后面真正需要的时候才创建 + 但可能有线程安全问题,因为创建单例的过程不是原子操作。threading.Lock() + +以上顺序不一定就是后面一种比前面一种好,只是一种扩展性增强和不断优化的思路。 +可以说越是靠前,可能就越使用小轻量级的场景和项目,越是靠后,可能就越适合大型项目和复杂场景。 \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/example.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/example.py" new file mode 100644 index 0000000..37ae489 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/example.py" @@ -0,0 +1,41 @@ +class A: + def __init__(self, x): + # === + # 这里中途可能花费了很大的时间复杂度用来计算出一个数值 + # === + self.x = x + + def build1(self): + for i in range(10000000): + self.x += 15 + + def build2(self): + for i in range(10000000): + self.x += 20 + + def clone(self): + return A(self.x) + + +class B: + + def __init__(self, a): + self.a = a + + def func(self): + print(self.a) + + +""" +原型模式可能应用到的地方: +1. 性能优化:由于创建复杂对象时,原型模式可以减少创建对象的过程,从而提高性能。 +地形生成、 + +2. 资源优化:由于创建复杂对象时,原型模式可以减少内存占用,从而节省系统资源。 +和第一个差不多 + +3. 代码复用:由于创建复杂对象时,原型模式可以减少代码量,从而提高代码复用率。 +子弹克隆 + + +""" diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/outline.txt" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/outline.txt" new file mode 100644 index 0000000..d501192 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\216\237\345\236\213\346\250\241\345\274\217/outline.txt" @@ -0,0 +1,9 @@ + + +1. 可能消耗很大计算量才能创建出一个实例,并且写在了工厂函数里。再次创建相同类型的实例可以直接克隆节省时间 +2. 使用简单化。比如在游戏中,让某个枪类给一个子弹属性, + 每次发射开火的时候都不停的复制这个子弹,所以直接给子弹写一个克隆方法就可以,不需要再写子弹的颜色之类的东西了。 + 这样给枪切换不同的子弹的时候,只需要更改蓝图子弹就可以了。 + + 还可能是机器学习模型训练的时候,每次训练都需要重新生成一个模型,这时候直接用原型模式就很方便。 + UI项目中,原型模式还可以直接生成好一个组件,然后每次用户拖拽到舞台上就是不停的复制一份这个组建,吸附在鼠标上然后放在舞台上 \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/a_class.cpython-311.pyc" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/a_class.cpython-311.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..f039b03ff1419c6064d30e3c06bef6904b3321d1 GIT binary patch literal 3540 zcmbtWT}%{L6uz@NvkZSOupn9`tgSU}Ls6^YLB*Dc{YebCENF|E?Xok#;{L$Qpn#i{ zG}K@<);2VdN*gh#5YaS^Nlhy)8WLX@vJYeulP2w2eA|Zh#iyQgXZG?hjlC@2eCM1y z_ue_@yLaYRc6J7V)_u99sZb!~cQjIsH_7~NAOl1q5*H*PuAU27>Mb0xkoSmW=_Qi& zriGB(@NcwwtHgVVOL&H+%cAp@0l%j6M+2(nH@nbFmTAm?4rGAHq@I&Vy+z_+0E^5^ zR#}jE*(M2cn#^xzcS<%{lx;HKWLfYvWk_kge0{nk_LBMxIio&PN(YuDWdLSNnSdLl zEI_-I4VWWs0L+!_fO%35phL<9%$M?D;SQav2qe-7(K(;aHR_yS=Omqz{Wi7+Lae~? ze_jI^AW<$#yeu5GfW@WEs1+=hohT0$-bq_j04tSeqGhK1ghWXzo9`x!b(@XHK_xt7 zjH_~`DX>bShzRKjSW5*5(S?(Q$(cse?qc<@DH+1$J} zFHgkmJ}JJTcv^JEMQ1Gan}-pIpCuR!%pooBhd@C3P~KKxM!O2K2b=W=8%o^DN(?p> zxs??eY$$UpD>K-(Z!s2)ca+HoaqO<_JlmqmTS$p*rslO>^uZG{X?ISx3HY$yqzWduBn7&DA%Jg9$ zv08D!2um^qhlP=$M?hc$EXogXmYZ&fYS`Coi}SKn*0d+8c%dh|Z{413t6W1#tY|mE zbDMcrj^-gsHiTW`Lb7#hP!7{wrCZgG(4wtnJL3y>$f{BZcPvT1KN4!|&}2-$NngP( z6#$rg$E7oa{E+R!CpR3|YDbO_ZyDJ#Z7;c_e!uTt+1R=9bJO=+}dm?WyE=rVJD3BT71#vby#2Gi^ z#fu9EaVBmvZihG%IK&w@;%0)Cjj+}+3)QVIM6?!JQE?`xsb8DdE4PDuMU*eHA_%H( zfK&nth6-ow&X~OuQ#jf&es<>dGO+INdr&r6H+5p>z)`Sb?z*`9MC|0p@so`+?nd-9 zpN=<&V(n+*?VU5toiVXu3wgi)1VWG&!_V#cZ<$G%U)Wng=?Nd7*mpm$YSUOcYeb600=#J&rOxrziyC)`kUZCJtX~H3~a5Wkz zC`HL>%FM@uLyx}miov;DIzfCpQ0X9h4SLMushEXepu!r01c z>u6@{K(+(By@5c_P`0tagPbaEodi(rtvhW{W{(fd1!FIHd~@fB&X->YJ&)I2M>AUo zvJ!mqT?jAz`1GtnQ9wn&h*DZL|4>Nh@4`%io+7;F`3Nf=$UZWFjfp zJm~2EAeoL<2DJwLgPZ!Rl9}isOJOS~9OIH?5!y&jVbA+Xo)cScRbx+_k>_absGTg^-ViYuIKtz54=m{F1Cb-(1+?0#?a$Mfy}r_SMnCIyO`^vMhY3OF(M;Nqi& z)-76}<8zX3MkW;_<+Xg(;XFuiUUkK4h4?YfZ>;7klZ;-j`EnWy?x?YskPLLj T*g!KGcKjE9Qta1%5~}eZuBysF literal 0 HcmV?d00001 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/factory.cpython-311.pyc" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/__pycache__/factory.cpython-311.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..42cab2c43bb54a26605569a7b148f95d4a53b275 GIT binary patch literal 4046 zcmcgvTTB~A6rJ_j>jx0BHqf+rf%>$Xkbu)fN(m{?<{=y!+NyCKS&Mf8M}Cl5r!5wd zP(>r8L@h~+K7hqki zT4(+GfWc)5a4s%jbQu|8t43qMcY`R+>U z=ER>j&n;ZLk{Y{o_wuNF_g+Ws!nL13a3^{tb>pMd*H=>?MBS;ePg6hskow}hJN5lH zsSn@Rlx}$-^sc*4GrfLrh?>D36*#EKl>j68Us7)xX{R`Q^$l1-Gc1$f^$W6Guu?ff zGVj2lP14he)%uei8dawf5oQJZwsu(siQvA3ouE-IliGKGNMjo0rmaBx2P8l zpXp}6z2|Z+PRDQvnbm#TD`MTJSCb;7K zfdqeGj7zEp&cAu%EwCzX>rB`>$G8ioWM%bOg=*d-x(T=kxCx-hm2eYq<41B6laoFL z^GH|Ue%q*UgK7duqu-@5M1eX+QkcGVbQHP``lYo1E8T%U4T4BR!J1>L4E9TFI>ILG z>=^_#aXgE)=K)sITZQx%cNW>Y=2+F($4Y{I)0?6#(bLhr^ZYG7=4gW}&bKG{_WzGy zPBfMv7&6i$5p327rZ3{sU})2w2=xHz%Mg%s7t|gTzq<<7d_Wi+99olI)m8q5T`RI% z*i~fn8slt+={}HITg=e`SDf!m@DQc{F?5Gvh~f+%#tf7#qHYGV&s;wFLhjE6^;(=` z_EbxW+@IH}xj)Of!uo=JMV!-@po_=Jd0|ZV`uh7NIlLz8D&cw7?W~r(4SoKQ5T?(e z`6VN~xa*<_FILwFr7YWQKiqRvv1A)<-ES!6u;GP$UhQy5?fAU| zVIM*h0`9b0ro$ab-EmdG!-f1Yz)1d=d~$0{Uz4obHqw=}ZXY?CM)wUxMLS=bUBaB90ASxoZ>_yoO?`)sWp2tqj;R5RS1~R zX$?Su4SEn@TxRwItcuv2g`hi?uj8?&FnaNP?o1;evn<6PX)6LUU(6R$8rhOFujF9| z-jH!q1Fx*yIBuO7NYpgM`G$C9W1_M#W@=o0VpzXP`0m1DP>hLU!_oO7TMG;?j;F># z%WV_yC7x)E^NsOJN21aZGdWbtopwv95TOTf&}IbObm=PyY5(utKkwf`br> zhbL-8z|hnJRW-XXg72A{v_Ejo6vF^_1UUw<$TAF*B-OFPtCVC%j6CpKbb65pa mMpe3ms^$B7ZVLl3yBd~IwtQdFT+YDytcE3&EtgQJOZX3pbB7xM literal 0 HcmV?d00001 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/a_class.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/a_class.py" new file mode 100644 index 0000000..0afa988 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/a_class.py" @@ -0,0 +1,53 @@ +from typing import Dict, List + + +class A: + CURRENT_YEAR = 2021 + + def __init__(self, + a: int, + b: float, + c: str, + d: List[int], + e: Dict[str, int] + ): + self.a = a + self.b = b + self.c = c + self.d = d + self.e = e + print(self.CURRENT_YEAR) + + def __str__(self): + return f"A(a={self.a}, b={self.b}, c={self.c}, d={self.d}, e={self.e})" + + __repr__ = __str__ + + def compute(self): + return self.a + self.b + len(self.c) + sum(self.d) + sum(self.e.values()) + + def __eq__(self, other): + return self.a == other.a and self.b == other.b and self.c == other.c and self.d == other.d and self.e == other.e + + def __hash__(self): + return hash((self.a, self.b, self.c, tuple(self.d), tuple(self.e.items()))) + + def __lt__(self, other): + return self.a < other.a + + def __gt__(self, other): + return self.a > other.a + + def __le__(self, other): + return self.a <= other.a + + def __ge__(self, other): + return self.a >= other.a + + def __ne__(self, other): + return self.a != other.a + + def __bool__(self): + return bool(self.a) + + pass diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/b_class.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/b_class.py" new file mode 100644 index 0000000..f7bef54 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/b_class.py" @@ -0,0 +1,6 @@ +class B: + def __init__(self, a: int): + self.a = a + + def __str__(self): + return f"<{self.a}>" diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/client.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/client.py" new file mode 100644 index 0000000..5f12d02 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/client.py" @@ -0,0 +1,30 @@ +from a_class import A +from factory import AFactory + + +def main(): + + a_normal_1 = AFactory.get_normal_instance() + # ==== + a2 = A(1, 1.3, 'world', [3, 4], {'c': 3, 'd': 4}) + # ==== + + a3 = A(2, 2.3, 'python', [5, 6], {'e': 5, 'f': 6}) + + AFactory.SYSTEM_PERFORMANCE = 0.5 # 模拟系统性能下降,降低创建对象的速度 + + # ==== + a_normal_2 = AFactory.get_normal_instance() + a_normal_3 = AFactory.get_normal_instance() + + AFactory.SYSTEM_PERFORMANCE = 1 # 模拟系统性能恢复,恢复创建对象的速度 + + a_strange_1 = AFactory.get_strange_instance() + + # ==== + a_strange_2 = AFactory.get_strange_instance() + print(a_normal_1, a_normal_2, a_normal_3, a_strange_1, a_strange_2) + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/factory.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/factory.py" new file mode 100644 index 0000000..a7a65bf --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/show/factory.py" @@ -0,0 +1,63 @@ +from a_class import A +from b_class import B +from abc import ABC, abstractmethod + + +class Factory(ABC): + + @staticmethod + @abstractmethod + def get_system_performance() -> int: + pass + + # SYSTEM_PERFORMANCE = 1000 + # SYSTEM_MEMORY = 1000000000 + # SYSTEM_DISK = 1000000000000 + # SYSTEM_CPU = 10 + + +class AFactory(Factory): + @staticmethod + def get_system_performance() -> int: + return 10010 + + SYSTEM_PERFORMANCE = 1001 + + @staticmethod + def get_normal_instance(): + result = A(1, 1.3, 'hello', [1, 2], {'a': 1, 'b': 2}) + result.d = [i * AFactory.get_system_performance() for i in result.d] + return result + + @staticmethod + def get_strange_instance(): + result = A(2, 123456789, 'python', [5, 6], {'e': 5, 'f': 6}) + result.d = [i * AFactory.SYSTEM_PERFORMANCE for i in result.d] + return result + + @staticmethod + def get_happy_instance(): + result = A(2, 123456789, 'python', [5, 6], {'e': 5, 'f': 666}) + result.d = [i * AFactory.SYSTEM_PERFORMANCE for i in result.d] + return result + + @staticmethod + def get_biggest_instance(): + result = A(2, float('inf'), 'python', [5, 6], {'e': 5, 'f': 666}) + result.d = [i * AFactory.SYSTEM_PERFORMANCE for i in result.d] + return result + + +class BFactory(Factory): + + @staticmethod + def get_system_performance() -> int: + return 15 + + @classmethod + def get_normal_instance(cls): + return B(15 * BFactory.get_system_performance()) + + @staticmethod + def get_biggest_instance(): + return B(999 * BFactory.get_system_performance()) diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/PersonUtils.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/PersonUtils.py" new file mode 100644 index 0000000..b70533a --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/PersonUtils.py" @@ -0,0 +1,80 @@ +from .show import MyClass +import platform + + +class PersonFactory1: + SYSTEM_PERFORMANCE = 100 + + @staticmethod + def _get_gender_by_name(name: str) -> bool: + """如果长度大于5,则判断为男性,否则为女性""" + return len(name) > 5 + + @staticmethod + def get_Bob(): + result = MyClass( + name='Tom', + age=20, + score_list=[90, 80, 70], + address='Beijing', + phone='13800138000', + email='tom@example.com', + hobby='reading' + ) + result.score_list = [int(i * PersonFactory1.SYSTEM_PERFORMANCE / 100) for i in result.score_list] + result.gender = PersonFactory1._get_gender_by_name(result.name) + return result + + @classmethod + def get_Alice(cls): + result = MyClass( + name='Jane', + age=25, + score_list=[85, 95, 90], + address='Shanghai', + phone='13900139000', + email='jane@example.com', + hobby='swimming' + ) + result.score_list = [int(i * cls.SYSTEM_PERFORMANCE / 100) for i in result.score_list] + result.gender = cls._get_gender_by_name(result.name) + return result + + +if platform.system() == 'Windows': + PersonFactory1.SYSTEM_PERFORMANCE = 150 +else: + PersonFactory1.SYSTEM_PERFORMANCE = 100 + + +class PersonFactory2(PersonFactory1): + SYSTEM_PERFORMANCE = 120 + + @staticmethod + def get_Tom(): + result = MyClass( + name='Bob', + age=20, + score_list=[90, 80, 70], + address='Beijing', + phone='13800138000', + email='bob@example.com', + hobby='reading' + ) + result.score_list = [int(i * PersonFactory2.SYSTEM_PERFORMANCE / 100) for i in result.score_list] + result.gender = PersonFactory2._get_gender_by_name(result.name) + return result + + @classmethod + def get_Jane(cls): + result = MyClass( + name='Alice', + age=25, + score_list=[85, 95, 90], + address='Shanghai', + phone='13900139000', + email='alice@example.com', + hobby='swimming' + ) + result.score_list = [int(i * cls.SYSTEM_PERFORMANCE / 100) for i in result.score_list] + result.gender = cls._get_gender_by_name(result.name) diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_1.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_1.py" new file mode 100644 index 0000000..3e1c3e4 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_1.py" @@ -0,0 +1,24 @@ +""" +工厂模式举例 + +1. 直接写在类的静态方法中 +""" + + +class Vector: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + @staticmethod + def get_zero() -> 'Vector': + # 目的:简化参数,可以直接通过类名调用静态方法 + return Vector(0, 0) + + @staticmethod + def get_random() -> 'Vector': + # 目的:简化参数,可以直接通过类名调用静态方法 + import random + return Vector(random.randint(0, 100), random.randint(0, 100)) + + pass diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_2.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_2.py" new file mode 100644 index 0000000..2421090 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/example_2.py" @@ -0,0 +1,83 @@ +""" +工厂模式 - 单独写一个工厂类的情况举例 +""" +from abc import ABCMeta, abstractmethod + + +class ResultType(metaclass=ABCMeta): + VALUE = 0 + + @abstractmethod + def f(self): + pass + + pass + + +class A(ResultType): + VALUE = 1 + + def f(self): + pass + + +class B(ResultType): + VALUE = 2 + + def f(self): + pass + + +class C(ResultType): + VALUE = 3 + + def f(self): + pass + + +class Factory: + + @staticmethod + def create_object(type_name): + """ + 但是这种情况可以直接被一个全局函数代替,不需要单独写一个工厂类 + """ + if type_name == 'A': + return A() + elif type_name == 'B': + return B() + elif type_name == 'C': + return C() + else: + raise ValueError('Invalid type name') + + COUNT = 0 + + @classmethod + def create_object_by_score(cls, score: int) -> ResultType: + """ + 但是如果需要记录创建对象的次数,可以用类方法来实现 + 这种情况的使用场景还有可能有: + 1. 记录创建对象的次数 + 2. 在创建对象时,会对其他环境产生依赖或者修改全局状态,可以用工厂模式来封装这些操作 + """ + cls.COUNT += 1 + if score >= 80: + return A() + elif score >= 60: + return B() + elif score >= 40: + return C() + else: + raise ValueError('Invalid type name') + + +def main(): + obj1 = Factory.create_object('A') + obj2 = Factory.create_object('B') + obj3 = Factory.create_object('C') + print(obj1, obj2, obj3) + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/ouline.txt" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/ouline.txt" new file mode 100644 index 0000000..23b542a --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/ouline.txt" @@ -0,0 +1,14 @@ +工厂模式: + +0. 正常写类:直接写一个类 +1. 出现工厂方法:但随着构造函数的参数越来越多,开始麻烦起来了。直接写在类里的一个静态方法(类方法也行) +2. 出现工厂方法的Utils文件:为了避免类中出现太多静态方法,可以把静态方法都放在一个Utils文件中。 + (单独写在一个文件里还可以更清晰,因为多个工厂函数内部可能调用到同一个其他的函数,那么这个被调用的函数就可以放在utils里,函数名前面加下划线表示私有) +3. 出现工厂类:发现可能集装在一个类中可能更好一些,可以写类属性,统一控制这些工厂方法。 + (java里必须写成类,因为函数没法单独存在) + (工厂类还可以不停的继承,实现,扩展,父工厂类可以有2个工厂函数,子工厂类可以有2+1个工厂函数) +4. 出现工厂类的继承或实现(抽象工厂):发现工厂类的某一个属性需要在不同的场景中频繁修改,于是开始了工厂类的继承或者实现。 + (这个属性可能是代表xx性能属性) + (如果直接从utils方法转成抽象工厂,那还需要再写一个工厂类) +5. 抽象工厂的抽象工厂:为了让A工厂类和B工厂类统一接口,所以出“AB统一工厂”这个抽象工厂,为了让C工厂类和D工厂类统一接口,所以出“CD统一工厂”。 + 然后可能会发现 CD统一工厂 和 AB统一工厂 有些重复,于是又出现了“统一工厂”这个更抽象的抽象工厂。 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/show_2.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/show_2.py" new file mode 100644 index 0000000..24e55fc --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\345\267\245\345\216\202\346\250\241\345\274\217/study/show_2.py" @@ -0,0 +1,77 @@ +from typing import List + + +class Animal: + def __init__(self, name): + self.name = name + + +class Basketball: + def __init__(self, name): + self.name = name + + +class Car: + def __init__(self, name): + self.name = name + + +class Desk: + def __init__(self, name): + self.name = name + + +from abc import ABC, ABCMeta, abstractmethod + + +class Factory(ABC): + FactoryLocation: List[int] = [100, 100] + FactoryName: str = "Factory" + + @abstractmethod + def test(self): + pass + + FactoryCommentList: List[str] = [] + + +class FactoryChina(Factory): + FactoryLocation: List[int] = [100, 100] + FactoryName: str = "China" + FactoryCommentList: List[str] = [] + + @staticmethod + def get_animal(name): + return Animal(name) + + @staticmethod + def get_ball(name): + return Basketball(name) + + def test(self): + print(self) + + +class FactoryUSA(Factory): + FactoryLocation: List[int, int] = [500, 200] + FactoryName: str = "USA" + FactoryCommentList: List[str] = [] + + @staticmethod + def get_car(name): + return Car(name) + + @staticmethod + def get_desk(name): + return Desk(name) + + def test(self): + print(self) + + +def main(): + a1 = FactoryChina.get_animal("dog") + b1 = FactoryChina.get_ball("basketball") + c1 = FactoryUSA.get_car("BMW") + d1 = FactoryUSA.get_desk("desk") + print(a1.name, b1.name, c1.name, d1.name) diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\212\275\350\261\241\345\267\245\345\216\202\346\250\241\345\274\217/example_1.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\212\275\350\261\241\345\267\245\345\216\202\346\250\241\345\274\217/example_1.py" new file mode 100644 index 0000000..7b56f89 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\212\275\350\261\241\345\267\245\345\216\202\346\250\241\345\274\217/example_1.py" @@ -0,0 +1,70 @@ +""" +传统的抽象工厂模式 + +抽象工厂模式是一种创建型设计模式, +它提供一个创建一系列相关或相互依赖对象的接口, +而无需指定它们具体的类。 + +在抽象工厂模式中, +一个工厂接口负责创建相关对象, +而子类则负责实现这些对象的创建。 + + +北京工厂、上海工厂、广州工厂等, +这些所有的工厂都统一写一个接口,这个工厂的接口就是抽象工厂。 +抽象工厂不能说是生产工厂的工厂,而是工厂的统一规定,工厂的接口。 + +抽象工厂模式的选择是基于工厂模式的类写法来的。 + +1 工厂方法写在类本身中 +2 工厂方法单独写在一个类中(工厂类) +3 工厂类被统一成一个接口(抽象工厂类) +""" + + +class AbstractFactory(object): + def create_product_a(self): + pass + + def create_product_b(self): + pass + + +class ConcreteFactory1(AbstractFactory): + def create_product_a(self): + return ConcreteProductA1() + + def create_product_b(self): + return ConcreteProductB1() + + +class ConcreteFactory2(AbstractFactory): + def create_product_a(self): + return ConcreteProductA2() + + def create_product_b(self): + return ConcreteProductB2() + + +class AbstractProductA(object): + pass + + +class ConcreteProductA1(AbstractProductA): + pass + + +class ConcreteProductA2(AbstractProductA): + pass + + +class AbstractProductB(object): + pass + + +class ConcreteProductB1(AbstractProductB): + pass + + +class ConcreteProductB2(AbstractProductB): + pass diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/example.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/example.py" new file mode 100644 index 0000000..a11e240 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/example.py" @@ -0,0 +1,33 @@ +class Person: + def __init__(self, name, age, hair_color=None, height=None, weight=None, address=None): + self.name = name + self.age = age + self.hair_color = hair_color + self.height = height + self.weight = weight + self.address = address + + +class PersonBuilder: + def __init__(self): + self.person = Person(None, None) + + def with_name(self, name): + self.person.name = name + return self + + def with_age(self, age): + self.person.age = age + return self + + def build(self): + return self.person + + +""" +好处: +参数太多的时候可以分步骤构建,顺序可能可以换,(但实际上py有默认参数,可以不用这么麻烦) +参数太多可以把几个捏在一起放在一个构造过程函数里 +""" +builder = PersonBuilder() +person = builder.with_name("John").with_age(30).build() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/outline.txt" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/outline.txt" new file mode 100644 index 0000000..c6d0dbb --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\210\233\345\273\272\345\236\213/\346\236\204\351\200\240\345\231\250\346\250\241\345\274\217/outline.txt" @@ -0,0 +1,11 @@ +一般来说python不需要构造器模式,因为: + +动态追加属性(虽然可能有编辑器警告,不太推荐) +可变默认参数,可以让参数灵活决定位置 +可以让构造函数没有参数,让属性全都是先给定一个默认值,后面只对需要的进行更改,这样可以减少代码量 + +如果遇到这种情况可以考虑构造器模式: + +1. 将构造函数的职责分解为多个小函数,每个小函数负责一个构造步骤 + 可选参数太多,统一写起来太麻烦,可以把三四个包起来形成一个步骤 +2. 创建对象的逻辑和对象本身分离。可以单独写一个 XxxGenerator 类 \ No newline at end of file diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/__init__.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/__init__.py" new file mode 100644 index 0000000..e69de29 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/file_type_object.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/file_type_object.py" new file mode 100644 index 0000000..d5152b8 --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/file_type_object.py" @@ -0,0 +1,9 @@ +""" +将当前这个代码文件直接视为一个对象 +""" + +value = 100 +value2 = 200 + +_value3 = 300 +__value4 = 400 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/test.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/test.py" new file mode 100644 index 0000000..f0f442a --- /dev/null +++ "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\345\245\207\346\200\252\347\232\204\346\250\241\345\274\217/file_type_class/test.py" @@ -0,0 +1,15 @@ +import file_type_object + + +def main(): + print(file_type_object) + print(file_type_object.value) + + print(file_type_object._value3) # 警告但可以访问 + print(file_type_object.__value4) # 反而又没有警告了 + + pass + + +if __name__ == '__main__': + main() diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\345\257\271\350\261\241\351\200\202\351\205\215\345\231\250.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\345\257\271\350\261\241\351\200\202\351\205\215\345\231\250.py" new file mode 100644 index 0000000..e69de29 diff --git "a/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\347\261\273\351\200\202\351\205\215\345\231\250.py" "b/\350\247\206\351\242\221\350\277\230\346\262\241\345\275\225\344\275\206\350\256\241\345\210\222\347\232\204/063 \350\256\276\350\256\241\346\250\241\345\274\217\345\244\247\345\205\250/\347\273\223\346\236\204\345\236\213/\351\200\202\351\205\215\345\231\250\346\250\241\345\274\217/\347\261\273\351\200\202\351\205\215\345\231\250.py" new file mode 100644 index 0000000..e69de29 From f9921e9d884aa760b0bffd1a21294be30944fa4f Mon Sep 17 00:00:00 2001 From: littlefean <2028140990@qq.com> Date: Tue, 18 Jun 2024 06:58:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=BF=B7?= =?UTF-8?q?=E6=83=91=E8=A1=8C=E4=B8=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\346\211\277\346\265\213\350\257\2251.py" | 53 ++++++++++++++ ...7\346\211\277\346\265\213\350\257\2253.py" | 68 ++++++++++++++++++ ...7\346\211\277\346\265\213\350\257\2254.py" | 51 +++++++++++++ ...7\346\211\277\346\265\213\350\257\2255.py" | 42 +++++++++++ .../__pycache__/a.cpython-311.pyc" | Bin 0 -> 1020 bytes .../__pycache__/d.cpython-311.pyc" | Bin 0 -> 632 bytes .../a.py" | 2 + 7 files changed, 216 insertions(+) create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\345\244\232\347\273\247\346\211\277\346\265\213\350\257\2251.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2253.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2254.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2255.py" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/a.cpython-311.pyc" create mode 100644 "python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/d.cpython-311.pyc" diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\345\244\232\347\273\247\346\211\277\346\265\213\350\257\2251.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\345\244\232\347\273\247\346\211\277\346\265\213\350\257\2251.py" new file mode 100644 index 0000000..1d49b1c --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\345\244\232\347\273\247\346\211\277\346\265\213\350\257\2251.py" @@ -0,0 +1,53 @@ +def extend(*extend_cls_list): + def decorator(cls_function): + def new_function(*args, **kwargs): + if len(extend_cls_list) == 0: + return cls_function(lambda: ..., *args, **kwargs) + else: + super_obj = lambda: ... + for extend_cls in extend_cls_list: + current_super_obj = extend_cls(*args, **kwargs) + for key, value in current_super_obj.__dict__.items(): + if not key.startswith('__'): + setattr(super_obj, key, value) + return cls_function(super_obj, *args, **kwargs) + + return new_function + + return decorator + + +@extend() +def Animal(self, *args, **kwargs): + self.name = kwargs.get('name', 'Unknown') + + def to_string(): + return f"I am {self.name}" + + self.to_string = to_string + return self + + +@extend() +def Runner(self, *args, **kwargs): + self.speed = kwargs.get('speed', 0) + return self + + +@extend(Animal, Runner) +def Dog(self, *args, **kwargs): + self.age = kwargs.get('age', 0) + + self.super_to_string = self.to_string # 保存父类的to_string方法 + + def to_string(): + print(self.super_to_string()) + return f"I am {self.name}, I am {self.age} years old, and I run at {self.speed} km/h." + + self.to_string = to_string + + return self + + +dog = Dog(name='Rufus', age=3, speed=10) +print(dog.to_string()) diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2253.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2253.py" new file mode 100644 index 0000000..6d3f512 --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2253.py" @@ -0,0 +1,68 @@ +from copy import deepcopy + + +def Person(name: str, age: int): + # 定义this对象,不能用object(), 否则会报错 + this = lambda: ... + + def toString(): + return f"Person(name={this.name}, age={this.age}, defaultValue={this.defaultValue})" + + def sayHello(): + print(f"Hello, my name is {this.name} and I am {this.age} years old., my default value is {this.defaultValue}") + + # 给this对象添加属性 + this.name = name + this.age = age + this.defaultValue = 100 + # 给this对象添加方法 + this.toString = toString + this.sayHello = sayHello + # 返回this对象 + return this + + +p1 = Person("Alice", 25) +print(p1) +print(p1.toString()) +print(p1.age, p1.name) +print(p1.toString.__closure__) + + +# 单继承 +def Worker(name: str, age: int, salary: int): + this = Person(name, age) + + def work(): + print( + f"I am a worker, my name is {this.name}, I am {this.age} years old, and my salary is {this.salary}, my default value is {this.defaultValue}") + + this.salary = salary + this.defaultValue = 200 + this.work = work + return this + + +w1 = Worker("Bob", 30, 5000) +w1.sayHello() +print(w1.toString()) + + +def SuperWorker(name: str, age: int, salary: int, bonus: int): + super = Worker(name, age, salary) + this = deepcopy(super) + print(id(super), id(this)) + + def work(): + this.super.work() + print( + f"I am a super worker, my name is {this.name}, I am {this.age} years old, and my salary is {this.salary}, my bonus is {this.bonus}, my default value is {this.defaultValue}") + + this.super = super + this.bonus = bonus + this.work = work + return this + + +sw1 = SuperWorker("Charlie", 35, 7000, 1000) + diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2254.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2254.py" new file mode 100644 index 0000000..16b9b05 --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2254.py" @@ -0,0 +1,51 @@ +import inspect + + +def extends(super_class): + def decorator(child_class): + + def new_class(*args, **kwargs): + + if super_class is not None: + print(args) + super_object = super_class(*args, **kwargs) + return child_class(super_object, *args, **kwargs) + else: + # bug + return child_class(lambda: ..., *args, **kwargs) + + new_class.__name__ = child_class.__name__ + new_class.__doc__ = child_class.__doc__ + + return new_class + + return decorator + + +@extends(None) +def Animal(self, name: str): + self.name = name + + def to_string(): + return f"I am {self.name}" + + self.to_string = to_string + return self + + +print(Animal("dog").to_string()) + + +@extends(Animal) # 太麻烦了 +def Fish(self, name: str, color: str): + self.name = name + self.color = color + + def to_string(): + return f"I am {self.name}, and my color is {self.color}" + + self.to_string = to_string + return self + + +print(Fish("goldfish", "gold").to_string()) diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2255.py" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2255.py" new file mode 100644 index 0000000..9e8ee75 --- /dev/null +++ "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\345\207\275\346\225\260\345\274\217\347\274\226\347\250\213/\347\224\250\351\227\255\345\214\205\347\216\251\351\235\242\345\220\221\345\257\271\350\261\241/\347\273\247\346\211\277\346\265\213\350\257\2255.py" @@ -0,0 +1,42 @@ +def extend(extend_cls): + def decorator(cls_function): + def new_function(*args, **kwargs): + if extend_cls is not None: + super_obj = extend_cls(*args, **kwargs) + return cls_function(super_obj, *args, **kwargs) + else: + return cls_function(lambda: ..., *args, **kwargs) + + return new_function + + return decorator + + +@extend(None) +def Animal(self, *args, **kwargs): + self.name = kwargs.get('name', 'Unknown') + + def to_string(): + return f"I am {self.name}" + + self.to_string = to_string + return self + + +@extend(Animal) +def Dog(self, *args, **kwargs): + self.age = kwargs.get('age', 0) + + self.super_to_string = self.to_string # 保存父类的to_string方法 + + def to_string(): + print(self.super_to_string()) + return f"I am {self.name}, I am {self.age} years old" + + self.to_string = to_string + + return self + + +dog = Dog(name='Rufus', age=3) +print(dog.to_string()) diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/a.cpython-311.pyc" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/a.cpython-311.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..9619eade2e7b53ec5783876a2c32cfb9043685bd GIT binary patch literal 1020 zcmcIi&1(}u6o0cj8`rI|HegLWlw3UI&|dT;;-wWUVl3nqBFmCpHMQA<$$F?*SaFM6 z$e{tH*Lbi}d-7sMME?N~X$&+h6oi;Iy#=ZuJ^5zME)94S-|YMCoA){I&Ag6A6+rs& zt917({yTgVkT?(yzo4)NI_P8pvLJv#bYYzUY$E?50=noxR?sP;Xi!~xAaDztHDG?= z1OHK~2WJgyabX>KGt}T2Nyg_?u)5N^Ps{uD=ldJ)I(99$ z=hk}9-gLL#c0a!8F24%5Ic=)E=uyk4Sd2e_2y$f#!G_&vZ1v(wwwmn&k;Z&*$*vrA zwH3K4ySVydU^v7G7#bnJb$^5w3OGXUFra^V2lZoD5#;;J{qk{>G)|gmep?&BM$ny| zMDS=)E0}B)MXsw{K)`OP@=Xiiw7pXb&IXx!s~?Tl8FuCU)pVlTK{YWQ!&n O!Pggop2sl%c)tN)V$T5p literal 0 HcmV?d00001 diff --git "a/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/d.cpython-311.pyc" "b/python\350\277\267\346\203\221\350\241\214\344\270\272/\343\200\220\345\276\205\347\240\224\347\251\266\343\200\221/\346\210\221\350\207\252\345\267\261\345\274\225\345\205\245\346\210\221\350\207\252\345\267\261/__pycache__/d.cpython-311.pyc" new file mode 100644 index 0000000000000000000000000000000000000000..0ac6aeace719c9f094d39695deade6ea469f2089 GIT binary patch literal 632 zcmY*V&o2W(6n?WmXe5fnA0RIDK+>B?BN5I*8kc4-be1Z1H`8?>4%1k%2ZvS&O-Sm1 z;N*gc;7@RD_C%_*w>zs%>RBNum%^E5ddv-tE25yP&(o{u=L&PIV0Y*yDzHf#&IjT)F zGpWbzRHGP*uFf%g!lZiCSh6e~JN}pHbvt1in&n^R=ey6XZ25HeWSgVOd{lWKI>T4&PO&E=