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

103 阅读2分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第1天,点击查看活动详情

组合 设计模式

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("不支持打印操作");
    }
}

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+"");
    }
}

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();
        }
    }
}

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();
    }
}

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