通用樹形結構的迭代與組合模式實現方案

来源:https://www.cnblogs.com/wanglifeng717/archive/2022/06/11/16363485.html
-Advertisement-
Play Games

日常開發過程過程中。樹形結構運用的非常頻繁。 例如:公司組織結構、各種分類結構、分組結構等等。 SET FOREIGN_KEY_CHECKS = 0; CREATE TABLE IF NOT EXISTS `tbl_sapo_group` ( `id` int(10) unsigned NOT NU ...


日常開發過程過程中。樹形結構運用的非常頻繁。

例如:公司組織結構、各種分類結構、分組結構等等。

 

 

 

 

SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE IF NOT EXISTS `tbl_sapo_group` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `code` varchar(100) NOT NULL COMMENT '唯一編碼',
  `create_time` datetime(3) NOT NULL COMMENT '創建時間',
  `last_update_time` datetime(3) DEFAULT NULL COMMENT '最後更新時間',
  `name` varchar(255) NOT NULL COMMENT '名稱',
  `detail` varchar(255) DEFAULT NULL COMMENT '詳情',
  `status` int(10) unsigned NOT NULL DEFAULT 2 COMMENT '狀態:0-無效,1-有效,2-編輯',
  `group_type` varchar(100) NOT NULL COMMENT '組類型',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_group_code` (`code`),
  KEY `idx_group_group_type` (`group_type`),
  CONSTRAINT `fk_group_group_type` FOREIGN KEY (`group_type`) REFERENCES `tbl_sapo_group_type` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='';

CREATE TABLE IF NOT EXISTS `tbl_sapo_group_rel` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `create_time` datetime(3) NOT NULL COMMENT '創建時間',
  `last_update_time` datetime(3) DEFAULT NULL COMMENT '最後更新時間',
  `parent_code` varchar(100) NOT NULL COMMENT '父節點代碼,tbl_sapo_group表code',
  `child_code` varchar(100) NOT NULL COMMENT '子節點代碼,tbl_sapo_group表code',
  `status` int(10) unsigned NOT NULL DEFAULT 2 COMMENT '狀態:0-無效,1-有效,2-編輯',
  `group_rel_type` varchar(100) NOT NULL COMMENT '組關係類型代碼,來自tbl_sapo_group_rel_type表code',
  `tree_code` varchar(100) NOT NULL COMMENT '樹節點代碼,tbl_sapo_tree表code',
  PRIMARY KEY (`id`),
  KEY `idx_group_rel_child_code` (`child_code`),
  KEY `idx_group_rel_parent_code` (`parent_code`),
  KEY `idx_group_rel_group_rel_type` (`group_rel_type`),
  KEY `idx_group_rel_tree_code_status_parent_code_child_code` (`tree_code`,`status`,`parent_code`,`child_code`),
  CONSTRAINT `fk_group_rel_child_code` FOREIGN KEY (`child_code`) REFERENCES `tbl_sapo_group` (`code`),
  CONSTRAINT `fk_group_rel_group_rel_type` FOREIGN KEY (`group_rel_type`) REFERENCES `tbl_sapo_group_rel_type` (`code`),
  CONSTRAINT `fk_group_rel_parent_code` FOREIGN KEY (`parent_code`) REFERENCES `tbl_sapo_group` (`code`),
  CONSTRAINT `fk_group_rel_tree_code` FOREIGN KEY (`tree_code`) REFERENCES `tbl_sapo_tree` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='組關係';

CREATE TABLE IF NOT EXISTS `tbl_sapo_group_rel_type` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `code` varchar(100) NOT NULL COMMENT '唯一編碼',
  `create_time` datetime(3) NOT NULL COMMENT '創建時間',
  `last_update_time` datetime(3) DEFAULT NULL COMMENT '最後更新時間',
  `name` varchar(255) NOT NULL COMMENT '名稱',
  `detail` varchar(255) DEFAULT NULL COMMENT '詳情',
  `status` int(10) unsigned NOT NULL DEFAULT 2 COMMENT '狀態:0-無效,1-有效,2-編輯',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_group_rel_type_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='組關係類型';

CREATE TABLE IF NOT EXISTS `tbl_sapo_group_type` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `code` varchar(100) NOT NULL COMMENT '唯一編碼',
  `create_time` datetime(3) NOT NULL COMMENT '創建時間',
  `last_update_time` datetime(3) DEFAULT NULL COMMENT '最後更新時間',
  `name` varchar(255) NOT NULL COMMENT '名稱',
  `detail` varchar(255) DEFAULT NULL COMMENT '詳情',
  `status` int(10) unsigned NOT NULL DEFAULT 2 COMMENT '狀態:0-無效,1-有效,2-編輯',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_group_type_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='組類型';

CREATE TABLE IF NOT EXISTS `tbl_sapo_tree` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `code` varchar(100) NOT NULL COMMENT '唯一編碼',
  `create_time` datetime(3) NOT NULL COMMENT '創建時間',
  `last_update_time` datetime(3) DEFAULT NULL COMMENT '最後更新時間',
  `name` varchar(255) NOT NULL COMMENT '名稱',
  `detail` varchar(255) DEFAULT NULL COMMENT '詳情',
  `status` int(10) unsigned NOT NULL DEFAULT 2 COMMENT '狀態:0-無效,1-有效,2-編輯',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_tree_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='樹定義';

CREATE TABLE IF NOT EXISTS `tbl_sapo_tree_group` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `create_time` datetime(3) NOT NULL COMMENT '創建時間',
  `last_update_time` datetime(3) DEFAULT NULL COMMENT '最後更新時間',
  `group_code` varchar(100) NOT NULL COMMENT '組代碼,tbl_sapo_group表code',
  `tree_code` varchar(100) NOT NULL COMMENT '樹代碼,tbl_sapo_tree表code',
  `status` int(10) unsigned NOT NULL DEFAULT 2 COMMENT '狀態:0-無效,1-有效,2-編輯',
  `is_root` int(10) unsigned DEFAULT NULL COMMENT '是否根節點:1-根節點,null非根節點',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uni_idx_tree_group_tree_code_is_root` (`tree_code`,`is_root`),
  KEY `idx_tree_group_group_code` (`group_code`),
  CONSTRAINT `fk_tree_group_group_code` FOREIGN KEY (`group_code`) REFERENCES `tbl_sapo_group` (`code`),
  CONSTRAINT `fk_tree_group_tree_code` FOREIGN KEY (`tree_code`) REFERENCES `tbl_sapo_tree` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='樹包含的組';

SET FOREIGN_KEY_CHECKS = 1;
建表語句

如圖所示關係型資料庫模型,基本滿足一個系統多顆樹、組可以復用的目的。

 

樹的節點可能是一個單獨的節點,也可能是一個子樹的根。我們需要遍歷的時候需要不同節點不同處理,使用【多態】。

但是處理的時候不要區分是何種節點,提供一種【透明化】處理方式,要實現需要引用兩個模式:迭代模式、組合模式

老規矩,先引入概念,之後實現。

迭代器模式 提供一個方式來遍歷集合而無需暴露集合的實現
組合模式 客戶可以將對象的集合以及個別對象一視同仁

 

迭代器模式:

 

 

迭代器示例:數組實現迭代器

 

 

// 迭代器介面
interface Iterator {
    boolean hasNext();

    Object next();
}

// 菜單項
class MenuItem {

    String name;
    String description;
    boolean vegetarian;
    double price;

    public MenuItem(String name, String description, boolean vegetarian, double price) {
        this.name = name;
        this.description = description;
        this.vegetarian = vegetarian;
        this.price = price;
    }
    // getter,setter方法
    public String getName() {
        return name;
    }
}

// 菜單
class DinerMenu {
    static final int MAX_ITEMS = 6;
    int numberOfItems = 0;
    MenuItem[] menuItems;

    public DinerMenu() {
        menuItems = new MenuItem[MAX_ITEMS];
        addItem("紅燒獅子頭", "江南名菜", true, 50d);
        addItem("夫妻肺片", "和夫妻沒啥關係", true, 70d);
    }

    public void addItem(String name, String description, boolean vegetarian, double price) {
        MenuItem menuItem = new MenuItem(name, description, vegetarian, price);
        if (numberOfItems >= MAX_ITEMS) {
            System.out.println("sorry,menu is full");
        } else {
            menuItems[numberOfItems] = menuItem;
            numberOfItems += 1;
        }
    }

    public MenuItem[] getMenuItems() {
        return menuItems;
    }

    public Iterator createIteator() {
        return new DinerMenuIterator(menuItems);
    }
}

class DinerMenuIterator implements Iterator {
    MenuItem[] items;
    int position = 0;

    public DinerMenuIterator(MenuItem[] items) {
        this.items = items;
    }

    public Object next() {
        MenuItem menuItem = items[position];
        position = position + 1;
        return menuItem;
    }

    public boolean hasNext() {
        // 數組可能沒裝滿
        if (position >= items.length || items[position] == null) {
            return false;
        } else {
            return true;
        }
    }

    public void remove() {
        if (position <= 0) {
            throw new IllegalStateException("you can't an item unitl you've done at least on next()");
        }
        if (items[position - 1] != null) {
            for (int i = position - 1; i < (items.length - 1); i++) {
                items[i] = items[i + 1];
            }
            items[items.length - 1] = null;
        }

    }

}

// 測試
class Test {
    public static void main(String[] args) {
        Iterator iterator = (new DinerMenu()).createIteator();
        while(iterator.hasNext()){
            MenuItem menuItem = (MenuItem) iterator.next();
            System.out.println(menuItem.getName());
        }

    }
}
迭代器模式示例
數組迭代器

1.當然remove可以不實現,因為可能併發remove,迭代器不安全。

我們簡單處理拋出java.lang.UnsupportedOperationException

2.java5之後,集合可以使用for/in形式代替了顯示的創建迭代器。

for( Object obj: collection){

}

 

對於不同的集合,我們有不同的遍歷方式。有沒有一種通用的遍歷集合的模式,屏蔽這種差異,該模式就是迭代器。

迭代器模式提供一種方法順序訪問一個聚合對象中的各個元素,而不暴露其內部的表示。

其實說白了,迭代器模式就是通過定義統一操作介面,來屏蔽不同底層的操作邏輯。

如果你能有一個統一的方法訪問聚合中的每一個對象,你就可以編寫多態的代碼和這些聚合搭配

把游走的任務放在迭代器上,而不是聚合上。這樣簡化了聚合的介面和實現。責任分配明晰。

符合【單一職責】,如果不使用迭代器模式,集合改變的話,例如由集合變數組,這個類必須改變,遍歷方式也跟著改變。

 

組合模式:

允許你將對象組合成樹形結構來表現“整體/部分”層次結構。

組合能讓客戶以一致的方式處理個別對象以及對象組合。即我們可以忽略對象組合和個別對象之間的差別,而使用相同操作。

組合模式犧牲【單一責任】獲取【透明性】,透明性即客戶處理組合和葉節點一視同仁。一個節點是組合還是葉節點,對客戶是透明的。

 

 

 

 

組合模式示例:

 

 

 

 

public abstract class MenuComponent {
   
    // 操作節點需要方法
    public void add(MenuComponent menuComponent) {
        throw new UnsupportedOperationException();
    }
    public void remove(MenuComponent menuComponent) {
        throw new UnsupportedOperationException();
    }
    public MenuComponent getChild(int i) {
        throw new UnsupportedOperationException();
    }
  
    // 菜單本身方法
    public String getName() {
        throw new UnsupportedOperationException();
    }
    public String getDescription() {
        throw new UnsupportedOperationException();
    }
    public double getPrice() {
        throw new UnsupportedOperationException();
    }
    public boolean isVegetarian() {
        throw new UnsupportedOperationException();
    }
  
    public void print() {
        throw new UnsupportedOperationException();
    }
}
MenuComponent
public class Menu extends MenuComponent {
    ArrayList<MenuComponent> menuComponents = new ArrayList<MenuComponent>();
    String name;
    String description;

    public Menu(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public void add(MenuComponent menuComponent) {
        menuComponents.add(menuComponent);
    }

    public void remove(MenuComponent menuComponent) {
        menuComponents.remove(menuComponent);
    }

    public MenuComponent getChild(int i) {
        return (MenuComponent) menuComponents.get(i);
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public void print() {
        System.out.print("\n" + getName());
        System.out.println(", " + getDescription());
        System.out.println("---------------------");

        Iterator<MenuComponent> iterator = menuComponents.iterator();
        while (iterator.hasNext()) {
            MenuComponent menuComponent = (MenuComponent) iterator.next();
            menuComponent.print();
        }
    }
}
Menu
public class MenuItem extends MenuComponent {
    String name;
    String description;
    boolean vegetarian;
    double price;
    
    public MenuItem(String name, 
                    String description, 
                    boolean vegetarian, 
                    double price) 
    { 
        this.name = name;
        this.description = description;
        this.vegetarian = vegetarian;
        this.price = price;
    }
  
    public String getName() {
        return name;
    }
  
    public String getDescription() {
        return description;
    }
  
    public double getPrice() {
        return price;
    }
  
    public boolean isVegetarian() {
        return vegetarian;
    }
  
    public void print() {
        System.out.print("  " + getName());
        if (isVegetarian()) {
            System.out.print("(v)");
        }
        System.out.println(", " + getPrice());
        System.out.println("     -- " + getDescription());
    }
}
MenuItem
public class Waitress {
    MenuComponent allMenus;
 
    public Waitress(MenuComponent allMenus) {
        this.allMenus = allMenus;
    }
 
    public void printMenu() {
        allMenus.print();
    }
}
Waitress

 

 示例:

使用迭代和組合模式實現一種通用的樹形結構:

1.核心及組和組的關係。

2.該方案實現了,內部迭代器和外部迭代器。根據實際情況使用。

 

 

 

 

 

public abstract class GroupComponent {

   

    public abstract Iterator<GroupComponent> createIterator();
    
    // 首行字元空幾格
    protected abstract String printTreeStr(int i);
    
    public abstract String getName();
    
    public  String printTreeStr() {
        return printTreeStr(0);
    };
    
    
    
    // 列印樹形解結構
    protected String padding_n(int n) {
        StringBuffer space = new StringBuffer("");
        for (int i = 0; i < n; i++) {
            space.append("-");
        }
        space.append("|");
        return space.toString();
    }
    
 // 遞歸獲取樹形結構
    public static GroupComponent getTree(String groupCode) {
        // 獲取通用dao
        CommonDao dao = SpringUtil.getBean(CommonDao.class);
        // 資料庫中組詳細信息model類
        SapoGroup sapoGroup = dao.getObj(SapoGroup.getInstance().setCode(groupCode));

        // 查詢該節點所有兒子
        List<SapoGroupRel> childList = dao.getObjListWithEmpty(SapoGroupRel.getInstance().setParentCode(groupCode));

        // 如果沒有子節點,直接新建葉子節點返回
        if (childList == null || childList.size() == 0) {
            LeafGroup leafGroup = new LeafGroup();
            leafGroup.setLeafGroup(sapoGroup);
            return leafGroup;
        } else {
            // 如果有子節點
            Group group = new Group();
            group.setGroupDetail(sapoGroup);
            for (SapoGroupRel rel : childList) {
                // 遞歸拿到上一個節點
                GroupComponent child = getTree(rel.getChildCode());
                group.getList().add(child);
            }
            return group;
        }
    }
}
GroupComponent
public class Group extends GroupComponent {

    Iterator<GroupComponent> iterator = null;

    public List<GroupComponent> list = new ArrayList<GroupComponent>();

    public SapoGroup groupDetail;

    public SapoGroup getGroupDetail() {
        return groupDetail;
    }

    public void setGroupDetail(SapoGroup groupDetail) {
        this.groupDetail = groupDetail;
    }

    /*
     * 列印樹形層級結構
     */
    protected String printTreeStr(int i) {
        // 需要列印的欄位
        String waitPrintStr = 	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • SQL的語法 SQL通用語法 SQL語句可以單行或多行書寫,以分號(“;”)結尾。 SQL語句可以使用空格或縮進增強可讀性。 MySQL資料庫的SQL語句不區分大小寫(建議關鍵字大寫)。 註釋 單行註釋: -- 內容 # 內容(MySQL特有) 多行註釋: /* 內容 */ SQL語句分類 分類 全 ...
  • MyBatisPlus(簡稱MP)是基於MyBatis框架基礎上開發的增強型工具,旨在簡化開發、提高效率 - 無侵入:只做增強不做改變,不會對現有工程產生影響 - 強大的 CRUD 操作:內置通用 Mapper,少量配置即可實現單表CRUD 操作 - 支持 Lambda:編寫查詢條件無需擔心欄位寫錯... ...
  • 分享嘉賓:羅景 58同城 高級架構師 編輯整理:洪鵬飛 內容來源:DataFun AI Talk《連接效率優化實踐》 出品社區:DataFun **導讀:**本次分享由以下幾個部分構成—— 58的業務背景 綜合排序框架 效率優化框架 基礎數據流程(數據) 策略優化路徑(演算法) 效率優化平臺(工程) ...
  • 1、簡介 註:本文代碼參考 我們將開發本系列第一個應用,並藉此學習一些Android基本概念以及構成應用的UI組件。 初學習,如果沒能全部理解,也不必擔心,後續還會涉及這些內容並有更加詳細的講解。 馬上要開發的應用名叫GeoQuiz,它能給出一道道地理知識問題。用戶點擊TRUE或FALSE 按鈕來回 ...
  • 原文地址:Jetpack架構組件學習(3)——Activity Results API使用 - Stars-One的雜貨小窩 技術與時俱進,頁面跳轉傳值一直使用的是startActivityForResult方法,如今有了新的API實現方式,學習並稍微總結下 😄 startActivityForR ...
  • 淺說一下吧 這個小項目由h5和css還有js和jq寫的 主題內容為個人音樂 博客等 首頁一級導航欄 以及側邊欄 整合部分圖標(側邊欄未添加收起操作 時間原因 會的朋友們可以自行添加一個動畫就可以 在main.js中) 中間音樂部分為作者和歌詞 點擊播放的按鈕可彈出對應的視頻mv 視頻mv均為酷狗音樂 ...
  • 瀏覽本篇文章前可以先看之前的前端網頁介紹和html常用標簽以便更容易理解 本文目錄: CSS相關查閱文檔:https://kohler.lanzouo.com/i0XFcz8svob 文檔截圖 CSS 技術介紹 CSS 是「層疊樣式表單」。是用於(增強)控制網頁樣式並允許將樣式信息與網頁內容分離的一 ...
  • 本文基於 Bilibili - 自由的加百利 前置條件: 需掌握函數的編寫、傳參、返回、調用 理解作用域、掌握定時器的用法 知道引用類型和基本數據類型的區別 知道函數也是引用類型 聽說過同步非同步的概念 瞭解類和對象的關係 匿名函數 來看一下一個函數的基本屬性: 匿名函數的自運行 我們可以將一個普通函 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...