輕松掌握java組合模式
組合模式,將對象組合成樹形結構以表示“部分-整體”的層次結構,組合模式使得用戶對單個對象和組合對象的使用具有一致性,組合模式可以讓客戶端像修改配置文件一樣簡單的完成本來需要流程控制語句來完成的功能。
特點:對于遞歸或者類似樹形的分級數(shù)據(jù)結構,可以用最簡單的方式進行處理。
企業(yè)級開發(fā)和常用框架中的應用:系統(tǒng)目錄結構和網(wǎng)站導航結構
下面以目錄結構舉例:
場景:假設我們現(xiàn)在有一個目錄,目錄下面還有子目錄和文件,現(xiàn)在我們要查看整個目錄及目錄下的所有文件和創(chuàng)建時間
具體代碼如下:
package com.test.composite; import java.util.ArrayList; import java.util.Date; import java.util.List; public class Demo { public static void main(String[] args) { Date d = new Date(); Dir f1 = new Dir("我的收藏", d); d.setYear(2012); Dir f2 = new Dir("圖片", d); Dir f3 = new Dir("音樂", d); d.setYear(2013); ActualFile f4 = new ActualFile("喜洋洋與灰太狼.avi", d); f1.add(f4); ActualFile f5 = new ActualFile("taiyanghua.jpg", d); ActualFile f6 = new ActualFile("變形精鋼.jpg", d); f2.add(f5); f2.add(f6); f1.add(f2); f1.add(f3); f1.showFile(); } } /** * 首先目錄和文件都屬于文件,所以我們可以抽象一個抽象文件出來 */ interface AbstractFile { /** * 展示文件方法 */ public void showFile(); } /** * 真實文件 */ class ActualFile implements AbstractFile { private String name; private Date createDate; public ActualFile(String name, Date createDate) { this.name = name; this.createDate = createDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } /** * 實現(xiàn)抽象文件類的展示文件方法 */ public void showFile() { System.out.println("文件名:"+this.name+"--創(chuàng)建時間:"+this.createDate.getTime()); } } /** * 目錄文件 */ class Dir implements AbstractFile { private String name; private Date createDate; /** * 作為目錄文件,會多出一個子文件列表 */ private List<AbstractFile> list = new ArrayList<>(); public Dir(String name, Date createDate) { super(); this.name = name; this.createDate = createDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } /** * 目錄文件的添加操作,為目錄添加子文件或者子目錄 */ public void add(AbstractFile f){ this.list.add(f); } /** * 目錄文件的刪除操作,刪除子文件或者子目錄 */ public void remove(AbstractFile f){ this.list.remove(f); } /** * 目錄文件的獲取操作,獲取目錄下面的子文件或者子目錄 */ public AbstractFile getIndex(int index){ return this.list.get(index); } public void showFile() { System.out.println("目錄名:"+this.name+"--創(chuàng)建時間:"+this.createDate.getTime()); for(AbstractFile f:list){ f.showFile(); } } }
組合模式更像是一種遍歷手段,但是這種手段也有一些限制,比如只能針對類似于樹形結構的數(shù)據(jù)。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Kafka多節(jié)點分布式集群搭建實現(xiàn)過程詳解
這篇文章主要介紹了Kafka多節(jié)點分布式集群搭建實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11Springboot項目中單元測試時注入bean失敗的解決方案
這篇文章主要介紹了Springboot項目中單元測試時注入bean失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11AJAX Servlet實現(xiàn)數(shù)據(jù)異步交互的方法
本篇文章主要介紹了AJAX Servlet實現(xiàn)數(shù)據(jù)異步交互的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07Mybatis?Plus?新版lambda?表達式查詢異常的處理
這篇文章主要介紹了Mybatis?Plus?新版lambda?表達式查詢異常的處理方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01簡單談談ThreadPoolExecutor線程池之submit方法
下面小編就為大家?guī)硪黄唵握務凾hreadPoolExecutor線程池之submit方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06