Java设计模式之组合模式深入刨析_第1页
Java设计模式之组合模式深入刨析_第2页
Java设计模式之组合模式深入刨析_第3页
Java设计模式之组合模式深入刨析_第4页
Java设计模式之组合模式深入刨析_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第Java设计模式之组合模式深入刨析目录1.基本介绍2.结构3.组合模式解决的问题4.组合模式解决学校院系展示5.组合模式的注意事项和细节

1.基本介绍

1)组合模式(CompositePattern),又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示整体-部分的层次关系

2)组合模式依据树形结构来组合对象,用来表示部分以及整体层次

3)这种类型的设计模式属于结构型模式

4)组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象

2.结构

组合模式主要包含三种角色:

抽象根节点(Component):定义系统各层次对象的共有方法和属性,可以预定义一些默认行为和属性树枝节点(Composite):定义树枝节点的行为,存储子节点,组合树枝节点和叶子节点形成一个树形结构叶子节点(Leaf):叶子节点对象,其下再无分支,是系统层次遍历的最小单位

3.组合模式解决的问题

1)组合模式解决这样的问题,当我们要处理的对象可以生成一颗树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子

2)对应的示意图

4.组合模式解决学校院系展示

1)应用实例要求

编写程序展示一个学校院系结构:需求是这样的,要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系

2)思路分析和图解(类图)

3)代码实现

Component组合对象声明接口

packagecom.zte;

publicabstractclassOrganizationComponent{

privateStringname;//名字

privateStringdes;//说明

publicStringgetName(){

returnname;

publicStringgetDes(){

returndes;

protectedvoidadd(OrganizationComponentorganizationComponent){

//默认实现

thrownewUnsupportedOperationException();

protectedvoidremove(OrganizationComponentorganizationComponent){

//默认实现

thrownewUnsupportedOperationException();

//构造器

publicOrganizationComponent(Stringname,Stringdes){

=name;

this.des=des;

//方法print,抽象方法

protectedabstractvoidprint();

}

Composite非叶子节点

packagecom.zte;

importjava.util.ArrayList;

importjava.util.List;

//University就是Composite,可以管理College

publicclassUniversityextendsOrganizationComponent{

ListOrganizationComponentorganizationComponentList=newArrayList();

//构造器

publicUniversity(Stringname,Stringdes){

super(name,des);

//重写add

@Override

protectedvoidadd(OrganizationComponentorganizationComponent){

organizationComponentList.add(organizationComponent);

//重写remove

@Override

protectedvoidremove(OrganizationComponentorganizationComponent){

organizationComponent.remove(organizationComponent);

@Override

protectedvoidprint(){

System.out.println("=========="+getName()+"=========");

for(OrganizationComponentorganizationComponent:organizationComponentList){

organizationComponent.print();

@Override

publicStringgetName(){

returnsuper.getName();

@Override

publicStringgetDes(){

returnsuper.getDes();

}

Composite非叶子节点

packagecom.zte;

importjava.util.ArrayList;

importjava.util.List;

publicclassCollegeextendsOrganizationComponent{

//list中存放department

ListOrganizationComponentorganizationComponentList=newArrayList();

publicCollege(Stringname,Stringdes){

super(name,des);

//重写add

@Override

protectedvoidadd(OrganizationComponentorganizationComponent){

organizationComponentList.add(organizationComponent);

//重写remove

@Override

protectedvoidremove(OrganizationComponentorganizationComponent){

organizationComponent.remove(organizationComponent);

@Override

protectedvoidprint(){

System.out.println("=========="+getName()+"=========");

for(OrganizationComponentorganizationComponent:organizationComponentList){

organizationComponent.print();

@Override

publicStringgetName(){

returnsuper.getName();

@Override

publicStringgetDes(){

returnsuper.getDes();

}

Leaf叶子节点

packagecom.zte;

publicclassDepartmentextendsOrganizationComponent{

publicDepartment(Stringname,Stringdes){

super(name,des);

//add和remove方法就不需要再写了

@Override

protectedvoidprint(){

System.out.println("==========="+getName()+"=========");

@Override

publicStringgetName(){

returnsuper.getName();

@Override

publicStringgetDes(){

returnsuper.getDes();

}

packagecom.zte;

publicclassClient{

publicstaticvoidmain(String[]args){

//创建大学

OrganizationComponentuniversity=newUniversity("清华大学","中国最好的大学");

//创建学院

OrganizationComponentcollege1=newCollege("计算机学院","计算机学院");

OrganizationComponentcollege2=newCollege("信息工程学院","信息工程学院");

//创建各个学院下面的系

college1.add(newDepartment("软件工程","软件工程"));

college1.add(newDepartment("网络工程","网络工程"));

college1.add(newDepartment("计算机科学与技术","老牌专业"));

college2.add(newDepartment("通信工程","通信工程"));

college2.add(newDepartment("信息工程","信息工程"));

//将学院添加到学校中

university.add(college1);

university.add(college2);

//打印大学底下的所有院系

university.print();

//打印学院底下的所有系

college1.print();

}

5.组合模式的注意事项和细节

1)简化客户端操作,客户端只需

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论