欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java設(shè)計(jì)模式之組合模式(Composite)

 更新時(shí)間:2017年01月02日 14:22:36   作者:yuminfeng728  
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式之組合模式Composite,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

概述

是一種結(jié)構(gòu)型模式,將對(duì)象以樹(shù)形結(jié)構(gòu)組織起來(lái),以表示“部分 - 整體”的層次結(jié)構(gòu),使得客戶端對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有唯一性。

UML類圖

上面的類圖包含的角色:

Component:為參加組合的對(duì)象聲明一個(gè)公共的接口,不管是組合還是葉節(jié)點(diǎn)。
Leaf:在組合中表示葉子結(jié)點(diǎn)對(duì)象,葉子結(jié)點(diǎn)沒(méi)有子結(jié)點(diǎn)。
Composite:表示參加組合的有子對(duì)象的對(duì)象,并給出樹(shù)枝構(gòu)建的行為;

代碼示例

import java.util.ArrayList;
import java.util.List;

abstract class Component {

  protected String name;

  public Component(String name) {
    this.name = name;
  }

  public abstract void Add(Component c);

  public abstract void Remove(Component c);

  public abstract void GetChild(int depth);
}

class Leaf extends Component {

  public Leaf(String name) {
    super(name);
  }

  @Override
  public void Add(Component c) {
    System.out.println("Can not add to a leaf");
  }

  @Override
  public void Remove(Component c) {
    System.out.println("Can not remove from a leaf");
  }

  @Override
  public void GetChild(int depth) {
    String temp = " ";
    for (int i = 0; i < depth; i++) {
      temp += "-";
      System.out.println(temp + name);
    }
  }

}

class Composite extends Component {

  private List<Component> children = new ArrayList<>();

  public Composite(String name) {
    super(name);
  }

  @Override
  public void Add(Component c) {
    children.add(c);
  }

  @Override
  public void Remove(Component c) {
    children.remove(c);
  }

  @Override
  public void GetChild(int depth) {

    for (Component c : children) {
      c.GetChild(depth);
    }
  }

}

public class Main {

  public static void main(String args[]) {
    Composite root = new Composite("root");
    root.Add(new Leaf("Leaf A"));
    root.Add(new Leaf("Leaf B"));

    Composite compX = new Composite("Composite X");
    compX.Add(new Leaf("Leaf XA"));
    compX.Add(new Leaf("Leaf XB"));
    root.Add(compX);

    Composite compXY = new Composite("Composite XY");
    compXY.Add(new Leaf("Leaf XYA"));
    compXY.Add(new Leaf("Leaf XYB"));
    compX.Add(compXY);

    root.GetChild(3);
  }

}

應(yīng)用場(chǎng)景

1.要求表示對(duì)象的部分-整體層次結(jié)構(gòu)。
2.想要客戶端忽略組合對(duì)象與單個(gè)對(duì)象的差異,客戶端將統(tǒng)一地使用組合結(jié)構(gòu)中的所有對(duì)象。

組合模式定義由Leaf對(duì)象和Composite對(duì)象組成的類結(jié)構(gòu);
使得客戶端變得簡(jiǎn)單;
它使得添加或刪除子部件變得很容易。

 以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring打包到j(luò)ar包的問(wèn)題解決

    spring打包到j(luò)ar包的問(wèn)題解決

    這篇文章主要給大家介紹了關(guān)于spring打包到j(luò)ar包遇到的問(wèn)題的解決方法,文中通過(guò)實(shí)例代碼結(jié)束的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用spring打包具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java中的interrupt、interrupted和isInterrupted方法區(qū)別詳解

    Java中的interrupt、interrupted和isInterrupted方法區(qū)別詳解

    這篇文章主要介紹了Java中的interrupt、interrupted和isInterrupted方法區(qū)別詳解,interrupt用于中斷線程,調(diào)用該方法的線程的狀態(tài)將會(huì)被設(shè)置為中斷狀態(tài),線程中斷僅僅是設(shè)置線程的中斷狀態(tài)位,并不會(huì)停止線程,需要用戶自己去監(jiān)視線程的狀態(tài)并作出處理,需要的朋友可以參考下
    2023-12-12
  • Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題整理

    Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題整理

    在本篇文章里小編給各位整理了關(guān)于Java學(xué)習(xí)關(guān)于循環(huán)和數(shù)組練習(xí)題相關(guān)內(nèi)容,有興趣的朋友們跟著參考學(xué)習(xí)下。
    2019-07-07
  • java 排序算法之快速排序

    java 排序算法之快速排序

    這篇文章主要介紹了java 排序算法之快速排序,文中通過(guò)圖片和代碼講解相關(guān)知識(shí)非常詳細(xì),大家如果有需要的話可以參考一下這篇文章
    2021-09-09
  • Java 線程池的作用以及該如何使用

    Java 線程池的作用以及該如何使用

    這篇文章主要介紹了Java 線程池的作用以及該如何使用,幫助大家更好的理解和學(xué)習(xí)Java的相關(guān)知識(shí),感興趣的朋友可以了解下
    2021-01-01
  • java 工廠模式的實(shí)例詳解

    java 工廠模式的實(shí)例詳解

    這篇文章主要介紹了java 工廠模式的實(shí)例詳解的相關(guān)資料,這里舉例說(shuō)明該如何實(shí)現(xiàn)工廠模式,需要的朋友可以參考下
    2017-09-09
  • Idea中Java項(xiàng)目如何修改項(xiàng)目名

    Idea中Java項(xiàng)目如何修改項(xiàng)目名

    這篇文章主要介紹了Idea中Java項(xiàng)目如何修改項(xiàng)目名問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java8利用Stream實(shí)現(xiàn)列表去重的方法詳解

    Java8利用Stream實(shí)現(xiàn)列表去重的方法詳解

    這篇文章主要為大家介紹了Java利用Stream實(shí)現(xiàn)列表去重的幾種方法詳解,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下
    2022-04-04
  • spring 自動(dòng)注入AutowiredAnnotationBeanPostProcessor源碼解析

    spring 自動(dòng)注入AutowiredAnnotationBeanPostProcessor源碼解析

    這篇文章主要介紹了spring自動(dòng)注入AutowiredAnnotationBeanPostProcessor源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 一文學(xué)會(huì)使用sa-token解決網(wǎng)站權(quán)限驗(yàn)證

    一文學(xué)會(huì)使用sa-token解決網(wǎng)站權(quán)限驗(yàn)證

    這篇文章主要為大家介紹了使用sa-token解決網(wǎng)站權(quán)限驗(yàn)證方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評(píng)論