Home > Java > javaTutorial > body text

Summarize and organize JAVA decorator patterns (detailed examples)

WBOY
Release: 2022-05-05 18:48:19
forward
2401 people have browsed it

This article brings you relevant knowledge about java, which mainly introduces related issues about design patterns. It mainly refers to the related content of decorator pattern without changing existing objects. In the case of structure, we can dynamically add some responsibility patterns to the object. I hope it will be helpful to everyone.

Summarize and organize JAVA decorator patterns (detailed examples)

## Recommended study: "

java video tutorial"

What is the decorator pattern

Definition of the Decorator pattern: refers to a pattern that dynamically adds some responsibilities to the object (that is, adds its additional functions) without changing the existing object structure. It belongs to the object Structural pattern.

Advantages

1. Decorators are a powerful supplement to inheritance and are more flexible than inheritance. They can dynamically extend functions for an object without changing the original object, that is, plug-in Ready to use

2. Different effects can be achieved by using unused decorative classes and the arrangement and combination of these decorative classes
3. The decorator mode fully complies with the opening and closing principle

Disadvantages

The decorator pattern will add many subclasses, and excessive use will increase the complexity of the program.

Knowledge Points

Normally, extending the functionality of a class is achieved using inheritance. However, inheritance has static characteristics and a high degree of coupling, and as extended functions increase, subclasses will expand. If you use composition relationships to create a wrapper object (that is, a decoration object) to wrap the real object and provide it with additional functionality while keeping the class structure of the real object unchanged, this is the goal of the decorator pattern. Let's analyze its basic structure and implementation method.

Implementation of Decorator Pattern

Case: Hei Xiaohu caught the Seven Heroes and even roasted Hongmao Abstract Component (Component) Role: Seven Heroes
ConcreteComponent role: Rainbow Cat
Abstract Decorator role: Add materials
Concrete Decorator role: Add salt and cumin.

Qixia

Qixia interface declares a barbecue abstract method

public interface Qi {
    void show();}
Copy after login
Hongmao

Hongmao class Implement the Qixia interface and implement the method of roasting rainbow cats

public class Hong implements Qi {

    @Override
    public void show() {
        System.out.println("烧烤一个虹猫");
    }}
Copy after login
Add ingredients

This is an abstract decorator that implements the Qixia interface and declares a Qixia Xia attribute, used to call Qixia's barbecue method

public class JiaLiao implements Qi {
    private Qi qi;

    JiaLiao() {
    }

    JiaLiao(Qi component) {
        this.qi = component;
    }

    @Override
    public void show() {
        qi.show();

    }}
Copy after login
add salt

Inherits the abstract class, and rewrites the barbecue method, adding a salt

public class Yan extends JiaLiao {
    private Qi qi;

    Yan() {
    }

    Yan(Qi qi) {
        super(qi);
    }

    @Override
    public void show() {
        super.show();
        add();
    }

    public void add() {
        System.out.println("加盐");
    }}
Copy after login
Add cumin

Inherited the abstract class, rewritten the barbecue method, and added a cumin

public class ZiRan extends JiaLiao {
    private Qi qi;

    ZiRan() {
    }

    ZiRan(Qi qi) {
        super(qi);
    }

    @Override
    public void show() {
        super.show();
        add();
    }

    public void add() {
        System.out.println("加孜然");
    }}
Copy after login
Test

newA rainbow cat, roast it.

Add some salt
Add some salt and cumin

public class Demo {
    public static void main(String[] args) {
        Qi qi = new Hong();
        qi.show();
        System.out.println();
        Qi qi1 = new Yan(qi);
        qi1.show();
        System.out.println();
        Qi qi2 = new ZiRan(qi1);
        qi2.show();
    }}
Copy after login

Summarize and organize JAVA decorator patterns (detailed examples)

Summary

Not only can you grill rainbow cats, but you can also grill them. For other Seven Heroes, you only need to add a class to implement the Seven Heroes interface.

Moreover, adding salt or cumin will not change the original meat quality of Hongmao.

Recommended study: "

java video tutorial"

The above is the detailed content of Summarize and organize JAVA decorator patterns (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template