组合的示例代码 java_java实现Composite组合模式的实例代码
//20210121
寫在前面:剛期末考試完,考了面向對象,里邊兒有23個設計模式,我尋思著考完挨個兒實現一下,本文實現組合模式
組合模式核心思想類似文件夾的概念,構件樹形結構,樹形有葉子結點和文件夾結點,文件夾結點可以包含葉子結點和文件夾結點
分為兩種模式
- 透明型:所有節點構造全部相同,但是由于葉子結點沒有下層結點,所以其有些方法為空,會不安全
- 安全型:葉子結點和文件架節點構造不同,這樣展示的時候需要判斷節點屬性,不方便調用,但是由于沒有空方法,會很安全
透明型組合模式程序源代碼:
//節點抽象父類
/**
* 透明模式就是把組合使用的方法放到抽象類中,不管葉子對象還是數值對象都有相同的結構
* 這樣做的好處就是葉子結點和樹枝結點對于外界沒有區別,他們具備完全一致的行為接口
*/
public abstract class ComponentTransparent {
protected String name;
public ComponentTransparent(String name){
this.name = name;
}
//增加一個葉子構件或者樹枝構件
public abstract void add(ComponentTransparent componentTransparent);
//刪除
public abstract void remove(ComponentTransparent componentTransparent);
//獲取分支下的所有葉子構件和樹枝構件
public abstract void display(int depth);
}
//文件架節點實現子類
import java.util.ArrayList;
public class CompositeTransparent extends ComponentTransparent{
public CompositeTransparent(String name){
super(name);
}
//構建容器
private ArrayList 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) {
//輸出樹形結構
for (int i = 0;i
System.out.print("-");
}
System.out.println(this.name);
//下級遍歷
for(ComponentTransparent componentTransparent:this.componentTransparentsArraylist){
componentTransparent.display(depth+1);
}
}
}
//葉子節點實現子類
public class LeafTransparent extends ComponentTransparent{
public LeafTransparent(String name){
super(name);
}
@Override
public void add(ComponentTransparent componentTransparent) {
//空實現,拋出"不支持請求"異常
throw new UnsupportedOperationException();
}
@Override
public void remove(ComponentTransparent componentTransparent) {
throw new UnsupportedOperationException();
}
@Override
public void display(int depth) {
//輸出樹形結構的葉子節點
for (int i = 0;i
System.out.print("-");
}
System.out.println(this.name);
}
}
安全型組合模式源代碼:安全型中,葉子結點沒有增加移除方法,方法需要自己實現,而不會在父類中指出
//節點抽象父類
public abstract class ComponentSafty {
protected String name;
public ComponentSafty(String name){
this.name = name;
}
//展示
public abstract void display(int depth);
}
//文件夾節點實現子類
import java.util.ArrayList;
public class CompositeSafty extends ComponentSafty{
public CompositeSafty(String name){
super(name);
}
private ArrayList 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
System.out.print("-");
}
System.out.println(this.name);
for (ComponentSafty componentSafty : componentSaftyArrayList) {
componentSafty.display(depth+1);
}
}
}
//葉子結點實現子類
public class LeafSafty extends ComponentSafty{
public LeafSafty(String name){
super(name);
}
@Override
public void display(int depth) {
for (int i=0;i
System.out.print("-");
}
System.out.println(this.name);
}
}
測試主類程序源代碼
//測試主類
public class Main {
private static void transparent(){
//創建根節點以及其子節點
ComponentTransparent root = new CompositeTransparent("root");
root.add(new LeafTransparent("Leaf A"));
root.add(new LeafTransparent("Leaf B"));
//創建第二層結點及其子節點
ComponentTransparent branch = new CompositeTransparent("Composite X");
branch.add(new LeafTransparent("Leaf XA"));
branch.add(new LeafTransparent("Leaf XB"));
root.add(branch);
//創建第三層節點及其子結點
ComponentTransparent branch2 = new CompositeTransparent("Composite XY");
branch2.add(new LeafTransparent("Leaf XYA"));
branch2.add(new LeafTransparent("Leaf XYB"));
branch.add(branch2);
//創建第二層結點
root.add(new LeafTransparent("Leaf C"));
//常見第二層節點并刪除
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(){
//創建根節點以及其子節點
CompositeSafty root = new CompositeSafty("root");
root.add(new LeafSafty("Leaf A"));
root.add(new LeafSafty("Leaf B"));
//創建第二層結點及其子節點
CompositeSafty branch = new CompositeSafty("Composite X");
branch.add(new LeafSafty("Leaf XA"));
branch.add(new LeafSafty("Leaf XB"));
root.add(branch);
//創建第三層節點及其子結點
CompositeSafty branch2 = new CompositeSafty("Composite XY");
branch2.add(new LeafSafty("Leaf XYA"));
branch2.add(new LeafSafty("Leaf XYB"));
branch.add(branch2);
//創建第二層結點
root.add(new LeafSafty("Leaf C"));
//常見第二層節點并刪除
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實現Composite組合模式的文章就介紹到這了,更多相關java組合模式內容請搜索云海天教程以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持云海天教程!
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的组合的示例代码 java_java实现Composite组合模式的实例代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 路由器ax是什么意思?
- 下一篇: php制作留言板的题_PHP实现留言板功