IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

大话设计模式之组合模式

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

组合模式

  将对象组合成树形结构以表示‘部分–整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性

84_1.png

涉及到的角色描述

  在组合模式结构图中包含如下几个角色:

● Component(抽象构件):它可以是接口或抽象类,为叶子构件和容器构件对象声明接口,在该角色中可以包含所有子类共有行为的声明和实现。在抽象构件中定义了访问及管理它的子构件的方法,如增加子构件、删除子构件、获取子构件等。

● Leaf(叶子构件):它在组合结构中表示叶子节点对象,叶子节点没有子节点,它实现了在抽象构件中定义的行为。对于那些访问及管理子构件的方法,可以通过异常等方式进行处理。

● Composite(容器构件):它在组合结构中表示容器节点对象,容器节点包含子节点,其子节点可以是叶子节点,也可以是容器节点,它提供一个集合用于存储子节点,实现了在抽象构件中定义的行为,包括那些访问及管理子构件的方法,在其业务方法中可以递归调用其子节点的业务方法。

使用场景

  当发现需求中体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象时,就可以考虑组合模式

透明方式与安全方式

  透明方式: 可以理解为在component中声明用来管理所有子对象的方法,其中包括add remove等。这样实现component接口的所有子类都具备了add和remove。这样做的好处就是叶节点和枝节点对于外界没有区别,他们具有完全一致的行为接口。但是问题也明显,因为leaf类本身不具备add

remove方法的功能,所有实现他们是没有意义的

  安全方式: 在component接口中不去声明add remove方法,那么子类的leaf也就不需要去实现它,而是在composite声明所有用来管理子类对象的方法,这样的好处不会出现上述透明方式的问题,但问题是 由于不够透明,所有树叶和树枝类将不具有相同的接口,客户端的调用需要做相应的

判断,带来了不便。

具体实现

  抽象构件

package com.chenpt.designModel.compositeModel;

/**
 * @Author: chen
 * @Description:
 * @Date: created in 2018/8/27
 * @Modified By:
 */
public interface AbstractFile {

    void add(AbstractFile c);
    void remove(AbstractFile c);
    AbstractFile getChild(int i);
    void operation();

}

 叶子节点1

package com.chenpt.designModel.compositeModel;

/**
 * @Author: chen
 * @Description:
 * @Date: created in 2018/8/27
 * @Modified By:
 */
public class ImageFile implements AbstractFile {

    String name;

    ImageFile(String name){
        this.name=name;
    }

    @Override
    public void add(AbstractFile component) {
        System.out.println("对不起,不支持该方法!");
    }

    @Override
    public void remove(AbstractFile component) {
        System.out.println("对不起,不支持该方法!");
    }

    @Override
    public AbstractFile getChild(int i) {
        System.out.println("对不起,不支持该方法!");
        return null;
    }

    @Override
    public void operation() {
        //模拟杀毒
        System.out.println("----对图像文件'" + name + "'进行杀毒");
    }
}

 叶子节点2

package com.chenpt.designModel.compositeModel;

/**
 * @Author: chen
 * @Description:
 * @Date: created in 2018/8/27
 * @Modified By:
 */
public class TextFile implements AbstractFile {

    String name;

    TextFile(String name){
        this.name=name;
    }

    @Override
    public void add(AbstractFile component) {
        System.out.println("对不起,不支持该方法!");
    }

    @Override
    public void remove(AbstractFile component) {
        System.out.println("对不起,不支持该方法!");
    }

    @Override
    public AbstractFile getChild(int i) {
        System.out.println("对不起,不支持该方法!");
        return null;
    }

    @Override
    public void operation() {
        //模拟杀毒
        System.out.println("----对文本文件'" + name + "'进行杀毒");
    }
}

 容器构件

package com.chenpt.designModel.compositeModel;

import java.util.ArrayList;

/**
 * @Author: chen
 * @Description:
 * @Date: created in 2018/8/27
 * @Modified By:
 */
public class Folder implements AbstractFile {

    private ArrayList<AbstractFile> fileList=new ArrayList<AbstractFile>();
    private String name;

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

    @Override
    public void add(AbstractFile c) {
        fileList.add(c);
    }

    @Override
    public void remove(AbstractFile c) {
        fileList.remove(c);
    }

    @Override
    public AbstractFile getChild(int i) {
        return fileList.get(i);
    }

    @Override
    public void operation() {
        System.out.println("****对文件夹'" + name + "'进行杀毒");  //模拟杀毒

        //递归调用成员构件的killVirus()方法
        for(AbstractFile obj : fileList) {
            obj.operation();
        }
    }
}

 客户端

package com.chenpt.designModel.compositeModel;

/**
 * @Author: chen
 * @Description:
 * @Date: created in 2018/8/27
 * @Modified By:
 */
public class MainTest {


    public static void main(String[] args){
        //针对抽象构件编程
        AbstractFile file1,file2,file3,folder1,folder2,folder3;

        folder1 = new Folder("chenpt的资料");
        folder2 = new Folder("图像文件");
        folder3 = new Folder("文本文件");

        file1 = new ImageFile("小龙女.jpg");
        file2 = new ImageFile("张无忌.gif");
        file3 = new TextFile("九阴真经.txt");

        folder2.add(file1);
        folder2.add(file2);
        folder3.add(file3);
        folder1.add(folder2);
        folder1.add(folder3);

        folder1.operation();

    }

}
//执行结果
****对文件夹'chenpt的资料'进行杀毒
****对文件夹'图像文件'进行杀毒
----对图像文件'小龙女.jpg'进行杀毒
----对图像文件'张无忌.gif'进行杀毒
****对文件夹'文本文件'进行杀毒
----对文本文件'九阴真经.txt'进行杀毒

  

 

 

 

 

文章永久链接:https://tech.souyunku.com/?p=39880


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(88) 打赏



未经允许不得转载:搜云库技术团队 » 大话设计模式之组合模式

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367