一起走进组合模式

一.介绍

组合模式(Composite Pattern)属于结构型模式。组合模式又叫作部分整体模式,它是一种将对象组合成树状层次结构的模式,用来表示整体-部分的关系,使用户对单个对象和组合对象具有一致的访问性。组合模式有透明方式安全方式两种实现方式
在这里插入图片描述

二.UML类图

1.透明方式

  • 抽象节点中定义了规范,客户端无需区别叶子节点和树枝节点,使用方便
  • 叶子节点本来无add、remove、getChild方法,但是因为抽象中定义了规范,所以必须实现它们,会带来一些安全性问题
    在这里插入图片描述

2.安全方式

  • 客户端需要区别叶子节点和树枝节点,使用不方便
  • 叶子节点无add、remove、getChild方法,不存在安全性问题
    在这里插入图片描述
三.具体代码

业务代码

//抽象节点
public abstract class Component {
    abstract void add(Component component);
    abstract void remove(Component component);
    abstract Component getChild(int i);
    abstract void operation();
}

//叶子节点
class Leaf extends Component{

    private String name;

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

    @Override
    void add(Component component) {}

    @Override
    void remove(Component component) {}

    @Override
    Component getChild(int i) {
        return null;
    }

    @Override
    void operation() {
        System.out.print(name);
    }
}

//树枝节点
class Composite extends Component{
    private ArrayList<Component> children = new ArrayList<>();

    @Override
    void add(Component component) {
       children.add(component);
    }

    @Override
    void remove(Component component) {
        children.remove(component);
    }

    @Override
    Component getChild(int i) {
        return children.get(i);
    }

    @Override
    void operation() {
        children.forEach(Component::operation);
    }
}

客户端

public class Client {
    public static void main(String[] args) {
        Component level1 = new Composite();
        level1.add(new Leaf("1"));
        level1.add(new Leaf("2"));
        Component level2 = new Composite();
        level2.add(new Leaf("2.1"));
        level1.add(level2);
        level1.operation();
    }
}
四.使用场景
  • 需要表示一个对象整体与部分的层次结构
  • 要求对用户隐藏组合对象与单个对象的不同,用户可以使用统一的接口操作组合结构中的所有对象
  • 组织机构树
  • 文件/文件夹
五.优点
  • 简化了客户端代码(客户端一致地处理单个对象和组合对象)
  • 符合开闭原则(新增具体实现类不需要更改源代码)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
fastjson的安全模式可以通过以下几种方式进行设置。首先,可以在代码中配置`ParserConfig.getGlobalInstance().setSafeMode(true)`来开启安全模式。例如,在应用程序的入口处添加以下代码: ```java public class DemoApplication { public static void main(String\[\] args) { ParserConfig.getGlobalInstance().setSafeMode(true); SpringApplication.run(DemoApplication.class, args); } } ``` 需要注意的是,如果使用`new ParserConfig`的方式,需要注意单例处理,否则可能会导致性能下降和内存泄漏。 另外一种方式是通过JVM启动参数来设置安全模式,可以在启动时加上`-Dfastjson.parser.safeMode=true`参数来开启安全模式。这样在应用程序运行时,fastjson会自动启用安全模式。 需要注意的是,安全模式的开启会完全禁用autoType功能,确保了fastjson在反序列化时不会受到恶意代码的攻击。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [Fastjson开启安全模式5种方法(VIP典藏版)](https://blog.csdn.net/libusi001/article/details/117710826)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [fastjson 安全漏洞](https://blog.csdn.net/regrethh/article/details/107381977)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值