北风设计模式课程---16、组合模式

北风设计模式课程---16、组合模式

一、总结

一句话总结:

不仅要通过视频学,还要看别的博客里面的介绍,搜讲解,搜作用,搜实例
设计模式都是对生活的抽象,比如用户获得装备,我可以先装备工厂先生产出来装备,然后给宗门武器库,宗门武器库发给我,如果是打怪获得的装备,可以是装备工厂把装备给的怪物装备库

 

1、什么是组合模式?

树:树枝+叶子:通过递归手段来构造树形的对象结构,并可以通过一个对象来访问整个对象树。

Composite模式也叫组合模式,是构造型的设
计模式之一。通过递归手段来构造树形的对象结
构,并可以通过一个对象来访问整个对象树。

 

2、组合模式的角色和职责?

Component(树形结构的节点抽象):比如文件接口
Leaf(树形结构的叶节点):比如文件
Composite(树形结构的枝节点):比如文件夹

 

Component (树形结构的节点抽象)
- 为所有的对象定义统一的接口(公共属性,行为等的定义)
- 提供管理子节点对象的接口方法
- [可选]提供管理父节点对象的接口方法

Leaf (树形结构的叶节点)
Component的实现子类

Composite(树形结构的枝节点)
Component的实现子类

 

3、组合模式参考代码的角色实例?

Component(树形结构的节点抽象):比如文件接口:File.java
Leaf(树形结构的叶节点):比如文件:IFile.java
Composite(树形结构的枝节点):比如文件夹:Folder.java
客户端调用:MainClass.java

 

 

 

二、内容在总结中

1、相关知识

Component(树形结构的节点抽象):比如文件接口:File.java
Leaf(树形结构的叶节点):比如文件:IFile.java
Composite(树形结构的枝节点):比如文件夹:Folder.java
客户端调用:MainClass.java

 

2、代码

Component(树形结构的节点抽象):比如文件接口:File.java

import java.util.List;


public class File implements IFile {
    private String name;
    
    public File(String name) {
        this.name = name;
    }
    

    public void display() {
        System.out.println(name);
    }

    public List<IFile> getChild() {
        return null;
    }


    public boolean add(IFile file) {
        return false;
    }

    public boolean remove(IFile file) {
        return false;
    }

}

 


Leaf(树形结构的叶节点):比如文件:IFile.java

import java.util.List;

/*
 * 文件节点抽象(是文件和目录的父类)
 */
public interface IFile {
    
    //显示文件或者文件夹的名称
    public void display();
    
    //添加
    public boolean add(IFile file);
    
    //移除
    public boolean remove(IFile file);
    
    //获得子节点
    public List<IFile> getChild();
}

 


Composite(树形结构的枝节点):比如文件夹:Folder.java

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


public class Folder implements IFile{
    private String name;
    private List<IFile> children;
    
    public Folder(String name) {
        this.name = name;
        children = new ArrayList<IFile>();
    }
    

    public void display() {
        System.out.println(name);
    }

    public List<IFile> getChild() {
        return children;
    }


    public boolean add(IFile file) {
        return children.add(file);
    }


    public boolean remove(IFile file) {
        return children.remove(file);
    }


}

 


客户端调用:MainClass.java

import java.util.List;


public class MainClass {
    public static void main(String[] args) {
        //C盘
        Folder rootFolder = new Folder("C:");
        //beifeng目录
        Folder beifengFolder = new Folder("beifeng");
        //beifeng.txt文件
        File beifengFile = new File("beifeng.txt");
        
        rootFolder.add(beifengFolder);
        rootFolder.add(beifengFile);
        
        //ibeifeng目录
        Folder ibeifengFolder = new Folder("ibeifeng");
        File ibeifengFile = new File("ibeifeng.txt");
        beifengFolder.add(ibeifengFolder);
        beifengFolder.add(ibeifengFile);
        
        Folder iibeifengFolder = new Folder("iibeifeng");
        File iibeifengFile = new File("iibeifeng.txt");
        ibeifengFolder.add(iibeifengFolder);
        ibeifengFolder.add(iibeifengFile);
        
        displayTree(rootFolder,0);
        
    }
    
    public static void displayTree(IFile rootFolder, int deep) {
        for(int i = 0; i < deep; i++) {
            System.out.print("--");
        }
        //显示自身的名称
        rootFolder.display();
        //获得子树
        List<IFile> children = rootFolder.getChild();
        //遍历子树
        for(IFile file : children) {
            if(file instanceof File) {
                for(int i = 0; i <= deep; i++) {
                    System.out.print("--");
                }
                file.display();
            } else {
                displayTree(file,deep + 1);
            }
        }
    }
}

 

 

三、java设计模式-----13、组合模式

