亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實(shí)現(xiàn)Composite組合模式的實(shí)例代碼

 更新時(shí)間:2021年01月21日 11:37:46   作者:醉生夢(mèng)死_0423  
這篇文章主要介紹了java實(shí)現(xiàn)Composite組合模式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

//20210121
寫在前面:剛期末考試完,考了面向?qū)ο?,里邊兒?3個(gè)設(shè)計(jì)模式,我尋思著考完挨個(gè)兒實(shí)現(xiàn)一下,本文實(shí)現(xiàn)組合模式

  • 組合模式核心思想類似文件夾的概念,構(gòu)件樹(shù)形結(jié)構(gòu),樹(shù)形有葉子結(jié)點(diǎn)和文件夾結(jié)點(diǎn),文件夾結(jié)點(diǎn)可以包含葉子結(jié)點(diǎn)和文件夾結(jié)點(diǎn)
  • 分為兩種模式

- 透明型:所有節(jié)點(diǎn)構(gòu)造全部相同,但是由于葉子結(jié)點(diǎn)沒(méi)有下層結(jié)點(diǎn),所以其有些方法為空,會(huì)不安全
- 安全型:葉子結(jié)點(diǎn)和文件架節(jié)點(diǎn)構(gòu)造不同,這樣展示的時(shí)候需要判斷節(jié)點(diǎn)屬性,不方便調(diào)用,但是由于沒(méi)有空方法,會(huì)很安全

透明型組合模式程序源代碼:

//節(jié)點(diǎn)抽象父類
/**
 * 透明模式就是把組合使用的方法放到抽象類中,不管葉子對(duì)象還是數(shù)值對(duì)象都有相同的結(jié)構(gòu)
 * 這樣做的好處就是葉子結(jié)點(diǎn)和樹(shù)枝結(jié)點(diǎn)對(duì)于外界沒(méi)有區(qū)別,他們具備完全一致的行為接口
 */
public abstract class ComponentTransparent {
 protected String name;

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

 //增加一個(gè)葉子構(gòu)件或者樹(shù)枝構(gòu)件
 public abstract void add(ComponentTransparent componentTransparent);

 //刪除
 public abstract void remove(ComponentTransparent componentTransparent);

 //獲取分支下的所有葉子構(gòu)件和樹(shù)枝構(gòu)件
 public abstract void display(int depth);
}
//文件架節(jié)點(diǎn)實(shí)現(xiàn)子類
import java.util.ArrayList;

public class CompositeTransparent extends ComponentTransparent{

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

 //構(gòu)建容器
 private ArrayList<ComponentTransparent> componentTransparentsArraylist= new ArrayList<>();

 @Override
 public void add(ComponentTransparent componentTransparent) {
  this.componentTransparentsArraylist.add(componentTransparent);
 }

 @Override
 public void remove(ComponentTransparent componentTransparent) {
  this.componentTransparentsArraylist.remove(componentTransparent);
 }

 @Override
 public void display(int depth) {
  //輸出樹(shù)形結(jié)構(gòu)
  for (int i = 0;i<depth;++i){
   System.out.print("-");
  }
  System.out.println(this.name);

  //下級(jí)遍歷
  for(ComponentTransparent componentTransparent:this.componentTransparentsArraylist){
   componentTransparent.display(depth+1);
  }
 }
}
//葉子節(jié)點(diǎn)實(shí)現(xiàn)子類
public class LeafTransparent extends ComponentTransparent{
 public LeafTransparent(String name){
  super(name);
 }

 @Override
 public void add(ComponentTransparent componentTransparent) {
  //空實(shí)現(xiàn),拋出"不支持請(qǐng)求"異常
  throw new UnsupportedOperationException();
 }

 @Override
 public void remove(ComponentTransparent componentTransparent) {
  throw new UnsupportedOperationException();
 }

 @Override
 public void display(int depth) {
  //輸出樹(shù)形結(jié)構(gòu)的葉子節(jié)點(diǎn)
  for (int i = 0;i<depth;++i){
   System.out.print("-");
  }
  System.out.println(this.name);
 }
}

安全型組合模式源代碼:安全型中,葉子結(jié)點(diǎn)沒(méi)有增加移除方法,方法需要自己實(shí)現(xiàn),而不會(huì)在父類中指出

//節(jié)點(diǎn)抽象父類
public abstract class ComponentSafty {
 protected String name;

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

 //展示
 public abstract void display(int depth);
}
//文件夾節(jié)點(diǎn)實(shí)現(xiàn)子類
import java.util.ArrayList;

public class CompositeSafty extends ComponentSafty{
 public CompositeSafty(String name){
  super(name);
 }

 private ArrayList<ComponentSafty> componentSaftyArrayList = new ArrayList<>();

 public void add(ComponentSafty component){
  this.componentSaftyArrayList.add(component);
 }

 public void remove(ComponentSafty componentSafty){
  this.componentSaftyArrayList.remove(componentSafty);
 }

 @Override
 public void display(int depth) {
  for (int i=0;i<depth;++i){
   System.out.print("-");
  }

  System.out.println(this.name);

  for (ComponentSafty componentSafty : componentSaftyArrayList) {
   componentSafty.display(depth+1);
  }
 }
}
//葉子結(jié)點(diǎn)實(shí)現(xiàn)子類
public class LeafSafty extends ComponentSafty{

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

 @Override
 public void display(int depth) {
  for (int i=0;i<depth;++i){
   System.out.print("-");
  }

  System.out.println(this.name);
 }
}

測(cè)試主類程序源代碼

