• 重学设计模式之-组合模式-mybatis-SqlNode使用组合模式


    组合 设计模式

    1.定义

    ​ 将对象组合成树型结构以表示“部分-整体” 的层次结构

    1.1 特点

    ​ 组合模式使客户端对单个对象和组合对象保持一致的方式处理

    1.2 类型 : 结构型

    2.适用场景

    ​ 1、希望客户端可以忽略组合对象与单个对象的差异时

    ​ 2、处理一个树型结构时

    3.缺点

    1.限制类型时会较为复杂

    2.使设计变得更加抽象

    4.优点

    ​ 1.清楚地定义分层次的复杂对象,表示对象的全部或部分层次

    ​ 2.让客户端忽略了层次的差异 方便对整个层次结构进行控制

    ​ 3.简化客户端代码

    ​ 4.符合开闭原则

    5.组合-相关的设计模式

    组合模式和访问者模式

    6.uml 设计图
    在这里插入图片描述

    8.代码如下

    package com.zw.design.pattern.creational.structural.composite;
    
    import java.math.BigDecimal;
    
    /****
     * 抽象组件
     */
    public abstract class CatalogComponent {
    
        public void  add(CatalogComponent catalogComponent){
            throw new UnsupportedOperationException("不支持添加操作");
        }
    
        public void  remove(CatalogComponent catalogComponent){
            throw new UnsupportedOperationException("不支持删除操作");
        }
    
        public String  getName(CatalogComponent catalogComponent){
            throw new UnsupportedOperationException("不支持获取名称操作");
        }
    
        public BigDecimal getPrice(CatalogComponent catalogComponent){
            throw new UnsupportedOperationException("不支持获取价格操作");
        }
    
        public void print(){
            throw new UnsupportedOperationException("不支持打印操作");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    package com.zw.design.pattern.creational.structural.composite;
    
    import java.math.BigDecimal;
    
    /****
     * 课程类
     */
    public class Course extends  CatalogComponent{
    
        private String name;
        private BigDecimal price;
    
        public Course(String name, BigDecimal price) {
            this.name = name;
            this.price = price;
        }
    
        @Override
        public String  getName(CatalogComponent catalogComponent) {
            return  this.name;
        }
    
        @Override
        public BigDecimal getPrice(CatalogComponent catalogComponent) {
            return this.price;
        }
    
        @Override
        public void print() {
            System.out.println("catalogComponent =名字 " + name +" 价格 "+ price+"");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    2.课程目录类

    package com.zw.design.pattern.creational.structural.composite;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /****
     * 课程目录
     */
    public class CourseCatalog  extends CatalogComponent{
        private List<CatalogComponent> items=new ArrayList<CatalogComponent>();
        private String name;
        private Integer level;//级别
    
        public CourseCatalog(String name,Integer level){
            this.name=name;
            this.level=level;
        }
    
        @Override
        public String getName(CatalogComponent catalogComponent) {
            return this.name;
        }
    
        @Override
        public void add(CatalogComponent catalogComponent) {
            items.add(catalogComponent);
        }
    
        @Override
        public void remove(CatalogComponent catalogComponent) {
            items.remove(catalogComponent);
        }
    
        @Override
        public void print() {
            System.out.println(this.name);
            for (CatalogComponent catalogComponent:items) {
                if (this.level!=null){
                    for (int i = 0; i < this.level; i++) {
                        System.out.print("    ");
                    }
                }
                catalogComponent.print();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    3.测试类

    package com.zw.design.pattern.creational.structural.composite;
    
    import java.math.BigDecimal;
    
    public class Test {
    
        public static void main(String[] args) {
            CatalogComponent linux=new Course("linux课程",new BigDecimal("2"));
            CatalogComponent window=new Course("windows课程",new BigDecimal("4"));
    
            CatalogComponent javaAdd=new CourseCatalog("java课程目录",2);
            CatalogComponent designPattern=new Course("设计模式",new BigDecimal("66"));
            CatalogComponent spring=new Course("spring源码解析",new BigDecimal("88"));
            javaAdd.add(designPattern);
            javaAdd.add(spring);
    
    
            CatalogComponent itzhouwei=new CourseCatalog("zw课程主目录",1);
            itzhouwei.add(linux);
            itzhouwei.add(window);
            itzhouwei.add(javaAdd);
            itzhouwei.print();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    测试结果如下
    在这里插入图片描述组合设计模式jdk当中应用解析
    在jdk 当中Container 当中是用组合模式的代码如图
    在这里插入图片描述
    比如HashMap 当中也使用该设计模式 还有ArrayList 当中addAll方法也使用该设计模式
    在这里插入图片描述
    在mybatis 使用组合模式类 SqlNode 类 它使用的是接口 而我的案例当中使用抽象类
    在这里插入图片描述
    SqlNode uml图在这里插入图片描述
    代码如下

  • 相关阅读:
    著名童星刘佳琪深圳市中心举办个人Live音乐汇专辑发布会圆满成功
    使用uniapp开发时自定义tabbar
    node.js的安装和使用(保姆教程)
    MySQL基础篇【第四篇】| 连接查询、子查询(嵌套)
    8.StringTable(字符串常量池)
    SpringBoot底层原理----配置优先级/Bean管理/springboot原理
    基于Java毕业设计影院网上售票系统演示录像源码+系统+mysql+lw文档+部署软件
    php7.3安装phalcon扩展
    设计模式概述
    创建环境时提示:ERROR conda.core.link:_execute(502)
  • 原文地址:https://blog.csdn.net/qq_32048567/article/details/126082296