当前位置:首页 > 科技  > 软件

一文彻底搞明白组合模式

来源: 责编: 时间:2024-05-09 09:20:49 223观看
导读本篇讲解Java设计模式中的组合模式,分为定义、模式应用前案例、结构、模式应用后案例、适用场景、模式可能存在的困惑和本质探讨7个部分。定义组合模式是将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式

本篇讲解Java设计模式中的组合模式,分为定义、模式应用前案例、结构、模式应用后案例、适用场景、模式可能存在的困惑和本质探讨7个部分。cV728资讯网——每日最新资讯28at.com

定义

组合模式是将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得客户对单个对象和复合对象的使用具有一致性。cV728资讯网——每日最新资讯28at.com

在新的分类方式中,组合模式被划分至类之间的交互类别中,其简化的是调用方与具备树结构的一组对象之间的交互,具体通过一致性的行为实现cV728资讯网——每日最新资讯28at.com

cV728资讯网——每日最新资讯28at.com

模式应用前案例

下面以一个典型的文件和目录为例来进行说明,先来看一下未应用组合模式之前的代码实现。cV728资讯网——每日最新资讯28at.com

public class File {//文件结构    private final String name;    public File(String name) {        this.name = name;    }    public void display() {        System.out.println("File: " + this.name);    }}public class Directory {//目录结构    private String name;    private final List<File> files;    private final List<Directory> directories;    // 初始化方法    public Directory(String name){        this.name = this.name;        this.files = new ArrayList<>();        this.directories = new ArrayList<>();    }    // 添加子节点    public void addFile(File file){        this.files.add(file);    }    // 添加子目录    public void addDirectory(Directory directory) {        this.directories.add(directory);    }    public void display(){        //System.out.println("Directory:"+this.name);        for(File file : this.files){            file.display();        }        for (Directory dir : this.directories) {            dir.display();        }    }}public class Client {//调用方代码    public static void main(String[] ars){        Directory root= new Directory("Root");        File file1=new File("file1.txt");        File file2=new File("file2.txt");        root.addFile(file1);        root.addFile(file2);        Directory subDirecory =new Directory ("Subdirectory");        File file3 = new File("file3.tx");        File file4 = new File("file4.tx");        subDirecory.addFile(file3);        subDirecory.addFile(file4);        root.addDirectory(subDirecory);        root.display();    }}

我们知道,文件和目录两者是一个大的树结构中的节点。在上面未使用组合模式的代码中,文件和目录都有自己定义的方法。这样在构建一个多层树结构的过程中,复杂度会提升。cV728资讯网——每日最新资讯28at.com

结构

cV728资讯网——每日最新资讯28at.com

组合模式的示例代码如下。cV728资讯网——每日最新资讯28at.com

public interface Component {    void operation();    void add(Component component);    void remove(Component component);    Component Display(int index);}public class Leaf implements Component{    private String name;    public Leaf(String name) {        this.name = name;    }    @Override    public void operation() {        System.out.println("Leaf: " + name + " operation()");    }    @Override    public void add(Component component) {        throw new UnsupportedOperationException("Leaf cannot have children");    }    @Override    public void remove(Component component) {        throw new UnsupportedOperationException("Leaf cannot remove children");    }    @Override    public Component Display(int index) {        throw new UnsupportedOperationException("Leaf cannot get child");    }}public interface Component {    void operation();    void add(Component component);    void remove(Component component);    Component Display(int index);}public class Client {    public static void main(String[] args) {        // 创建叶子节点        Component leaf1 = new Leaf("LeafA");        Component leaf2 = new Leaf("LeafB");        Component leaf3 = new Leaf("LeafC");        // 创建复合节点        Component composite = new Composite("CompositeX");        composite.add(leaf1);        composite.add(leaf2);        // 创建另一个复合节点,并添加之前的复合节点和新的叶子节点        Component root = new Composite("Root");        root.add(composite);        root.add(leaf3);        // 执行操作        root.operation();    }}

模式应用后案例

上面文件与目录的案例,使用组合模式之后的代码实现如下。cV728资讯网——每日最新资讯28at.com

public interface IComponent {//接口    void display();}public class File implements IComponent{//文件实现    private final String name;    public File(String name) {        this.name = name;    }    @Override    public void display() {        System.out.println("File: " + this.name);    }}public class Directory implements IComponent{//目录实现    private String name;    private final List<IComponent> children;    // 初始化方法    public Directory(String name){        this.name = this.name;        this.children = new ArrayList<>();    }    // 添加子节点    public void addComponent(IComponent component){        this.children.add(component);    }    // 显示目录内容    @Override    public void display() {       //System.out.println("Directory: " + this.name);        for (IComponent child : this.children) {            child.display();        }    }}public class Client {//调用方代码    public static void main(String[] ars){        Directory root= new Directory("Root");        File file1 = new File("file1.txt");        File file2 = new File ("file2.txt");        root.addComponent(file1);        root.addComponent(file2);        Directory subDirectory =new Directory ("Subdirectory");        File file3 = new File("file3.txt");        File file4 = new File("file4.txt");        subDirectory.addComponent(file3);        subDirectory.addComponent(file4);        root.addComponent(subDirectory);        root.display();    }}

在上述代码中,由于树的结构使用一个接口和实现的家族来实现,这样树的结构中所有类的行为都是一致的,简化了编码时的复杂度。cV728资讯网——每日最新资讯28at.com

适用场景

当需求中出现的一系列概念或对象,它们之间存在部分-整体的层次结构或共同构成一颗树的结构时,就可以考虑使用组合模式。cV728资讯网——每日最新资讯28at.com

模式可能存在的困惑

困惑1:组合模式中的“组合”,与“组合优于继承”中的“组合”,有什么关联?cV728资讯网——每日最新资讯28at.com

两者都代表了一种关系。前者的“组合”指的是将一系列对象按照层次化结构进行组织。而后者的“组合”指的是两个对象之间的聚合或组合关系,以此来取代类之间继承关系。cV728资讯网——每日最新资讯28at.com

本质

组合模式的本质在于提供了一种机制来处理对象之间的部分-整体关系,并且通过统一接口来简化调用方使用复杂层次结构时可能遇到的问题。cV728资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-87479-0.html一文彻底搞明白组合模式

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 大营销抽奖系统,DDD开发要如何建模?

下一篇: 开发者对 React 19 Beta 发布感到困惑

标签:
  • 热门焦点
  • 鸿蒙OS 4.0公测机型公布:甚至连nova6都支持