//測(cè)試主類
public class Main {
 private static void transparent(){
  //創(chuàng)建根節(jié)點(diǎn)以及其子節(jié)點(diǎn)
  ComponentTransparent root = new CompositeTransparent("root");
  root.add(new LeafTransparent("Leaf A"));
  root.add(new LeafTransparent("Leaf B"));

  //創(chuàng)建第二層結(jié)點(diǎn)及其子節(jié)點(diǎn)
  ComponentTransparent branch = new CompositeTransparent("Composite X");
  branch.add(new LeafTransparent("Leaf XA"));
  branch.add(new LeafTransparent("Leaf XB"));
  root.add(branch);

  //創(chuàng)建第三層節(jié)點(diǎn)及其子結(jié)點(diǎn)
  ComponentTransparent branch2 = new CompositeTransparent("Composite XY");
  branch2.add(new LeafTransparent("Leaf XYA"));
  branch2.add(new LeafTransparent("Leaf XYB"));
  branch.add(branch2);

  //創(chuàng)建第二層結(jié)點(diǎn)
  root.add(new LeafTransparent("Leaf C"));

  //常見(jiàn)第二層節(jié)點(diǎn)并刪除
  ComponentTransparent leaf = new LeafTransparent("Leaf D");
  root.add(leaf);
  root.display(1);
  root.remove(leaf);

  for(int i =0;i<10;++i){
   System.out.print("=");
  }
  System.out.println();
  //展示
  root.display(1);
 }

 private static void safty(){
//創(chuàng)建根節(jié)點(diǎn)以及其子節(jié)點(diǎn)
  CompositeSafty root = new CompositeSafty("root");
  root.add(new LeafSafty("Leaf A"));
  root.add(new LeafSafty("Leaf B"));

  //創(chuàng)建第二層結(jié)點(diǎn)及其子節(jié)點(diǎn)
  CompositeSafty branch = new CompositeSafty("Composite X");
  branch.add(new LeafSafty("Leaf XA"));
  branch.add(new LeafSafty("Leaf XB"));
  root.add(branch);

  //創(chuàng)建第三層節(jié)點(diǎn)及其子結(jié)點(diǎn)
  CompositeSafty branch2 = new CompositeSafty("Composite XY");
  branch2.add(new LeafSafty("Leaf XYA"));
  branch2.add(new LeafSafty("Leaf XYB"));
  branch.add(branch2);

  //創(chuàng)建第二層結(jié)點(diǎn)
  root.add(new LeafSafty("Leaf C"));

  //常見(jiàn)第二層節(jié)點(diǎn)并刪除
  LeafSafty leaf = new LeafSafty("Leaf D");
  root.add(leaf);
  root.display(1);
  root.remove(leaf);

  for(int i =0;i<10;++i){
   System.out.print("=");
  }
  System.out.println();
  //展示
  root.display(1);
 }

 public static void main(String[] args) {
  System.out.println("透明模式:");
  transparent();
  for(int i =0;i<10;++i){
   System.out.print("=");
  }
  System.out.println();
  System.out.println("安全模式:");
  safty();
 }
}

輸出如下:

到此這篇關(guān)于java實(shí)現(xiàn)Composite組合模式的文章就介紹到這了,更多相關(guān)java組合模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Boot+Mybatis的整合過(guò)程

    Spring Boot+Mybatis的整合過(guò)程

    這篇文章主要介紹了Spring Boot+Mybatis的整合過(guò)程,需要的朋友可以參考下
    2017-07-07
  • Java多線程Semaphore工具的使用詳解

    Java多線程Semaphore工具的使用詳解

    Semaphore 是一種用于控制線程并發(fā)訪問(wèn)數(shù)的同步工具。它通過(guò)維護(hù)一定數(shù)量的許可證來(lái)限制對(duì)共享資源的訪問(wèn),許可證的數(shù)量就是可以同時(shí)訪問(wèn)共享資源的線程數(shù)目,需要的朋友可以參考下
    2023-05-05
  • java關(guān)于持久層面試題目整理

    java關(guān)于持久層面試題目整理

    在本篇文章里小編給大家分享的是一篇關(guān)于java關(guān)于持久層面試題目整理內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-03-03
  • 詳解Maven安裝教程及是否安裝成功

    詳解Maven安裝教程及是否安裝成功

    這篇文章主要介紹了詳解Maven安裝教程及是否安裝成功,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Java實(shí)現(xiàn)企業(yè)微信消息推送功能的詳細(xì)步驟

    Java實(shí)現(xiàn)企業(yè)微信消息推送功能的詳細(xì)步驟

    這篇文章主要介紹了Java實(shí)現(xiàn)企業(yè)微信消息推送功能,本文圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • Spring mvc是如何實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的前后端的連接操作的?

    Spring mvc是如何實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的前后端的連接操作的?

    今天給大家?guī)?lái)的是關(guān)于Spring mvc的相關(guān)知識(shí),文章圍繞著Spring mvc是如何實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的前后端的連接操作的展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Java使用OCR技術(shù)識(shí)別驗(yàn)證碼實(shí)現(xiàn)自動(dòng)化登陸方法

    Java使用OCR技術(shù)識(shí)別驗(yàn)證碼實(shí)現(xiàn)自動(dòng)化登陸方法

    在本篇文章里小編給大家分享的是關(guān)于Java 如何使用 OCR 技術(shù)識(shí)別驗(yàn)證碼實(shí)現(xiàn)自動(dòng)化登陸的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-08-08
  • 最新評(píng)論