Design Patterns Combination Pattern Notes

illustrate

Record the writing method of learning design pattern-combination pattern. The JDK version used is version 1.8.

Composite

Intent : Combine objects into a tree structure to represent a "part-whole" hierarchy. Composite enables users to use single objects and composite objects consistently.
Structure :
Insert image description here

in:

  • Component declares an interface for the objects in the composition; implements the default behavior of the interface common to all classes where appropriate; declares an interface for accessing and managing the subcomponents of the Component; (optional) defines an interface in the recursive structure for access a parent component and implement it where appropriate.
  • Leaf represents the leaf node object in the combination, and the leaf node has no child nodes; the behavior of the primitive object is defined in the combination.
  • Composite defines the behavior of those components that have subcomponents; stores subcomponents; and implements operations related to subcomponents in the Component interface.
  • Client manipulates the objects of composite components through the Component interface.

applicability:

  • Want to represent a part-whole hierarchy of objects.
  • It is hoped that users will ignore the difference between composite objects and single objects, and users will use all objects in the composite structure uniformly.

Usage scenarios:
The combination mode is born in response to the tree structure, so the usage scenario of the combination mode is where the tree structure appears. For example: file directory display, multi-level directory presentation and other tree structure data operations.

Table of contents

Insert image description here

Combination pattern example class diagram

Insert image description here
Insert image description here

Use this UML class diagram to implement the composition pattern example.

Menu component abstract class

package com.example.deesign_patterns.composite;

//菜单组件类
public abstract class MenuComponent {
    
    

    //菜单组件的名称
    protected String name;
    //菜单组件的层级
    protected int level;

    //添加子菜单
    public void add(MenuComponent menuComponent){
    
    
        throw new UnsupportedOperationException();
    }

    //移除子菜单
    public void remove(MenuComponent menuComponent){
    
    
        throw new UnsupportedOperationException();
    }

    //获取指定的子菜单
    public MenuComponent getChild(int index){
    
    
        throw new UnsupportedOperationException();
    }

    //获取菜单或者菜单项的名称
    public String getName(){
    
    
        return name;
    }

    //打印菜单名称的方法(包含子菜单和子菜单项)
    public abstract void print();
}

Menu type

package com.example.deesign_patterns.composite;

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

//菜单类
public class Menu extends MenuComponent{
    
    

    //菜单可以有多个子菜单或者子菜单项
    private List<MenuComponent> menuComponentList=new ArrayList<MenuComponent>();

    //构造方法
    public Menu(String name,int level) {
    
    
        this.name=name;
        this.level=level;
    }

    @Override
    public void add(MenuComponent menuComponent) {
    
    
        menuComponentList.add(menuComponent);
    }

    @Override
    public void remove(MenuComponent menuComponent) {
    
    
        menuComponentList.remove(menuComponent);
    }

    @Override
    public MenuComponent getChild(int index) {
    
    
        return menuComponentList.get(index);
    }

    @Override
    public void print() {
    
    
        //打印菜单名称
        for(int i=0;i<level;i++){
    
    
            System.out.print("--");
        }
        System.out.println(name);
        //打印子菜单或者子菜单项名称
        for(MenuComponent component:menuComponentList){
    
    
            component.print();
        }
    }
}

Menu item class

package com.example.deesign_patterns.composite;

//菜单项类
public class MenuItem extends MenuComponent{
    
    

    //构造方法
    public MenuItem(String name,int level) {
    
    
        this.name=name;
        this.level=level;
    }

    @Override
    public void print() {
    
    
        //打印菜单项的名称
        for(int i=0;i<level;i++){
    
    
            System.out.print("--");
        }
        System.out.println(name);
    }
}

Test class

package com.example.deesign_patterns.composite;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        //创建菜单树
        MenuComponent menu1=new Menu("菜单管理",2);
        menu1.add(new MenuItem("页面访问",3));
        menu1.add(new MenuItem("展开菜单",3));
        menu1.add(new MenuItem("编辑菜单",3));
        menu1.add(new MenuItem("删除菜单",3));
        menu1.add(new MenuItem("新增菜单",3));

        MenuComponent menu2=new Menu("权限配置",2);
        menu2.add(new MenuItem("页面访问",3));
        menu2.add(new MenuItem("提交保存",3));

        MenuComponent menu3=new Menu("角色管理",2);
        menu3.add(new MenuItem("页面访问",3));
        menu3.add(new MenuItem("新增角色",3));
        menu3.add(new MenuItem("修改角色",3));

        //创建一级菜单
        MenuComponent component=new Menu("系统管理",1);
        //将二级菜单添加到一级菜单
        component.add(menu1);
        component.add(menu2);
        component.add(menu3);

        //打印菜单名称(如果有子菜单一块打印)
        component.print();
    }
}

Insert image description here

benefit:

  • The combination mode can clearly define hierarchical complex objects, representing all or part of the object's hierarchy. It allows the client to ignore hierarchical differences and facilitate control of the entire hierarchical structure.
  • The client can consistently use a composite structure or a single object within it, without having to worry about whether it is a single object or the entire composite object, simplifying the client code.
  • It is very convenient to add new branch nodes and leaf nodes in the combination mode without any modification to the existing class library, which complies with the opening and closing principle.
  • The combination mode provides a flexible solution for the object-oriented implementation of tree structures. Through the recursive combination of leaf nodes and branch nodes, complex tree structures can be formed, but the control of the tree structure is very simple.

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/131350832