最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java設計模式之組合模式深入刨析

 更新時間:2022年05月16日 09:58:22   作者:行萬里路,讀萬卷書  
組合模式,又叫部分整體模式,它創(chuàng)建了對象組的數(shù)據(jù)結構組合模式使得用戶對單個對象和組合對象的訪問具有一致性。本文將通過示例為大家詳細介紹一下組合模式,需要的可以參考一下

1.基本介紹

1)組合模式(Composite Pattern),又叫部分整體模式,它創(chuàng)建了對象組的樹形結構,將對象組合成樹狀結構以表示“整體-部分”的層次關系

2)組合模式依據(jù)樹形結構來組合對象,用來表示部分以及整體層次

3)這種類型的設計模式屬于結構型模式

4)組合模式使得用戶對單個對象和組合對象的訪問具有一致性,即:組合能讓客戶以一致的方式處理個別對象以及組合對象

2.結構

組合模式主要包含三種角色:

  • 抽象根節(jié)點(Component):定義系統(tǒng)各層次對象的共有方法和屬性,可以預定義一些默認行為和屬性
  • 樹枝節(jié)點(Composite):定義樹枝節(jié)點的行為,存儲子節(jié)點,組合樹枝節(jié)點和葉子節(jié)點形成一個樹形結構
  • 葉子節(jié)點(Leaf):葉子節(jié)點對象,其下再無分支,是系統(tǒng)層次遍歷的最小單位

3.組合模式解決的問題

1)組合模式解決這樣的問題,當我們要處理的對象可以生成一顆樹形結構,而我們要對樹上的節(jié)點和葉子進行操作時,它能夠提供一致的方式,而不用考慮它是節(jié)點還是葉子

2)對應的示意圖

4.組合模式解決學校院系展示

1)應用實例要求

編寫程序展示一個學校院系結構:需求是這樣的,要在一個頁面中展示出學校的院系組成,一個學校有多個學院,一個學院有多個系

2)思路分析和圖解(類圖)

3)代碼實現(xiàn)

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) {
        // 默認實現(xiàn)
        throw new UnsupportedOperationException();
    }
    protected void remove(OrganizationComponent organizationComponent) {
        // 默認實現(xiàn)
        throw new UnsupportedOperationException();
    }
    // 構造器
    public OrganizationComponent(String name, String des) {
        this.name = name;
        this.des = des;
    }
    // 方法print,抽象方法
    protected abstract void print();
}

Composite非葉子節(jié)點

package com.zte;
import java.util.ArrayList;
import java.util.List;
// University 就是 Composite,可以管理College
public 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非葉子節(jié)點

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葉子節(jié)點

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) {
        // 創(chuàng)建大學
        OrganizationComponent university = new University("清華大學", "中國最好的大學");
        // 創(chuàng)建學院
        OrganizationComponent college1 = new College("計算機學院", "計算機學院");
        OrganizationComponent college2 = new College("信息工程學院", "信息工程學院");
        // 創(chuàng)建各個學院下面的系
        college1.add(new Department("軟件工程", "軟件工程"));
        college1.add(new Department("網絡工程", "網絡工程"));
        college1.add(new Department("計算機科學與技術", "老牌專業(yè)"));
        college2.add(new Department("通信工程", "通信工程"));
        college2.add(new Department("信息工程", "信息工程"));
        // 將學院添加到學校中
        university.add(college1);
        university.add(college2);
        // 打印大學底下的所有院系
        university.print();
        // 打印學院底下的所有系
        college1.print();
    }
}

5.組合模式的注意事項和細節(jié)

1)簡化客戶端操作,客戶端只需要面對一致的對象而不用考慮整體部分或者節(jié)點葉子的問題

2)其有較強的擴展性,當我們需要修改組合對象時,我們只需要調整內部的層次關系,客戶端不用做出任何改動

