Combined pattern decorator pattern

Get into the habit of writing together! This is the 12th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Combination mode

The Composite Pattern, also known as the Part Whole Pattern, is used to treat a group of similar objects as a single object. The composition pattern composes objects according to a tree structure, which is used to represent parts and whole hierarchies. This type of design pattern is a structural pattern that creates a tree structure of groups of objects.

This pattern creates a class that contains its own group of objects. This class provides a way to modify the same group of objects.

We use the following example to demonstrate the usage of the combined pattern. The example demonstrates the hierarchy of employees in an organization.

Role: Combine objects into a tree structure to represent a "part-whole" hierarchy. The composite mode allows users to use individual objects and composite objects consistently.

For example, the department tree structure,

public class Dept {
   private String name;
   private String parentId;
   private List<Dept> children;
   }
复制代码

CascaderCascading selection structure in element

const options = [
  {
    value: 'guide',
    label: 'Guide',
    children: [
      {
        value: 'disciplines',
        label: 'Disciplines',
        children: [
          {
            value: 'consistency',
            label: 'Consistency',
          },
          {
            value: 'feedback',
            label: 'Feedback',
          },
          {
            value: 'efficiency',
            label: 'Efficiency',
          },
          {
            value: 'controllability',
            label: 'Controllability',
          },
        ],
      },
      ]
  }
复制代码

Decorator pattern

The Decorator Pattern allows adding new functionality to an existing object without changing its structure. This type of design pattern is a structural pattern, which acts as a wrapper around an existing class.

This pattern creates a decorated class that wraps the original class and provides additional functionality while maintaining the integrity of the class's method signatures.

Role: Dynamically add some additional responsibilities to an object. The decorator pattern is more flexible than subclassing in terms of adding functionality.

Usually the decoration class contains an object of the original class, which is equivalent to a wrapper class, which is an extension of the function of the original class

The stream InputStream in jdk is a typical decorator pattern, and its subclasses have

  • ByteArrayInputStream

  • FileInputStream

  • PipedInputStream

  • SequenceInputStream

  • FilterInputStream

    public class FilterInputStream extends InputStream {
        // 包含了一个原类对象
        protected volatile InputStream in;
    }
    复制代码

    Spring Wrapper

    BeanWrapperIt is a wrapper class interface, and its implementation class BeanWrapperImplreceives an Object object for wrapping processing.

    public BeanWrapperImpl(Object object) {
        super(object);
      }
    ​
      /**
       * Create a new BeanWrapperImpl, wrapping a new instance of the specified class.
       * @param clazz class to instantiate and wrap
       */
      public BeanWrapperImpl(Class<?> clazz) {
        super(clazz);
      }
    ​
    复制代码

Decorative mode is like putting on clothes, wrapping and expanding layer by layer.

\

Guess you like

Origin juejin.im/post/7087488256836648991