【学习打卡】第5天 Java设计模式精讲-Debug方式+内存分析 第十四讲
课程名称:Java设计模式精讲-Debug方式+内存分析,真正学懂设计模式
课程章节: 组合模式讲解+Coding+源码解析
主讲老师:Geely
课程内容:
今天学习的内容包括:
什么是组合模式 组合模式 优点 缺点 Coding 源码解析 以及在业务上的应用
课程收获:
组合 设计模式
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图
今天学习课程共用了1.5个小时,重新学习一下设计模式 更加清楚知道组合设计模式的应用以及如何在自己项目当中去使用它 大家一起加油 💪🏻
共同學習,寫下你的評論
評論加載中...
作者其他優質文章