    华为全新的HarmonyOS 4.0操作系统将于今天下午正式登场,官方在发布会之前也已经正式给出了可升级的机型产品,这意味着这些机型会率先支持升级享用。这次的HarmonyOS 4.0支持
  • K60 Pro官方停产 第三方瞬间涨价

    虽然没有官方宣布,但Redmi的一些高管也已经透露了,Redmi K60 Pro已经停产且不会补货,这一切都是为了即将到来的K60 Ultra铺路,属于厂家的正常操作。但有意思的是该机在停产之后
  • 对标苹果的灵动岛 华为带来实况窗功能

    继苹果的灵动岛之后,华为也在今天正式推出了“实况窗”功能。据今天鸿蒙OS 4.0的现场演示显示,华为的实况窗可以更高效的展现出实时通知,比如锁屏上就能看到外卖、打车、银行
  • JavaScript 混淆及反混淆代码工具

    介绍在我们开始学习反混淆之前,我们首先要了解一下代码混淆。如果不了解代码是如何混淆的,我们可能无法成功对代码进行反混淆,尤其是使用自定义混淆器对其进行混淆时。什么是混
  • 腾讯盖楼,字节拆墙

    来源 | 光子星球撰文 | 吴坤谚编辑 | 吴先之&ldquo;想重温暴刷深渊、30+技能搭配暴搓到爽的游戏体验吗?一起上晶核,即刻暴打!&rdquo;曾凭借直播腾讯旗下代理格斗游戏《DNF》一
  • iQOO 11S评测:行业唯一的200W标准版旗舰

    【Techweb评测】去年底,iQOO推出了“电竞旗舰”iQOO 11系列,作为一款性能强机,该机不仅全球首发2K 144Hz E6全感屏,搭载了第二代骁龙8平台及144Hz电竞
  • iQOO 11S屏幕细节公布:首发三星2K E6全感屏 安卓最好的直屏手机

    日前iQOO手机官方宣布,新一代电竞旗舰iQOO 11S将会在7月4日19:00正式与大家见面。随着发布时间的日益临近,官方关于该机的预热也更加密集,截至目前已
  • 首发天玑9200+ iQOO Neo8系列发布首销售价2299元起

    2023年5月23日晚,iQOO Neo8系列正式发布。其中,Neo系列首款Pro之作——iQOO Neo8 Pro强悍登场,限时售价3099元起;价位段最强性能手机iQOO Neo8同期上市
  • Meta盲目扩张致超万人被裁,重金押注元宇宙而前景未明

    图片来源:图虫创意日前,Meta创始人兼CEO 马克&middot;扎克伯发布公开信,宣布Meta计划裁员超11000人,占其员工总数13%。他公开承认了自己的预判失误:&ldquo;不仅
Top