架构师训练营 -week3- 作业
发布于: 2020 年 06 月 24 日
作业一:
请在草稿纸上手写一个单例模式的实现代码
作业二:
作业描述
组合模式
组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。
代码实现
class Component(object): """ 组件抽象类 """ def __init__(self, name): self.name = name def add(self, comp): """ 添加组件 :param comp: 组件 :return: """ pass def remove(self, comp): """ 移除组件 :param comp: 组件 :return: """ pass def display(self): passclass Leaf(Component): """ 叶节点 """ def __init__(self, name): super().__init__(name) def display(self): """ 打印叶节点 :return: """ print(self.name)class Composite(Component): """ 枝节点 可以添加子节点 """ def __init__(self, name): super().__init__(name) self.children = [] def add(self, comp): """""" self.children.append(comp) def remove(self, comp): self.children.remove(comp) def display(self): """ 先输出自身 再挨个输出子节点 :return: """ print(self.name) for comp in self.children: comp.display() if __name__ == "__main__": winForm = Composite("WinForm(WINDOW窗口)") winForm.add(Leaf("Picture(LOGO图片)")) winForm.add(Leaf("Button(登录)")) winForm.add(Leaf("Button(注册)")) frame = Composite("Frame(FRAME1)") winForm.add(frame) frame.add(Leaf("Lable(用户名)")) frame.add(Leaf("TextBox(文本框)")) frame.add(Leaf("Lable(密码)")) frame.add(Leaf("PasswordBox(密码框)")) frame.add(Leaf("CheckBox(复选框)")) frame.add(Leaf("TextBox(记住用户名)")) frame.add(Leaf("LinkLable(忘记密码)")) winForm.display()
打印结果
划线
评论
复制
发布于: 2020 年 06 月 24 日 阅读数: 28
Geek_5a6ca3
关注
还未添加个人签名 2019.09.16 加入
还未添加个人简介
评论