"Combination Mode" of Design Patterns

What is composite mode

Composite pattern is a structural design pattern that combines objects into a tree structure to represent a "part-whole" hierarchy. The composite mode allows the client to handle single objects and composite objects uniformly, so that the client does not need to distinguish the types of objects, which simplifies the code of the client.

Why use composite mode

Composite mode has the following advantages:

  • Simplify client code: Since the combination mode can handle single objects and composite objects uniformly, the client does not need to care about the type of the object, making the client code more concise.
  • Support recursive operation: Combination mode can use recursion to traverse the entire tree structure, so as to realize the operation on the entire structure.
  • Easy to extend: It is very easy to add new components to the tree structure without changing the existing class structure.

Where is it used in work? Taking Android as an example

In Android, the composition pattern can be used to create UI interface. For example, we can use the combination mode to create an Activity layout, which includes components such as TextView, Button, and ImageView. These components can be combined into a whole to form a layout, and the entire layout can be traversed using recursion.

Design ideas

In the combination mode, we abstract all components into an abstract component class, such as the View class in Android. This class has two subclasses: leaf component and container component. Among them, the leaf component is the smallest unit that cannot contain other components, such as TextView in Android, and the container component can contain other components, such as ViewGroup in Android.

Some common methods are defined in the abstract component class, such as adding sub-View, deleting sub-View, obtaining sub-View, etc. These methods implement different operations for leaf components and container components.

Code

Abstract component class:

public abstract class View {
    
    
    private String name;

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

    public abstract void addView(View view);
    public abstract void removeView(View view);
    public abstract View getChildView(int index);
    public abstract void draw();
}

Leaf component class:

public class TextView extends View {
    
    

    public TextView(String name) {
    
    
        super(name);
    }

    @Override
    public void addView(View view) {
    
    
        // 叶子组件不能添加其他组件
    }

    @Override
    public void removeView(View view) {
    
    
        // 叶子组件不能删除其他组件
    }

    @Override
    public View getChildView(int index) {
    
    
        // 叶子组件没有子View
        return null;
    }

    @Override
    public void draw() {
    
    
        // 绘制TextView
    }
}

Container component class:

public class ViewGroup extends View {
    
    
    private List<View> views = new ArrayList<>();

    public ViewGroup(String name) {
    
    
        super(name);
    }

    @Override
    public void addView(View view) {
    
    
        views.add(view);
    }

    @Override
    public void removeView(View view) {
    
    
        views.remove(view);
    }

    @Override
    public View getChildView(int index) {
    
    
        return views.get(index);
    }

    @Override
    public void draw() {
    
    
        // 绘制ViewGroup
        for (View view : views) {
    
    
            view.draw();
        }
    }
}

Example of use:

public class Client {
    
    
    public static void main(String[] args) {
    
    

        // 创建叶子组件
        View textView = new TextView("TextView");

        // 创建容器组件
        ViewGroup viewGroup = new ViewGroup("ViewGroup");
        viewGroup.addView(textView);

        // 执行容器组件操作
        viewGroup.draw();
    }
}

Output result:

绘制ViewGroup
绘制TextView

Summarize

Composite mode is a common design mode for processing tree structures. It can handle leaf objects and container objects uniformly, and supports recursive operations. In Android, the composite pattern can be used to create UI interfaces to achieve complex layouts. At design time, we need to abstract common component classes and operations, and implement different operations for leaf components and container components.

Guess you like

Origin blog.csdn.net/weixin_45112340/article/details/129851999