zoukankan      html  css  js  c++  java
  • 北风设计模式课程---组合模式

    北风设计模式课程---组合模式

    一、总结

    一句话总结:

    不仅要通过视频学,还要看别的博客里面的介绍,搜讲解,搜作用,搜实例
    设计模式都是对生活的抽象,比如用户获得装备,我可以先装备工厂先生产出来装备,然后给宗门武器库,宗门武器库发给我,如果是打怪获得的装备,可以是装备工厂把装备给的怪物装备库

    1、组合模式的作用是什么?

    组合能让客户以【一致的方式处理】个别对象以及组合对象。

    2、组合模式解决什么问题?

    当我们的【要处理的对象可以生成一颗树形结构】,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子。

    3、组合模式的使用场景是什么?

    当需要遍历组织机构,或者处理的对象具有树形结构时使用

    4、组合模式优点?

    (1)、简化客户端操作:客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题。
    (2)、具有较强的扩展性:当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动。
    (3)、方便创建出复杂的层次结构:客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构。

    5、组合模式缺点?

    要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,难以实现组合模式。

    二、设计模式(十二):组合模式

    转自或参考:设计模式(十二):组合模式
    https://www.cnblogs.com/jenkinschan/p/6071933.html

    一、概述

      允许你将对象组合成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及组合对象。

    二、解决问题

      组合模式解决这样的问题,当我们的要处理的对象可以生成一颗树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子。

    三、结构类图

    四、应用实例

      上一讲中,我们是以大学的院系结构来讲解迭代器的,在组合模式中,我们还是引用这个例子。把学校作为根节点,学院做普通节点,专业就是叶子。

      首先,从上面的类图中知道,对于客户端来说,其需要面对的就是一个组件,而不用管是否节点还是叶子。我们就先来创建这样一个组件

    package com.jet.pattern.Composite;
    
    /**
     * description:
     * 机构组件,学院和系称为机构
     * Created by Administrator on 2016/11/17.
     */
    public abstract class OrganizationComponent {
        // 每个方法提供默认的实现
        public void add(OrganizationComponent organizationComponent){
            throw new UnsupportedOperationException();
        }
    
        public void remove(OrganizationComponent organizationComponent){
            throw new UnsupportedOperationException();
        }
    
        public String getName(){
            throw new UnsupportedOperationException();
        }
    
        public String getDescription(){
            throw new UnsupportedOperationException();
        }
    
        public void print(){
            throw new UnsupportedOperationException();
        }
    
    }

    接着从上到下创建组件对象,创建学校对象

    package com.jet.pattern.Composite.impl;
    
    import com.jet.pattern.Composite.OrganizationComponent;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * description:
     * 大学对象
     * Created by Administrator on 2017/1/13.
     */
    public class University extends OrganizationComponent {
        String name;
        String description;
        List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
    
        public University(String name, String description) {
            this.name = name;
            this.description = description;
        }
    
        // 重写机构组件的方法,其作为树有增加和删除方法
        @Override
        public void add(OrganizationComponent organizationComponent) {
            organizationComponents.add(organizationComponent);
        }
    
        @Override
        public void remove(OrganizationComponent organizationComponent) {
            organizationComponents.remove(organizationComponent);
        }
    
        @Override
        public String getName() {
            return name;
        }
    
        @Override
        public String getDescription() {
            return description;
        }
    
        @Override
        public void print() {
            System.out.println("-------" + getName() + "-----------");
            // 大学下面有很多学院,把他们遍历出来
            for(OrganizationComponent organizationComponent : organizationComponents){
                organizationComponent.print();
            }
        }
    }

    创建学院对象

    package com.jet.pattern.Composite.impl;
    
    import com.jet.pattern.Composite.OrganizationComponent;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * description:
     * 学院是一个机构,
     * Created by Administrator on 2017/1/13.
     */
    public class College extends OrganizationComponent {
        String name;
        String description;
        List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
    
        public College(String name, String description) {
            this.name = name;
            this.description = description;
        }
    
        // 重写机构组件的方法,其作为树有增加和删除方法
        @Override
        public void add(OrganizationComponent organizationComponent) {
            organizationComponents.add(organizationComponent);
        }
    
        @Override
        public void remove(OrganizationComponent organizationComponent) {
            organizationComponents.remove(organizationComponent);
        }
    
        @Override
        public String getName() {
            return name;
        }
    
        @Override
        public String getDescription() {
            return description;
        }
    
        @Override
        public void print() {
            System.out.println("-------" + getName() + "-----------");
            // 学院下面有很多专业,把他们遍历出来
            for(OrganizationComponent organizationComponent : organizationComponents){
                organizationComponent.print();
            }
        }
    }

    创建专业(系)对象

    package com.jet.pattern.Composite.impl;
    
    import com.jet.pattern.Composite.OrganizationComponent;
    
    /**
     * description:
     * 专业(系)对象
     * Created by Administrator on 2017/1/13.
     */
    public class Department extends OrganizationComponent {
        String name;
        String description;
    
        public Department(String name, String description) {
            this.name = name;
            this.description = description;
        }
    
        // 重写机构组件的方法,其作为叶子没有增加和删除方法
        @Override
        public String getName() {
            return name;
        }
    
        @Override
        public String getDescription() {
            return description;
        }
    
        // 叶子只需要输出自己的信息
        @Override
        public void print() {
            System.out.println(getName());
        }
    }

    创建输出对象,模拟客户端

    package com.jet.pattern.Composite.impl;
    
    import com.jet.pattern.Composite.OrganizationComponent;
    
    /**
     * description:
     * 输出信息,模拟客户调用
     * Created by Administrator on 2017/1/13.
     */
    public class OutputInfo {
        OrganizationComponent allOrganization;
    
        public OutputInfo(OrganizationComponent allOrganization) {
            this.allOrganization = allOrganization;
        }
    
        public void printOrganization(){
            allOrganization.print();
        }
    }

    测试组合模式

    package com.jet.pattern.Composite.test;
    
    import com.jet.pattern.Composite.OrganizationComponent;
    import com.jet.pattern.Composite.impl.College;
    import com.jet.pattern.Composite.impl.Department;
    import com.jet.pattern.Composite.impl.OutputInfo;
    import com.jet.pattern.Composite.impl.University;
    
    /**
     * description:
     * 测试组合模式
     * Created by Administrator on 2017/1/13.
     */
    public class CompositeTest {
        public static void main(String[] args) {
            // 从大到小创建对象,学院和专业组合成为学校,先创建学校,它也是机构组件
            OrganizationComponent university = new University("清华大学","全国最好的大学");
    
            // 接着创建学院
            OrganizationComponent computerCollege = new College("计算机学院","计算机学院");
            OrganizationComponent infoEngineeringCollege = new College("信息工程学院","信息工程学院");
    
            // 计算机学院有下面专业
            computerCollege.add(new Department("计算机科学与技术","计算机科学与技术"));
            computerCollege.add(new Department("软件工程 ","软件工程"));
            computerCollege.add(new Department("网络工程","网络工程"));
    
            // 信息工程学院有下面专业
            infoEngineeringCollege.add(new Department("通信工程","通信工程"));
            infoEngineeringCollege.add(new Department("信息工程","信息工程"));
    
            // 学校有下面学院
            university.add(computerCollege);
            university.add(infoEngineeringCollege);
    
            // 输出学校机构信息
            OutputInfo info = new OutputInfo(university);
            info.printOrganization();
        }
    }

    测试输出结果如下:

    五、使用场景

      1.当需要遍历组织机构,或者处理的对象具有树形结构时使用。

    六、优缺点

      1.优点

      (1)、简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题。

      (2)、具有较强的扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动。

      (3)、方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构。

      2.缺点

      (1)、要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,难以实现组合模式。

     
  • 相关阅读:
    Python 学习笔记(十三)Python函数(二)
    Python 学习笔记(十三)Python函数(一)
    Python 学习笔记(十二)Python文件和迭代(二)
    tb数据过多用省略号显示
    js,el表达式,<c:if>
    html元素标签时间格式化
    oracle链接报错shared memory realm does not exist
    mysql查找字段在哪个表中
    删除数据库重复数据
    excel使用poi操作。
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/11031947.html
Copyright © 2011-2022 走看看