Github source address

An overview of 23 design patterns

  • Java Language Design – An overview of 23 design patterns

Creation pattern

  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Mode
  • Prototype mode
  • Singleton

Structural mode

  • Facade Pattern
  • Adapter mode (Adapter)
  • Proxy mode
  • Composite mode
  • Flyweight Mode
  • Decorator pattern
  • Bridge mode (Bridge)

Behavioral pattern

  • Mediator Mode
  • Observer Model
  • Command mode
  • Iterator pattern (Iterator)
  • Template Method
  • Strategy Pattern
  • State mode
  • Memento Mode
  • Interpreter mode
  • Chain of Responsibility model
  • Visitor Pattern

define

The Decorator Pattern allows you to add new functionality to an existing object without changing its structure. This type of design pattern is a structural pattern that acts as a wrapper around existing classes.

This pattern creates a decorator class that wraps the original class and provides additional functionality while preserving the integrity of the class method signature.

Add some extra responsibilities to an object dynamically. Decorator patterns are more flexible in terms of adding functionality than subclassing.

Usage scenarios

Extend the functionality of a class.

2, dynamic increase function, dynamic cancel.

It’s basically for scalability.

When a class extends functionality, extending it by inheritance will result in a subclass explosion as more and more functionality is added.

The advantages and disadvantages

Advantages: The decorator class and the decorator class can develop independently without coupling with each other. The decorator pattern is an inherited replacement pattern, and the decorator pattern can dynamically extend the functionality of an implementation class.

Disadvantages: multi-layer decoration is more complex.

The sample

1. There is a duck. He learns to talk

public interface DuckStudy {
    void study(a);
}
Copy the code
class Duck implements DuckStudy {
    @Override
    public void study(a) {
        System.out.println("I know how to quack, quack, quack."); }}Copy the code

2. After a few days, you learned the fly function again, so you asked your friend to add this function. However, your friend worried about directly modifying the inside of the Duck, so he used decorator mode to achieve it

public abstract class Decorator implements DuckStudy {
    protected DuckStudy duckStudy;

    public Decorator(DuckStudy duckStudy) {
        this.duckStudy = duckStudy;
    }

    @Override
    public void study(a) { duckStudy.study(); }}Copy the code
class DuckDecorator extends Decorator{
    public DuckDecorator(DuckStudy duckStudy) {
        super(duckStudy);
    }

    @Override
    public void study(a) {
        super.study();
        // Now I have learned another function
        addFly();
    }

    private void addFly(a) {
        System.out.println("I learned a new skill -- I can fly."); }}Copy the code

3. Execution result

public class TestDemo {
    public static void main(String[] args) {
        Duck duck = new Duck();

        DuckDecorator duckDecorator = newDuckDecorator(duck); duckDecorator.study(); }}Copy the code
I learned a new skill - I can flyCopy the code