factory.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:python-design-patterns 作者: prateeksan 项目源码 文件源码
def generate_children(count, parent, life_min, life_max):
    """We are using a generator to randomly seed a sequence of specifications for the
    factory. While this is not strictly part of the Factory Pattern, it can be useful
    for testing the factory or for generating objects based on some pre-defined algorithm.
    It is perfectly acceptable to implement factories without generators (depends on your
    specific use-case)."""

    types = parent.__subclasses__()
    for i in range(count):
        yield { 
            "class_name": random.choice(types).__name__, 
            "lifespan": random.randint(life_min, life_max)
        }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号