設計模式11---組合模式(Composite Pattern)

一、組合模式定義

將對象組合成樹形結構以表示“部分-整體”的層次結構,使得用戶對單個對象和組合對象的使用具有一致性。Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

?????

?

??? 如上圖所示(截取自《Head First Design Patterns》一書),主要包括三個部分:?

??? 1. Component抽象組件。定義參加組合對象的共有方法和屬性,可以定義一些默認的函數或屬性。?

????2. Leaf葉子節點。構成組合樹的最小構建單元。?

??? 3. Composite樹枝節點組件。它的作用是組合樹枝節點和葉子節點形成一個樹形結構。?

Component : 組合中的對象聲明接口,在適當的情況下,實現所有類共有接口的默認行為。聲明一個接口用于訪問和管理 Component 的子部件。

 1 abstract class Component {
 2     protected String name;
 3 
 4     public Component(String name) {
 5         this.name = name;
 6     }
 7 
 8     public abstract void Add(Component c);
 9     public abstract void Remove(Component c);
10     public abstract void Display(int depth);
11 }

Leaf : 表示葉節點對象。葉子節點沒有子節點。

 1 class Leaf extends Component {
 2 
 3     public Leaf(String name) {
 4         super(name);
 5     }
 6 
 7     @Override
 8     public void Add(Component c) {
 9         System.out.println("Can not add to a leaf");
10     }
11 
12     @Override
13     public void Remove(Component c) {
14         System.out.println("Can not remove from a leaf");
15     }
16 
17     @Override
18     public void Display(int depth) {
19         String temp = "";
20         for (int i = 0; i < depth; i++) 
21             temp += '-';
22         System.out.println(temp + name);
23     }
24 
25 }

?

Composite : 定義枝節點行為,用來存儲子部件,在 Component 接口中實現與子部件相關的操作。例如 Add 和 Remove。

 1 class Composite extends Component {
 2 
 3     private List<Component> children = new ArrayList<Component>();
 4 
 5     public Composite(String name) {
 6         super(name);
 7     }
 8 
 9     @Override
10     public void Add(Component c) {
11         children.add(c);
12     }
13 
14     @Override
15     public void Remove(Component c) {
16         children.remove(c);
17     }
18 
19     @Override
20     public void Display(int depth) {
21         String temp = "";
22         for (int i = 0; i < depth; i++) 
23             temp += '-';
24         System.out.println(temp + name);
25 
26         for (Component c : children) {
27             c.Display(depth + 2);
28         }
29     }
30 
31 }

Client : 通過 Component 接口操作結構中的對象。

 1 public class CompositePattern {
 2 
 3 public static void main(String[] args) {
 4     Composite root = new Composite("root");
 5     root.Add(new Leaf("Leaf A"));
 6     root.Add(new Leaf("Leaf B"));
 7 
 8     Composite compX = new Composite("Composite X");
 9     compX.Add(new Leaf("Leaf XA"));
10     compX.Add(new Leaf("Leaf XB"));
11     root.Add(compX);
12 
13     Composite compXY = new Composite("Composite XY");
14     compXY.Add(new Leaf("Leaf XYA"));
15     compXY.Add(new Leaf("Leaf XYB"));
16     compX.Add(compXY);
17 
18     root.Display(1);
19 }
20 
21 }

二、組合模式優勢?

  節點自由擴展增加。使用組合模式,如果想增加一個樹枝節點或者葉子節點都是很簡單的,只要找到它的父節點就可以了,非常容易擴展,符合“開閉原則”。應用最廣的模式之一。應用在維護和展示部分-整體關系的場景,如樹形菜單、文件夾管理等等。一棵樹形結構的所有節點都是Component,局部和整體對調用者來說都是一樣的,沒有區別,所以高層模塊不比關心自己處理的是單個對象還是整個組合結構,簡化了高層模塊的代碼。

?  1、可以清楚地定義分層次的復雜對象,表示對象的全部或部分層次,使得增加新構件也更容易。

????? 2、客戶端調用簡單,客戶端可以一致的使用組合結構或其中單個對象。

????? 3、定義了包含葉子對象和容器對象的類層次結構,葉子對象可以被組合成更復雜的容器對象,而這個容器對象又可以被組合,這樣不斷遞歸下去,可以形成復雜的樹形結構。

????? 4、更容易在組合體內加入對象構件,客戶端不必因為加入了新的對象構件而更改原有代碼。

組合模式的缺點: 使設計變得更加抽象,對象的業務規則如果很復雜,則實現組合模式具有很大挑戰性,而且不是所有的方法都與葉子對象子類都有關聯。

使用場景:

 ? ?1、需要表示一個對象整體或部分層次,在具有整體和部分的層次結構中,希望通過一種方式忽略整體與部分的差異,可以一致地對待它們。

?

????? 2、讓客戶能夠忽略不同對象層次的變化,客戶端可以針對抽象構件編程,無須關心對象層次結構的細節。

?

?三、組合模式在Android源碼中的應用?

??? 在Android源碼中,都能找到使用組合模式的例子,其中在《Android源碼學習之觀察者模式應用》介紹到的ViewGroup和View的結構就是一個組合模式,結構圖如下所示:?

