Python 官方文档:入门教程 => 点击学习
目录模式介绍UML类图组合模式案例组合模式的注意事项和细节模式介绍 组合模式(Composite Pattern),又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以
类图解析:
背景介绍:一所大学包含多个学院,学院又包含多个学科专业
OrganizationComponent(抽象类)
public abstract class OrganizationComponent {
private String name;
private String des;
protected void add(OrganizationComponent o) {
throw new UnsupportedOperationException("该方法不支持");
}
protected void remove(OrganizationComponent o) {
throw new UnsupportedOperationException("该方法不支持");
}
public OrganizationComponent(String name, String des) {
this.name = name;
this.des = des;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
protected abstract void print();
}
Department(专业类)
public class Department extends OrganizationComponent{
public Department(String name, String des) {
super(name, des);
}
@Override
protected void print() {
System.out.println(getName());
}
}
College(学院类)
public class College extends OrganizationComponent {
List<OrganizationComponent> departments = new ArrayList<>();
public College(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationComponent o) {
departments.add(o);
}
@Override
protected void remove(OrganizationComponent o) {
departments.remove(o);
}
@Override
protected void print() {
System.out.println("-----------" + getName() + "-----------");
departments.forEach(OrganizationComponent::print);
}
}
University(大学类)
public class University extends OrganizationComponent {
List<OrganizationComponent> colleges = new ArrayList<>();
public University(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationComponent o) {
colleges.add(o);
}
@Override
protected void remove(OrganizationComponent o) {
colleges.remove(o);
}
@Override
protected void print() {
System.out.println("-----------" + getName() + "-----------");
colleges.forEach(OrganizationComponent::print);
}
}
Client(测试类)
public class Client {
public static void main(String[] args) {
// 创建大学
University university = new University("清华大学", "中国顶级大学");
// 计算机学院
College computerCollege = new College("计算机学院", "计算机学院");
College infoEngineerCollege = new College("信息工程学院", "信息工程学院");
// 创建专业 并添加
computerCollege.add(new Department("软件工程","软件工程不错"));
computerCollege.add(new Department("网络工程","网络工程不错"));
computerCollege.add(new Department("计算机科学与技术","计算机科学与技术老牌专业"));
infoEngineerCollege.add(new Department("通信工程","通信工程不好学"));
infoEngineerCollege.add(new Department("信息工程","信息工程好学"));
// 添加学院到大学
university.add(computerCollege);
university.add(infoEngineerCollege);
// 打印
university.print();
}
}
实现结果:
到此这篇关于Java设计模式中的组合模式的文章就介绍到这了,更多相关Java组合模式内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java设计模式中的组合模式
本文链接: https://www.lsjlt.com/news/164574.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0