zoukankan      html  css  js  c++  java
  • 组合模式

    1.组合模式简介

    组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。属于结构型模式,它创建了对象组的树形结构(注意树形结构的对象比较适合使用组合模式)。这种模式创建了一个包含自己对象组的类,该类提供了修改相同对象组的方式。

    关键代码:树枝内部组合该接口,并且含有内部属性 List,里面放 Component。

    优点:高层模块调用简单,节点自由增加。

    缺点:在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。

    使用场景:部分、整体场景,如树形菜单,文件、文件夹的管理。

    注意事项:定义时为具体类。

    2.Demo1 CEO候选者

    import java.util.ArrayList;
    import java.util.List;
    
    class Employee {
        private String name;
        private String position;
        private List<Employee> candidates;
    
        public Employee(String name, String position) {
            this.name = name;
            this.position = position;
            this.candidates = new ArrayList<Employee>();
        }
    
        public void addCandidate(Employee e) {
            candidates.add(e);
        }
    
        public String getName() {
            return this.name;
        }
    
        public String getPosition() {
            return this.position;
        }
    
        public void printCandidates() {
            System.out.println("Candidates for position " + getPosition() + " are:");
            for (Employee e : candidates) {
                System.out.println("Name: " + e.getName() + "  Position: " + e.getPosition());
            }
        }
    }
    
    
    
    
    
    public class CompositePatternDemo1 {
        public static void main(String args[]) {
            Employee e1 = new Employee("ZhangShan", "Manager");
            Employee e2 = new Employee("LiShia", "Developer");
            Employee e3 = new Employee("WhangEr", "Leader");
            Employee e4 = new Employee("Mazhi", "CEO");
    
            e4.addCandidate(e1);
            e4.addCandidate(e2);
            e4.addCandidate(e3);
    
            e4.printCandidates();
        }
    }

    3.Demo2 文件目录遍历

    import java.util.ArrayList;
    import java.util.List;
    import java.io.*;
    
    abstract class Node {
        protected String name;
    
        public Node(String name) {
            this.name = name;
        }
    
        public void addNode(Node node) throws Exception { //本方法体内没有catch此抛出的异常就需要声明扔出这个异常
            throw new Exception("Invalid Arguement"); //注意这里是throw,上面是throws
        }
    
        abstract public void display();
    }
    
    class Filer extends Node {
        public Filer(String name) {
            super(name);
        }
    
        public void display() {
            System.out.println(name);
        }
    }
    
    class Noder extends Node {
        private List<Node> lists;
    
        public Noder(String name) {
            super(name);
            lists = new ArrayList<Node>();
        }
    
        public void addNode(Node node) throws Exception { //重写,这里面没有抛出异常,所以也可以不加throws Exception
            lists.add(node);
        }
    
        public void display() {
            System.out.println(name);
            for (Node node : lists) { //这是另一种形式的递归调用
                node.display();
            }
        }
    }
    
    
    public class CompositePatternDemo2 {
    
        public static void createTree(Node node) throws Exception { //没有catch addNode()抛出的异常,需要继续向上层调用抛出异常
            File file = new File(node.name);
            File[] lists = file.listFiles();
            for (File list : lists) {
                if (list.isFile()) {
                    Node f = new Filer(list.getAbsolutePath());
                    node.addNode(f);
                }
                if (list.isDirectory()) {
                    Node n = new Noder(list.getAbsolutePath());
                    node.addNode(n);
                    createTree(n);
                }
            }
        }
    
        public static void main(String args[]) {
            Node node = new Noder("./tmp");
            try {
                createTree(node); //这里处理了异常,终于不用再向上抛出异常了。
            } catch (Exception e) {
                e.printStackTrace();
            }
            node.display();
        }
    }

    参考:http://www.runoob.com/design-pattern/composite-pattern.html

  • 相关阅读:
    jqueryui 进度条使用
    第一阶段站立会议03
    第一阶段站立会议02
    第一阶段站立会议01
    找到了——电梯会议
    软件需求规格说明书
    团队项目计划会议
    软件开发团队简介
    找水王
    NABCD需求分析
  • 原文地址:https://www.cnblogs.com/hellokitty2/p/10696028.html
Copyright © 2011-2022 走看看