GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — Python 组合模式 | 菜鸟教程
Python 设计模式
Python 组合模式组合模式是一种结构型设计模式,它允许你将对象组合成树形结构来表示"部分-整体"的层次结构。组合模式让客户端可以统一地处理单个对象和对象组合,无需关心处理的是单个对象还是整个组合结构。 生活中的类比想象一下文件系统的组织结构:
这种"部分-整体"的层次关系就是组合模式的典型应用场景。 组合模式的核心组件1. 组件接口(Component)这是组合模式的核心,定义了所有对象(包括叶子节点和组合节点)的通用接口。 实例
from abc import ABC, abstractmethod
from typing import List class FileSystemComponent(ABC): """文件系统组件抽象基类""" def __init__(self, name: str): self.name = name self.parent = None @abstractmethod def display(self, indent: int = 0) -> None: """显示组件信息""" pass @abstractmethod def get_size(self) -> int: """获取组件大小""" pass def get_path(self) -> str: """获取完整路径""" if self.parent: return f"{self.parent.get_path()}/{self.name}" return self.name 2. 叶子节点(Leaf)表示组合中的叶子对象,叶子节点没有子节点。 实例
class File(FileSystemComponent):
"""文件类 - 叶子节点""" def __init__(self, name: str, size: int): super().__init__(name) self._size = size def display(self, indent: int = 0) -> None: """显示文件信息""" spaces = " " * indent print(f"{spaces}📄 {self.name} ({self._size} bytes)") def get_size(self) -> int: """返回文件大小""" return self._size 3. 组合节点(Composite)表示可以包含子组件的组合对象,定义了存储子组件的方法。 实例
class Directory(FileSystemComponent):
"""目录类 - 组合节点""" def __init__(self, name: str): super().__init__(name) self._children: List[FileSystemComponent] = [] def add(self, component: FileSystemComponent) -> None: """添加子组件""" component.parent = self self._children.append(component) def remove(self, component: FileSystemComponent) -> None: """移除子组件""" self._children.remove(component) component.parent = None def display(self, indent: int = 0) -> None: """显示目录及其所有子组件""" spaces = " " * indent print(f"{spaces}📁 {self.name}/") # 递归显示所有子组件 for child in self._children: child.display(indent + 1) def get_size(self) -> int: """计算目录总大小(包含所有子组件)""" total_size = 0 for child in self._children: total_size += child.get_size() return total_size def find_component(self, name: str) -> FileSystemComponent: """查找指定名称的组件""" for child in self._children: if child.name == name: return child if isinstance(child, Directory): found = child.find_component(name) if found: return found return None 完整示例:文件系统模拟让我们通过一个完整的例子来演示组合模式的实际应用: 实例
def demonstrate_composite_pattern():
"""演示组合模式的使用""" # 创建根目录 root = Directory("root") # 创建子目录 documents = Directory("documents") pictures = Directory("pictures") music = Directory("music") # 创建文件 readme = File("README.txt", 1024) notes = File("notes.md", 2048) photo1 = File("vacation.jpg", 1536000) photo2 = File("family.png", 2048000) song1 = File("song1.mp3", 4096000) song2 = File("song2.mp3", 5120000) # 构建目录结构 root.add(readme) root.add(documents) root.add(pictures) root.add(music) documents.add(notes) pictures.add(photo1) pictures.add(photo2) music.add(song1) music.add(song2) # 显示整个文件系统结构 print("=== 文件系统结构 ===") root.display() print("\n=== 大小统计 ===") print(f"根目录总大小: {root.get_size()} bytes") print(f"图片目录大小: {pictures.get_size()} bytes") print(f"音乐目录大小: {music.get_size()} bytes") print("\n=== 路径信息 ===") print(f"文件路径: {photo1.get_path()}") print(f"目录路径: {pictures.get_path()}") print("\n=== 查找组件 ===") found = root.find_component("song1.mp3") if found: print(f"找到文件: {found.get_path()}") # 运行演示 if __name__ == "__main__": demonstrate_composite_pattern() 运行上述代码,你将看到以下输出:
组合模式的 UML 类图
组合模式的优缺点优点
缺点
实际应用场景1. GUI 组件系统实例
class UIComponent:
"""UI 组件基类""" def render(self): pass def add(self, component): pass class Button(UIComponent): """按钮 - 叶子节点""" def render(self): print("渲染按钮") class Panel(UIComponent): """面板 - 组合节点""" def __init__(self): self.children = [] def add(self, component): self.children.append(component) def render(self): print("开始渲染面板") for child in self.children: child.render() print("结束渲染面板") 2. 组织架构管理实例
class Employee:
"""员工基类""" def get_salary(self): pass class Developer(Employee): """开发人员 - 叶子节点""" def __init__(self, salary): self.salary = salary def get_salary(self): return self.salary class Department(Employee): """部门 - 组合节点""" def __init__(self): self.employees = [] def add(self, employee): self.employees.append(employee) def get_salary(self): return sum(emp.get_salary() for emp in self.employees) 实践练习练习 1:扩展文件系统为文件系统添加以下功能:
练习 2:菜单系统设计设计一个餐厅菜单系统,要求:
练习 3:组织结构图创建一个公司组织结构系统:
总结组合模式通过统一的接口来处理单个对象和对象组合,使得客户端代码更加简洁和灵活。这种模式特别适用于具有层次结构的场景,如文件系统、GUI 组件、组织架构等。 关键要点:
AI 思考中...
点我分享笔记取消
分类导航
Advertisement
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |