美文网首页
11.6设计模式-组合模式-详解

11.6设计模式-组合模式-详解

作者: 205蚁 | 来源:发表于2018-11-18 17:28 被阅读0次

设计模式-组合模式

    1. 组合模式详解
    1. 组合模式在android中的实际运用


      11_1-11-7[01_45_32][20180802-202841-4].jpg

1.组合模式详解

1.概念
    将对象以属性结构组织起来,以达成“部分-整体”的层次结构,使得客户端对单个对象和组合对象的使用具有一致性
    树的结构(二叉树,平衡二叉树)-->组合设计模式
2.使用场景
    1.需要表示一个对象整体或部分层次。他有整体和层次的区分,又让客户端使用一致
    2.让客户端能够忽略不同对象层次的变化。客户端只需要对抽象的结构编程,而不需要关心每一个对象层次的细节
3.UML结构图分析
    图;
                                        Component
                                
                                +operation():void
                                +add(com:Component):void
                                +remove(com:Component):void
                                +getChild():int 
    
        client 
                
                     Leaf                           Composite
            
                +operation():void               +operation():void
                                                +add(com:Component):void
                                                +remove(com:Component):void
                                                +getChild(i:int):Component
                                                
        组合设计模式包含了四个部分,Client,Component抽象组件(它有叶子组件和组成组件)
        Component:最重要部分:是一个接口,也可以管理所有的子对象,如果要递归,使用Component即可
        Leaf: 叶子节点没有子节点
        Composite:树枝节点,定义了子部件的一些部分行为,实现了抽象构建Component的方法
        
        Client通过Component控制Leaf和Composite
        
        
4.实际代码分析
    1.抽象类
    public abstract class File{ //可以定义接口,也可以是抽象方法
        private String name;
        private get/setName();
        
        public abstract void watch();
        public void add(File file){
            throw new UnsupportedOperationException();
        }
        public void remove(File file){
            throw new UnsupportedOperationException();
        }
        publci File getChild(int position){
            throw new UnsupportedOperationException();
        }
        
    }
    三个方法组合方法:add、remove、getChild
    抽象方法watch()留给子类实现
    
    2.实现类 (1.文件夹Folder 2.文本文件)
        public class Folder extends File{
            private List<File> mFileList;
            public Folder(String name){
                super(name);
                mFileList = new ArrayList<>();
            }
            public void watch(){
                StringBuffer fileName = new StringBuffer();
                for(File file:mFileList){
                    fileName.append(file.getName()+";");
                }
                System.out.printf("组合模式","这是一个叫"+getName()+"文件夹,包含"+mFileList.size()+"个文件,分别是"+fileName)
            }
            public void add(File file){
                mFileList.add(file);
            }
            public void remove(File file){
                mFileList.add(file);
            }
            public File getChild(int position){
                return mFileList.get(position);
            }
        
        }
        
        public class TextFile extends File{
            public TextFile(String name){
                super(name);
            }
            
            public void watch(){
                System.out.print("组合模式","这是一个叫"+getName()+"的文本文件");
            }
        }
    
        3.调用
            public static void main(String[] args){
                TextFile txA = new TextFile("a.txt");
                TextFile txB = new TextFile("b.txt");
                TextFile txc = new TextFile("c.txt");
                
                txA.watch();
                Folder folder = new Folder("某个文件夹名称"):
                folder.add(txA);
                folder.add(txB);
                folder.add(txc);
                folder.watch();
                folder.getChild(1).watch();
                
            }
            属性结构中,不管是Folder,还是TextFile,它们都是抽象File的节点,我们只关心File的改变,把File封装好,来进行选择。
            
        总结: 整体到部分的组合模式。
            
            
5.组合模式的优点
    1.高层模块调用简单,只需要调用高层的api,而不需要访问所有组成部分
    2.节点自由增加(刚才两个子类,可以更多)
缺点:控制树枝构建的类型就不太容易了,定义好类型之后,想改变的话不好整,继承方法增加新的方法比较麻烦

2.组合模式在android中的实际运用

1.Android中View的结构是树形结构的

ViewGroup

11_1-11-7[01_45_32][20180802-202841-4].jpg 11-8_11章结束[00_00_31][20180802-205510-5].jpg 11-8_11章结束[00_01_15][20180802-205604-6].jpg 11-8_11章结束[00_02_42][20180802-205617-7].jpg 11-8_11章结束[00_03_07][20180802-205623-8].jpg

相关文章

网友评论

      本文标题:11.6设计模式-组合模式-详解

      本文链接:https://www.haomeiwen.com/subject/qwpgfqtx.html