溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在java項(xiàng)目中實(shí)現(xiàn)一個(gè)Composite組合模式

發(fā)布時(shí)間:2021-01-21 14:52:03 來(lái)源:億速云 閱讀:129 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章給大家介紹怎么在java項(xiàng)目中實(shí)現(xiàn)一個(gè)Composite組合模式,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

  • 組合模式核心思想類似文件夾的概念,構(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 branch3 = new CompositeTransparent("Composite XY");
  branch3.add(new LeafTransparent("Leaf XYA"));
  branch3.add(new LeafTransparent("Leaf XYB"));
  branch.add(branch3);

  //創(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 branch3 = new CompositeSafty("Composite XY");
  branch3.add(new LeafSafty("Leaf XYA"));
  branch3.add(new LeafSafty("Leaf XYB"));
  branch.add(branch3);

  //創(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();
 }
}

輸出如下:

怎么在java項(xiàng)目中實(shí)現(xiàn)一個(gè)Composite組合模式

關(guān)于怎么在java項(xiàng)目中實(shí)現(xiàn)一個(gè)Composite組合模式就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI