Java设计模式之组合模式实例分析
导读:本文共3560字符,通常情况下阅读需要12分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 1.基本介绍1)组合模式(Composite Pattern),又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体-部分”的层次关系2)组合模式依据树形结构来组合对象,用来表示部分以及整体层次3)这种类型的设计模式属于结构型模式4)组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象... ...
目录
(为您整理了一些要点),点击可以直达。1.基本介绍
1)组合模式(Composite Pattern),又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体-部分”的层次关系
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){this.name=name;this.des=des;}//方法print,抽象方法protectedabstractvoidprint();}
Composite非叶子节点
packagecom.zte;importjava.util.ArrayList;importjava.util.List;//University就是Composite,可以管理CollegepublicclassUniversityextendsOrganizationComponent{List<OrganizationComponent>organizationComponentList=newArrayList<>();//构造器publicUniversity(Stringname,Stringdes){super(name,des);}//重写add@Overrideprotectedvoidadd(OrganizationComponentorganizationComponent){organizationComponentList.add(organizationComponent);}//重写remove@Overrideprotectedvoidremove(OrganizationComponentorganizationComponent){organizationComponent.remove(organizationComponent);}@Overrideprotectedvoidprint(){System.out.println("=========="+getName()+"=========");for(OrganizationComponentorganizationComponent:organizationComponentList){organizationComponent.print();}}@OverridepublicStringgetName(){returnsuper.getName();}@OverridepublicStringgetDes(){returnsuper.getDes();}}
Composite非叶子节点
packagecom.zte;importjava.util.ArrayList;importjava.util.List;publicclassCollegeextendsOrganizationComponent{//list中存放departmentList<OrganizationComponent>organizationComponentList=newArrayList<>();publicCollege(Stringname,Stringdes){super(name,des);}//重写add@Overrideprotectedvoidadd(OrganizationComponentorganizationComponent){organizationComponentList.add(organizationComponent);}//重写remove@Overrideprotectedvoidremove(OrganizationComponentorganizationComponent){organizationComponent.remove(organizationComponent);}@Overrideprotectedvoidprint(){System.out.println("=========="+getName()+"=========");for(OrganizationComponentorganizationComponent:organizationComponentList){organizationComponent.print();}}@OverridepublicStringgetName(){returnsuper.getName();}@OverridepublicStringgetDes(){returnsuper.getDes();}}
Leaf叶子节点
packagecom.zte;publicclassDepartmentextendsOrganizationComponent{publicDepartment(Stringname,Stringdes){super(name,des);}//add和remove方法就不需要再写了@Overrideprotectedvoidprint(){System.out.println("==========="+getName()+"=========");}@OverridepublicStringgetName(){returnsuper.getName();}@OverridepublicStringgetDes(){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)简化客户端操作,客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题
2)其有较强的扩展性,当我们需要修改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动
3)方便创建出复杂的层次结构,客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构
4)需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式
5)要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
Java设计模式之组合模式实例分析的详细内容,希望对您有所帮助,信息来源于网络。