转自或参考:java设计模式-----13、组合模式
https://www.cnblogs.com/xiaobai1226/p/8567280.html

  Composite模式也叫组合模式,是构造型的设计模式之一。通过递归手段来构造树形的对象结构,并可以通过一个对象来访问整个对象树。 

  组合(Composite)模式的其它翻译名称也很多,比如合成模式、树模式等等。在《设计模式》一书中给出的定义是:将对象以树形结构组织起来,以达成“部分-整体”的层次结构,使得客户端对单个对象和组合对象的使用具有一致性。

 

  从定义中可以得到使用组合模式的环境为:在设计中想表示对象的“部分-整体”层次结构;希望用户忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象。

 

  

 

  组合模式的角色和职责

  1、Component (树形结构的节点抽象)

    1.1、为所有的对象定义统一的接口(公共属性,行为等的定义)

    1.2、提供管理子节点对象的接口方法

    1.3、[可选]提供管理父节点对象的接口方法

  2、Leaf (树形结构的叶节点)

    Component的实现子类

  3、Composite(树形结构的枝节点)

    Component的实现子类

  安全性与透明性

  组合模式中必须提供对子对象的管理方法,不然无法完成对子对象的添加删除等等操作,也就失去了灵活性和扩展性。但是管理方法是在Component中就声明还是在Composite中声明呢?

  一种方式是在Component里面声明所有的用来管理子类对象的方法,以达到Component接口的最大化(如下图所示)。目的就是为了使客户看来在接口层次上树叶和分支没有区别——透明性。但树叶是不存在子类的,因此Component声明的一些方法对于树叶来说是不适用的。这样也就带来了一些安全性问题。

 

 

 

  另一种方式就是只在Composite里面声明所有的用来管理子类对象的方法(如下图所示)。这样就避免了上一种方式的安全性问题,但是由于叶子和分支有不同的接口,所以又失去了透明性。

  《设计模式》一书认为:在这一模式中,相对于安全性,我们比较强调透明性。对于第一种方式中叶子节点内不需要的方法可以使用空处理或者异常报告的方式来解决。

   我们举个例子,比如说文件夹与文件,层次结构就符合树形结构

  首先我们新建一个Component接口(树形结构的节点抽象)

 1 /*
 2  * 文件节点抽象(是文件和目录的父类)
 3  */
 4 public interface IFile {
 5     
 6     //显示文件或者文件夹的名称
 7     public void display();
 8     
 9     //添加
10     public boolean add(IFile file);
11     
12     //移除
13     public boolean remove(IFile file);
14     
15     //获得子节点
16     public List<IFile> getChild();
17 }

   该例子符合透明性,如果是安全性,将IFile中的方法去除,不在IFile声明,直接在子类中声明即可。

  接下来,创建一个Leaf(叶子结点),因为文件是不可再分的,所以File是叶子结点

 1 /*
 2  * 文件(leaf 叶子结点)
 3  */
 4 public class File implements IFile {
 5     private String name;
 6     
 7     public File(String name) {
 8         this.name = name;
 9     }
10     
11 
12     public void display() {
13         System.out.println(name);
14     }
15 
16     public List<IFile> getChild() {
17         return null;
18     }
19 
20 
21     public boolean add(IFile file) {
22         return false;
23     }
24 
25     public boolean remove(IFile file) {
26         return false;
27     }
28 
29 }

  然后继续创建Composite(文件夹),因为文件夹下面还可能有文件与文件夹,所以是composite

 1 public class Folder implements IFile{
 2     private String name;
 3     private List<IFile> children;
 4     
 5     public Folder(String name) {
 6         this.name = name;
 7         children = new ArrayList<IFile>();
 8     }
 9     
10     public void display() {
11         System.out.println(name);
12     }
13 
14     public List<IFile> getChild() {
15         return children;
16     }
17 
18     public boolean add(IFile file) {
19         return children.add(file);
20     }
21 
22     public boolean remove(IFile file) {
23         return children.remove(file);
24     }
25 }

  然后是客户端,用递归的形式把这个具有树形结构的对象遍历出来

 1 public class MainClass {
 2     public static void main(String[] args) {
 3         //C盘
 4         Folder rootFolder = new Folder("C:");
 5         //C盘下的目录一
 6         Folder folder1 = new Folder("目录一");
 7         //C盘下的文件一
 8         File file1 = new File("文件一.txt");
 9         
10         rootFolder.add(folder1);
11         rootFolder.add(file1);
12         
13         //目录一下的目录二
14         Folder folder2 = new Folder("目录二");
15         //目录一下的文件二
16         File file2 = new File("文件二.txt");
17         folder1.add(folder2);
18         folder1.add(file2);
19         
20         //目录二下的目录三
21         Folder folder3 = new Folder("目录三");
22         //目录二下的文件三
23         File file3 = new File("文件三.txt");
24         folder2.add(folder3);
25         folder2.add(file3);
26         
27         displayTree(rootFolder,0);
28         
29     }
30     
31     public static void displayTree(IFile rootFolder, int deep) {
32         for(int i = 0; i < deep; i++) {
33             System.out.print("--");
34         }
35         //显示自身的名称
36         rootFolder.display();
37         //获得子树
38         List<IFile> children = rootFolder.getChild();
39         //遍历子树
40         for(IFile file : children) {
41             if(file instanceof File) {
42                 for(int i = 0; i <= deep; i++) {
43                     System.out.print("--");
44                 }
45                 file.display();
46             }else {
47                 displayTree(file,deep + 1);
48             }
49         }
50     }
51 }

  这样子,就把,这棵树遍历了出来。

  优缺点

    优点:

    1) 使客户端调用简单,客户端可以一致的使用组合结构或其中单个对象,用户就不必关心自己处理的是单个对象还是整个组合结构,这就简化了客户端代码。

    2)更容易在组合体内加入对象部件. 客户端不必因为加入了新的对象部件而更改代码。这一点符合开闭原则的要求,对系统的二次开发和功能扩展很有利!

    缺点:

    组合模式不容易限制组合中的构件。

  总结

  组合模式是一个应用非常广泛的设计模式,它本身比较简单但是很有内涵,掌握了它对你的开发设计有很大的帮助。

 

 

 

转载于:https://www.cnblogs.com/Renyi-Fan/p/11031928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值