溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java設(shè)計(jì)模式之組合模式實(shí)例分析

發(fā)布時(shí)間:2022-05-16 11:37:08 來源:億速云 閱讀:193 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Java設(shè)計(jì)模式之組合模式實(shí)例分析”,在日常操作中,相信很多人在Java設(shè)計(jì)模式之組合模式實(shí)例分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java設(shè)計(jì)模式之組合模式實(shí)例分析”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

1.基本介紹

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

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

3)這種類型的設(shè)計(jì)模式屬于結(jié)構(gòu)型模式

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

2.結(jié)構(gòu)

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

  • 抽象根節(jié)點(diǎn)(Component):定義系統(tǒng)各層次對象的共有方法和屬性,可以預(yù)定義一些默認(rèn)行為和屬性

  • 樹枝節(jié)點(diǎn)(Composite):定義樹枝節(jié)點(diǎn)的行為,存儲(chǔ)子節(jié)點(diǎn),組合樹枝節(jié)點(diǎn)和葉子節(jié)點(diǎn)形成一個(gè)樹形結(jié)構(gòu)

  • 葉子節(jié)點(diǎn)(Leaf):葉子節(jié)點(diǎn)對象,其下再無分支,是系統(tǒng)層次遍歷的最小單位

3.組合模式解決的問題

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

2)對應(yīng)的示意圖

Java設(shè)計(jì)模式之組合模式實(shí)例分析

4.組合模式解決學(xué)校院系展示

1)應(yīng)用實(shí)例要求

編寫程序展示一個(gè)學(xué)校院系結(jié)構(gòu):需求是這樣的,要在一個(gè)頁面中展示出學(xué)校的院系組成,一個(gè)學(xué)校有多個(gè)學(xué)院,一個(gè)學(xué)院有多個(gè)系

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

Java設(shè)計(jì)模式之組合模式實(shí)例分析

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

Composite非葉子節(jié)點(diǎn)

package com.zte;
import java.util.ArrayList;
import java.util.List;
// University 就是 Composite,可以管理College
public class University extends OrganizationComponent {
    List<OrganizationComponent> organizationComponentList = new ArrayList<>();
    // 構(gòu)造器
    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é)點(diǎn)

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é)點(diǎn)

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

5.組合模式的注意事項(xiàng)和細(xì)節(jié)

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

2)其有較強(qiáng)的擴(kuò)展性,當(dāng)我們需要修改組合對象時(shí),我們只需要調(diào)整內(nèi)部的層次關(guān)系,客戶端不用做出任何改動(dòng)

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

4)需要遍歷組織機(jī)構(gòu),或者處理的對象具有樹形結(jié)構(gòu)時(shí),非常適合使用組合模式

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

到此,關(guān)于“Java設(shè)計(jì)模式之組合模式實(shí)例分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI