组合模式
当需要处理一个有层次结构的数据时便于使用组合模式。比如需要输出公司组织机构的包含关系数据。
组合模式创建了对象组的树形结构,同时表示数据的层次关系。
依据树形结构来组合对象来表示整体部分的层次关系。这种数据类型的模式属于结构型模式。组合模式是用户对单个对象和组合对象的访问具有一致性。
代码示例:
/**
*组合模式下的 抽象方法 用于子类的实现
*/
public abstract class OrganizationComposite {
private String name;
private String 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;
}
public OrganizationComposite(String name, String des) {
this.name = name;
this.des = des;
}
protected void add(OrganizationComposite organizationComposite) {
throw new UnsupportedOperationException();
}
protected void remove(OrganizationComposite organizationComposite) {
throw new UnsupportedOperationException();
}
protected abstract void print();
}
/**
* 学校
* 可以管理下一个层级的数据
*/
public class Unversity extends OrganizationComposite {
List<OrganizationComposite> organizationComposites = new ArrayList<>();
public Unversity(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationComposite organizationComposite) {
organizationComposites.add(organizationComposite);
}
@Override
protected void remove(OrganizationComposite organizationComposite) {
organizationComposites.remove(organizationComposite);
}
@Override
public String getDes() {
return super.getDes();
}
@Override
public String getName() {
return super.getName();
}
@Override
protected void print() {
System.out.println("===========" + getName() + "==========");
for (OrganizationComposite organizationComposite : organizationComposites) {
organizationComposite.print();
}
}
}
/**
* 院系 组合下一个层级
*/
public class College extends OrganizationComposite {
List<OrganizationComposite> organizationComposites = new ArrayList<>();
public College(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationComposite organizationComposite) {
organizationComposites.add(organizationComposite);
}
@Override
protected void remove(OrganizationComposite organizationComposite) {
organizationComposites.remove(organizationComposite);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
@Override
protected void print() {
System.out.println("===========" + getName() + "==========");
for (OrganizationComposite organizationComposite : organizationComposites) {
organizationComposite.print();
}
}
}
public class Department extends OrganizationComposite {
public Department(String name, String des) {
super(name, des);
}
@Override
public String getDes() {
return super.getDes();
}
@Override
public String getName() {
return super.getName();
}
@Override
protected void print() {
System.out.println(getName());
}
}
public class Client {
public static void main(String[] args) {
OrganizationComposite unversityComposite=new Unversity("南昌大学","南昌最有名的大学");
OrganizationComposite collegeComposite=new College("南昌信息院","挺不错的");
OrganizationComposite collegeComposite2=new College("南昌计算机院","挺有趣的");
unversityComposite.add(collegeComposite);
unversityComposite.add(collegeComposite2);
OrganizationComposite department=new Department("java 软件开发","非你莫属");
OrganizationComposite department2=new Department("网络编程","你来就对了");
collegeComposite.add(department);
collegeComposite2.add(department2);
unversityComposite.print();
}
}
简化客户端操作,客户端只需要面对一致的对象,而不需要考虑整体部分或叶子部分。具有较强的扩展性。方便创建出复杂的层次结构。