?? ? 現在來看看它們是如何利用組合模式組織在一起的,首先在View類定義了有關具體操作,然后在ViewGroup類中繼承View類,并添加相關的增加、刪除和查找孩子View節點,代碼如下:?

/*
* @attr ref android.R.styleable#ViewGroup_clipChildren * @attr ref android.R.styleable#ViewGroup_clipToPadding * @attr ref android.R.styleable#ViewGroup_layoutAnimation * @attr ref android.R.styleable#ViewGroup_animationCache * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren * @attr ref android.R.styleable#ViewGroup_descendantFocusability * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges */ public abstract class ViewGroup extends View implements ViewParent, ViewManager {

?接著看增加孩子節點函數:?

  /*** Adds a child view. If no layout parameters are already set on the child, the* default parameters for this ViewGroup are set on the child.** @param child the child view to add** @see #generateDefaultLayoutParams()*/public void addView(View child) { addView(child, -1); } /** * Adds a child view. If no layout parameters are already set on the child, the * default parameters for this ViewGroup are set on the child. * * @param child the child view to add * @param index the position at which to add the child * * @see #generateDefaultLayoutParams() */ public void addView(View child, int index) { LayoutParams params = child.getLayoutParams(); if (params == null) { params = generateDefaultLayoutParams(); if (params == null) { throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); } } addView(child, index, params); } /** * Adds a child view with this ViewGroup's default layout parameters and the * specified width and height. * * @param child the child view to add */ public void addView(View child, int width, int height) { final LayoutParams params = generateDefaultLayoutParams(); params.width = width; params.height = height; addView(child, -1, params); } /** * Adds a child view with the specified layout parameters. * * @param child the child view to add * @param params the layout parameters to set on the child */ public void addView(View child, LayoutParams params) { addView(child, -1, params); } /** * Adds a child view with the specified layout parameters. * * @param child the child view to add * @param index the position at which to add the child * @param params the layout parameters to set on the child */ public void addView(View child, int index, LayoutParams params) { if (DBG) { System.out.println(this 

轉載于:https://www.cnblogs.com/linghu-java/p/5728308.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/457515.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/457515.shtml
英文地址,請注明出處:http://en.pswp.cn/news/457515.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Linux 多核下綁定硬件中斷到不同 CPU(IRQ Affinity)

轉載 - Linux 多核下綁定硬件中斷到不同 CPU&#xff08;IRQ Affinity&#xff09; 作者 digoal 日期 2016-11-20 標簽 Linux , IRQ , 中斷 , CPU親和 , 綁定中斷處理CPU 背景 原文 http://www.vpsee.com/2010/07/load-balancing-with-irq-smp-affinity/ 原文 硬件中斷發生頻繁…

請列舉你了解的分布式鎖_這幾種常見的“分布式鎖”寫法,搞懂再也不怕面試官,安排!...

什么是分布式鎖&#xff1f;大家好&#xff0c;我是jack xu&#xff0c;今天跟大家聊一聊分布式鎖。首先說下什么是分布式鎖&#xff0c;當我們在進行下訂單減庫存&#xff0c;搶票&#xff0c;選課&#xff0c;搶紅包這些業務場景時&#xff0c;如果在此處沒有鎖的控制&#x…

leetcode 268

等差數列求值 1 class Solution {2 public:3 int missingNumber(vector<int>& nums) {4 int nnums.size();5 int kn*(n1)/2;6 for(int i0;i<n;i)7 k-nums[i];8 return k;9 } 10 }; 轉載于:https://www.cnblogs.…

301緩存重定向?301 Moved Permanently (from disk cache)

今天在寫一個博客系統時&#xff0c;發現首頁數據經常刷新不出來&#xff0c;甚至后端根本就沒有接受到這個請求&#xff0c;以為是Ajax的問題&#xff0c;但通過抓包發現Ajax請求確實已經發出去了&#xff0c;但狀態碼是 301 Moved Permanently (from disk cache),301是永久重…

Firefox 50優化Electrolysis

Mozilla正式發布Firefox 50。最新的版本中提升了來自多個內容進程用戶的用戶體驗&#xff0c;并修復了十幾個高影響的安全漏洞。\\在Firefox最新版本的變更中&#xff0c;我們注意到了它對于Electrolysis的進一步改進。Electrolysis是Mozilla實現在后臺進程中呈現和執行web相關…

ModuleNotFoundError: No module named '_ctypes' ERROR:Command errored out with exit status 1: python

Ubuntu下載 nginx 時報錯&#xff1a; ERROR: Command errored out with exit status 1:command: /usr/local/bin/python3.7 -c import sys, setuptools, tokenize; sys.argv[0] ""/tmp/pip-install-7e0xdb36/uwsgi/setup.py""; __file__""/tmp…

python opc plc_PYthon簡易OPC數據采集寫入Access

利用hollias comm opcserver 與Python實現交互。代碼如下&#xff1a;# -*- coding: utf-8 -*-from sys import *from getopt import *#from os import * 造成f open(test.txt, r) TypeError: an integer is required錯誤import signalimport sysimport osimport typesimport …

邊工作邊刷題:70天一遍leetcode: day 73

Read N Characters Given Read4 I/II 要點&#xff1a;這題的要點就是搞清楚幾個變量的內在邏輯&#xff1a;只有buffer是整4 bytes的。而client要讀的bytes&#xff08;需求&#xff09;和實際上disk上有的bytes&#xff08;供給&#xff09;都是不整的。所以&#xff0c; 循環…

javascript時間戳和日期字符串相互轉換

1 <html xmlns"http://www.w3.org/1999/xhtml">2 <head>3 <meta http-equiv"Content-Type" content"text/html; charsetutf-8" />4 <script type"text/javascript">5 // 獲取當前時間戳(以s為單位)6 var time…

wireshark 十六進制過濾_CTF流量分析之wireshark使用

01.基本介紹在CTF比賽中&#xff0c;對于流量包的分析取證是一種十分重要的題型。通常這類題目都是會提供一個包含流量數據的pcap文件&#xff0c;參賽選手通過該文件篩選和過濾其中無關的流量信息&#xff0c;根據關鍵流量信息找出flag或者相關線索。pcap流量包的分析通常都是…

vim 插件管理

1  進入自己的vim mkdir ./bundle/vundle 2  在vimrc同級中執行 git clone https://github.com/gmarik/vundle.git ./bundle/vundle 將一些插件文件 下載到./bundle/vundle中 3  編寫自己的vim配置&#xff0c;其實很簡單 set nocompatible " be iMp…

ubuntu install wiznote

sudo add-apt-repository ppa:wiznote-team #添加官方源sudo apt-get update #更新源sudo apt-get install wiznote #安裝為知筆記

python 對象序列化 pickling_python操作文件——序列化pickling和JSON

當我們在內存中定義一個dict的時候&#xff0c;我們是可以隨時修改變量的內容的&#xff1a;>>> ddict(namewc,age28)>>>d{name: wc, age: 28}我們可以隨時修改name和age的值。但是當我們重新運行程序的時候&#xff0c;name、age的初始化值還是wc和28&#…

python實現Redis訂閱發布

Redis 發布訂閱 Redis 發布訂閱可以用在像消息通知&#xff0c;群聊&#xff0c;定向推送&#xff0c;參數刷新加載等業務場景 發布訂閱模型有三個角色&#xff1a; 發布者&#xff08;Publisher&#xff09;訂閱者(Subscriber)頻道(channel) 每個訂閱者可以訂閱多個頻道&am…

nfs的快速部署

1、nfs內核自帶協議模塊不用安裝&#xff0c;如果沒有yum安裝yum -y install nfs-utils2、配置vim /etc/exports #配置文件 此文件一般為空&#xff0c;編寫格式為&#xff1a; /share/word 192.168.31.254(rw) 192.168.31.252(ro) # 共享/share/word目錄給192.168.…

redistemplate怎么修改數據_如何使用RedisTemplate訪問Redis數據結構?

在springboot項目中&#xff0c;集成各種框架變得非常容易。下面簡單介紹一下如何在springboot項目中集成單機模式redis。集群模式也差不多&#xff0c;這里就不過多介紹了。首先你得安裝redis服務&#xff0c;無論在linux還是windows系統上。如果沒有安裝&#xff0c;請自行百…

HyperLogLog原理與在Redis中的使用

Redis-HyperLogLog 基于HyperLogLog算法&#xff0c;使用極小的空間完成巨量運算 Redis 中HyperLogLog 基本使用 常用命令 PFADD key element [element …]: 將任意數量的元素添加到指定的 HyperLogLog 里面。PFCOUNT key [key …]: 計算hyperloglog的獨立總數prmerge destk…

iOS開發UI篇—xib的簡單使用

一、簡單介紹 xib和storyboard的比較&#xff0c;一個輕量級一個重量級。 共同點&#xff1a; 都用來描述軟件界面 都用Interface Builder工具來編輯 不同點: Xib是輕量級的&#xff0c;用來描述局部的UI界面 Storyboard是重量級的&#xff0c;用來描述整個軟件的多個界面&…

【云棲計算之旅】線下沙龍第2期精彩預告:Docker在云平臺上的最佳實踐

Docker是一個開源的應用容器引擎&#xff0c;提供了一種在安全、可重復的環境中自動部署軟件的方式&#xff0c;允許開發者將他們的應用和依賴包打包到一個可移植的容器中&#xff0c;然后發布到任何流行的Linux機器上&#xff0c;也可以實現虛擬化。容器完全使用沙箱機制&…

小程序mpvue圖片繪制水印_開發筆記:使用 mpvue 開發斗圖小程序

之前用過 wepy 框架寫了個小程序 GitHub - yshkk/shanbay-mina: 基于 wepy 框架的 “扇貝閱讀” 微信小程序 &#xff0c;感覺寫法上類似 vue&#xff0c;但不那么徹底。現在美團點評發布的 mpvue 支持開發者可以用 vue 的語法開發微信小程序&#xff0c;正好有強需求需要一個斗…