3)方便創(chuàng)建出復雜的層次結構,客戶端不用理會組合里面的組成細節(jié),容易添加節(jié)點或者葉子從而創(chuàng)建出復雜的樹形結構

4)需要遍歷組織機構,或者處理的對象具有樹形結構時,非常適合使用組合模式

5)要求較高的抽象性,如果節(jié)點和葉子有很多差異性的話,比如很多方法和屬性都不一樣,不適合使用組合模式

到此這篇關于Java設計模式之組合模式深入刨析的文章就介紹到這了,更多相關Java組合模式內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java中Comparator升序降序的具體使用

    Java中Comparator升序降序的具體使用

    本文主要介紹了Java中Comparator升序降序的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • java理論基礎函數(shù)式接口特點示例解析

    java理論基礎函數(shù)式接口特點示例解析

    這篇文章主要為大家介紹了java理論基礎函數(shù)式接口特點示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • 關于RedisTemplate常用方法封裝

    關于RedisTemplate常用方法封裝

    文章總結了RedisTemplate的常用方法封裝,包括序列化配置、基礎常量和接口實現(xiàn),這些內容是個人經驗分享,旨在為讀者提供參考和支持
    2024-12-12
  • Java之進程和線程的區(qū)別

    Java之進程和線程的區(qū)別

    這篇文章主要介紹了進程與線程的區(qū)別,線程具有許多傳統(tǒng)進程所具有的特征,而把傳統(tǒng)的進程稱為重型進程(Heavy—Weight Process),它相當于只有一個線程的任務,有感興趣的小伙伴可以參考閱讀本文
    2023-03-03
  • SpringMVC 攔截器的使用示例

    SpringMVC 攔截器的使用示例

    這篇文章主要介紹了SpringMVC 攔截器的使用示例,幫助大家更好的理解和學習使用SpringMVC,感興趣的朋友可以了解下
    2021-04-04
  • mybatisPlus 實體類與數(shù)據(jù)庫表映射關系詳解

    mybatisPlus 實體類與數(shù)據(jù)庫表映射關系詳解

    這篇文章主要介紹了mybatisPlus 實體類與數(shù)據(jù)庫表映射關系詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • Java中Maven項目導出jar包配置的示例代碼

    Java中Maven項目導出jar包配置的示例代碼

    這篇文章主要介紹了Java中Maven項目導出jar包配置的示例代碼,需要的朋友可以參考下
    2018-11-11
  • 一文帶你剖析Redisson分布式鎖的原理

    一文帶你剖析Redisson分布式鎖的原理

    相信使用過redis的,或者正在做分布式開發(fā)的童鞋都知道redisson組件,它的功能很多,但我們使用最頻繁的應該還是它的分布式鎖功能,少量的代碼,卻實現(xiàn)了加鎖、鎖續(xù)命(看門狗)、鎖訂閱、解鎖、鎖等待(自旋)等功能,我們來看看都是如何實現(xiàn)的
    2022-11-11
  • SpringBoot Mybatis 配置文件形式詳解

    SpringBoot Mybatis 配置文件形式詳解

    這篇文章主要介紹了SpringBoot Mybatis 配置文件形式詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Java8新特性 StreamAPI實例詳解

    Java8新特性 StreamAPI實例詳解

    這篇文章主要為大家介紹了Java8新特性 StreamAPI實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論

迁安市| 荆门市| 永福县| 成安县| 中方县| 津市市| 汉中市| 山阳县| 黄骅市| 秀山| 麻栗坡县| 财经| 霍林郭勒市| 和平区| 岐山县| 芦山县| 南木林县| 射阳县| 盘山县| 丹阳市| 深水埗区| 娄烦县| 龙里县| 瑞安市| 财经| 株洲县| 东兴市| 云霄县| 丰原市| 略阳县| 宁安市| 湖南省| 定襄县| 万山特区| 巨鹿县| 色达县| 元氏县| 康定县| 镇原县| 乡宁县| 徐汇区|