您好,欢迎访问一九零五行业门户网

Java设计模式之组合模式实例分析

1.基本介绍1)组合模式(composite pattern),又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体-部分”的层次关系
2)组合模式依据树形结构来组合对象,用来表示部分以及整体层次
3)这种类型的设计模式属于结构型模式
4)组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象
2.结构组合模式主要包含三种角色:
抽象根节点(component):定义系统各层次对象的共有方法和属性,可以预定义一些默认行为和属性
树枝节点(composite):定义树枝节点的行为,存储子节点,组合树枝节点和叶子节点形成一个树形结构
叶子节点(leaf):叶子节点对象,其下再无分支,是系统层次遍历的最小单位
3.组合模式解决的问题1)组合模式解决这样的问题,当我们要处理的对象可以生成一颗树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子
2)对应的示意图
4.组合模式解决学校院系展示1)应用实例要求
编写程序展示一个学校院系结构:需求是这样的,要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系
2)思路分析和图解(类图)
3)代码实现
component组合对象声明接口
package com.zte;public abstract class organizationcomponent { private string name;// 名字 private string des;// 说明 public string getname() { return name; } public string getdes() { return des; } protected void add(organizationcomponent organizationcomponent) { // 默认实现 throw new unsupportedoperationexception(); } protected void remove(organizationcomponent organizationcomponent) { // 默认实现 throw new unsupportedoperationexception(); } // 构造器 public organizationcomponent(string name, string des) { this.name = name; this.des = des; } // 方法print,抽象方法 protected abstract void print();}
composite非叶子节点
package com.zte;import java.util.arraylist;import java.util.list;// university 就是 composite,可以管理collegepublic class university extends organizationcomponent { list<organizationcomponent> organizationcomponentlist = new arraylist<>(); // 构造器 public university(string name, string des) { super(name, des); } //重写add @override protected void add(organizationcomponent organizationcomponent) { organizationcomponentlist.add(organizationcomponent); } // 重写remove @override protected void remove(organizationcomponent organizationcomponent) { organizationcomponent.remove(organizationcomponent); } @override protected void print() { system.out.println("==========" + getname() + "========="); for (organizationcomponent organizationcomponent : organizationcomponentlist) { organizationcomponent.print(); } } @override public string getname() { return super.getname(); } @override public string getdes() { return super.getdes(); }}
composite非叶子节点
package com.zte;import java.util.arraylist;import java.util.list;public class college extends organizationcomponent { // list中存放department list<organizationcomponent> organizationcomponentlist = new arraylist<>(); public college(string name, string des) { super(name, des); } //重写add @override protected void add(organizationcomponent organizationcomponent) { organizationcomponentlist.add(organizationcomponent); } // 重写remove @override protected void remove(organizationcomponent organizationcomponent) { organizationcomponent.remove(organizationcomponent); } @override protected void print() { system.out.println("==========" + getname() + "========="); for (organizationcomponent organizationcomponent : organizationcomponentlist) { organizationcomponent.print(); } } @override public string getname() { return super.getname(); } @override public string getdes() { return super.getdes(); }}
leaf叶子节点
package com.zte;public class department extends organizationcomponent { public department(string name, string des) { super(name, des); } // add和remove方法就不需要再写了 @override protected void print() { system.out.println("===========" + getname() + "========="); } @override public string getname() { return super.getname(); } @override public string getdes() { return super.getdes(); }}
package com.zte;public class client { public static void main(string[] args) { // 创建大学 organizationcomponent university = new university("清华大学", "中国最好的大学"); // 创建学院 organizationcomponent college1 = new college("计算机学院", "计算机学院"); organizationcomponent college2 = new college("信息工程学院", "信息工程学院"); // 创建各个学院下面的系 college1.add(new department("软件工程", "软件工程")); college1.add(new department("网络工程", "网络工程")); college1.add(new department("计算机科学与技术", "老牌专业")); college2.add(new department("通信工程", "通信工程")); college2.add(new department("信息工程", "信息工程")); // 将学院添加到学校中 university.add(college1); university.add(college2); // 打印大学底下的所有院系 university.print(); // 打印学院底下的所有系 college1.print(); }}
5.组合模式的注意事项和细节1)简化客户端操作,客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题
2)其有较强的扩展性,当我们需要修改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动
3)方便创建出复杂的层次结构,客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构
4)需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式
5)要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式
以上就是java设计模式之组合模式实例分析的详细内容。
其它类似信息

推荐信息