组合模式
2016-06-27 11:24:04 0 举报
组合模式是一种结构型设计模式,它将对象组合成树形结构以表示“部分整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。通过使用组合模式,我们可以将复杂的对象表示为更简单的子对象的组合,从而简化了代码的复杂度。在实际应用中,组合模式可以用于实现文件系统、组织结构、图形绘制等场景。
作者其他创作
大纲/内容
Left
+Leaf(String name)+operation()+add()+remove()+getAll()
Component:抽象父类Composite:组合对象类Left:具体对象类
public class Client {\tpublic static void main(String[] args) {\t\tComponent root = new Composite(\"根目录\");\t\tComponent file1 = new Leaf(\"文件1\");\t\troot.add(file1);\t\tComponent dir1 = new Composite(\"子目录1\");\t\tComponent file2 = new Leaf(\"文件2\");\t\tdir1.add(file2);\t\troot.add(dir1);\t\troot.getAll();\t\t\tdir1.getAll();\t}}
Composite
-clist:List
+Composite(String name)+Composite()+operation()+add(Component component)+remove(Component component)+getAll()
Component
-name:String
+Component(String name)+Component()+add(Component component)+remove(Component component)+getAll()
0 条评论
下一页