IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

python设计模式之组合模式

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

  最开始接触组合设计模式是在大话设计模式这本书中的案例,讲的是让你设计一个公司的组织架构,一个总公司下有人力资源部门,IT部门, 财务部门,还有广州分公司、深圳分公司,在分公司下也有IT部门和财务部门,让你用代码构建出来,然后在自学数据结构关于树型结构的时候,让你用python代码实现一个二叉树, 最后第三次是在学习李建忠老师的23种设计模式时,感觉打通了任督二脉,明白组合设计模式本质上是一种树形的数据结构型模式。为什么这么说呢,接下来我们看一下它的官方定义和示例。

组合设计模式GOF官方定义: 将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

类结构图如下:

125_1.png

首先我们用python代码和组合设计模式来实现一个公司的组织架构。

01、设计节点基类

class Department:
    """
    部门基类
    """
    def __init__(self, name):
        self.name = name

    def display(self):
        pass


class Company(Department):
    """
    公司基类
    """

    def __init__(self, name):
        self.department_list = list()
        super().__init__(name)

    def add(self, department: Department):
        self.department_list.append(department)

    def remove(self, department: Department):
        self.department_list.remove(department)

    def display(self, a=None):
        # 这里有点递归的味道
        for department in self.department_list:
            department.display(self.name)

02、设计部门类

class ITDepartment(Department):
    """
    IT部门
    """
    def display(self, company):
        print("这是" + company + self.name)


class FinanceDepartment(Department):
    """
    财务部门
    """

    def display(self, company):
        print("这是" + company + self.name)

03、设计分公司类

class GuangZhouCompany(Company):
    """
    广州分公司
    """



class ShenZhenCompany(Company):
    """
    深圳分公司
    """

04、设计和运行主程序

if __name__ == "__main__":
    root = Company("总公司")
    g_company = GuangZhouCompany("广州分公司")
    s_company = ShenZhenCompany("深圳分公司")
    it_department = ITDepartment("总部IT部门")
    git_department = ITDepartment("广州IT部门")
    sit_department = ITDepartment("深圳IT部门")
    f_department = FinanceDepartment("总部财务部门")
    gf_department = FinanceDepartment("广州财务部门")
    sf_department = FinanceDepartment("深圳财务部门")
    root.add(g_company)
    root.add(s_company)
    root.add(it_department)
    root.add(f_department)
    g_company.add(git_department)
    g_company.add(gf_department)
    s_company.add(sf_department)
    s_company.add(sit_department)
    root.display(None)

运行结果如下:

/usr/local/bin/python3.7 /Users/bytedance/PycharmProjects/untitled3/组合设计模式/组合设计模式.py
这是广州分公司广州IT部门
这是广州分公司广州财务部门
这是深圳分公司深圳财务部门
这是深圳分公司深圳IT部门
这是总公司总部IT部门
这是总公司总部财务部门

Process finished with exit code 0

05、总结

1、 组合模式采用树形结构来实现普遍存在的对象容器, 从而将“一对多”的关系转化成“一对一”的关系, 使得客户代码可以一致地处理对象和对象容器,无需关系处理的是单个对象,还是组合对象。
2、 将“客户代码与复杂的对象容器结构”解耦是组合模式的核心, 解耦之后,客户代码将与纯粹的抽象接口–而非对象容器的内部实现结构发生依赖, 从而更能应对变化。
3、 组合模式在具体实现中,可以让父对象中的子对象反向追溯;如果父对象有频繁的遍历需求可使用缓存技巧来改善效率。

文章永久链接:https://tech.souyunku.com/?p=37852


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(77) 打赏



未经允许不得转载:搜云库技术团队 » python设计模式之组合模式

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367