Home > Java > javaTutorial > What are the principles and implementation methods of the decoration pattern in Java design patterns?

What are the principles and implementation methods of the decoration pattern in Java design patterns?

WBOY
Release: 2023-04-22 10:52:08
forward
1130 people have browsed it

1. What is the decoration mode

When we want to improve or enhance the functionality of an object, but do not want to inherit the class and create subclasses, we can consider using the decoration mode.

Using decoration mode to enhance a certain function of an object is more flexible than generating a subclass and then enhancing it.

2. How to implement

1) Abstract component: Define an abstract class that defines the method that the "decorator" needs to be "decorated"

2 ) Concrete component: a subclass of abstract component. Become a "decorated class"

3) Decorative component: It is also a subclass of abstract component. This class holds a reference to the "decorator" and is used to call the decorator's methods. It can be an abstract class or a non-abstract class.

4) Concrete decoration class: It is a subclass of decoration component, called "decorator"

3. Code implementation

1) Abstract component

/**
 * 抽象组件,定义了被装饰类需要装饰的方法
 */
public abstract class ICake {
    /**
     * 需要装饰的方法
     * @return
     */
    public abstract int getPrice();
}
Copy after login

2) Decorated person

/**
 * 被装饰者,是抽象组件的一个子类,定义需要被装饰的方法
 */
public class Cake extends ICake {
    /**
     * 被装饰类中,装饰方法的基本实现
     * @return
     */
    @Override
    public int getPrice() {
        System.out.println("getPrice");
       return 100;
    }
}
Copy after login

3) Decoration class

/**
 * 装饰类,也是抽象组件的一个子类。
 * 可以是抽象类,也可以是非抽象类。
 */
public abstract class CakeDecorator extends ICake {
    /**
     * 被装饰类的引用,需要在装饰类中,调用到被装饰类的方法。
     */
    ICake cake;
    public CakeDecorator(ICake cake){
        this.cake = cake;
    }
}
Copy after login

4) Specific decoration

/**
 * 具体装饰类
 */
public class BlueBerryCake extends CakeDecorator {
    public BlueBerryCake(ICake cake) {
        super(cake);
    }
    /**
     * 被装饰的方法
     * 在调用到被装饰类的基础方法后,还要调用装饰类要增强的方法
     * @return
     */
    @Override
    public float getPrice(){
        return cake.getPrice()+elsePrice();
    }
    /**
     * 增强的方法
     * @return
     */
    private float elsePrice(){
        System.out.println("else getPrice");
        return 50;
    }
}
Copy after login

5) Test class

public class TestDecorator {
    public static void main(String args[]){
        ICake cake = new Cake();
        CakeDecorator decorater = new BlueBerryCake(cake);
        float price = decorater.getPrice();
        System.out.println(price);
    }
}
Copy after login

4 .Expansion

1) The enhancement of a certain method can also be achieved by using subclass inheritance. Compared with implementing through decoration mode, what are the advantages of decoration mode?

The decorator and the decorator are loosely coupled. The decoration class only depends on the abstract component. The decoration class only needs to know that the decorated class is a subclass of the abstract component, without knowing which subclass it is.

The decoration mode satisfies the "open-close principle". You can add a decoration class for the component without modifying the specific component.

5.JavaIo and decoration mode

Many classes in the java.IO package use the decoration mode.

Reader

FileReader

BufferdReader

The above is the detailed content of What are the principles and implementation methods of the decoration pattern in Java design patterns?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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