设计模式的重要性对于程序员来说,相当于盾牌对于美国队长,暴风战斧相对于雷神,内裤对于绿巨人(绿巨人最强武器,手动狗头)来说,是必不可少的。
在此,特别总结下23种设计模式:
本人将会以专栏形式详细介绍下每种设计模式,文章代码使用java语言,希望能够帮助读者打造一柄编码前行路上的神兵利器。
本文主要针对设计模式的第七种,组合模式进行详细介绍
编写程序展示一个学校院系结构:需求是这样,要在一个页面中展示出学校的院系组成,一个学校有多个学院, 一个学院有多个系。如图
/**
* 组织机构,顶级抽象类
*/
public abstract class OrganizationComponent {
String name;
String des;
protected void add(OrganizationComponent organizationComponent) {
//默认实现
throw new UnsupportedOperationException();
}
protected void remove(OrganizationComponent organizationComponent) {
//默认实现
throw new UnsupportedOperationException();
}
//抽象方法,需要子类实现
public abstract void print();
public OrganizationComponent(String name, String des) {
this.name = name;
this.des = des;
}
public OrganizationComponent() {
}
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;
}
}
/**
* 专业
*/
public class Department extends OrganizationComponent {
//List中存放的 Department
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
public Department(String name, String des) {
super(name, des);
}
/**
* 重写add
*
* @param organizationComponent
*/
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
/**
* 重写remove
*
* @param organizationComponent
*/
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public void print() {
System.out.println(getName());
}
}
/**
* 学院
*/
public class College extends OrganizationComponent {
//List中存放的 Department
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
public College(String name, String des) {
super(name, des);
}
/**
* 重写add
*
* @param organizationComponent
*/
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
/**
* 重写remove
*
* @param organizationComponent
*/
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public void print() {
System.out.println("--------------" + getName() + "--------------");
//遍历
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
/**
* 大学
*/
public class University extends OrganizationComponent {
//List中存放的 Department
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
public University(String name, String des) {
super(name, des);
}
/**
* 重写add
*
* @param organizationComponent
*/
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
/**
* 重写remove
*
* @param organizationComponent
*/
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public void print() {
System.out.println("*****************" + getName() + "*****************");
//遍历
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
public class Client {
public static void main(String[] args) {
OrganizationComponent university = new University("清华大学", " 中国顶级大学 ");
OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院 ");
OrganizationComponent 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 的集合类-HashMap 就使用了组合模式
读者有兴趣可以自行debug了解详情,这里就不做过多介绍了