如何在Spring Boot中实现Decorator模式 [英] How to implement Decorator pattern in Spring Boot
问题描述
我知道如何在没有Spring的情况下实现和使用装饰器模式.
I know how to implement and use a decorator pattern without Spring.
由于采用这种模式,您可以自己控制组件的创建过程,并且可以执行动态行为添加.
Because in this pattern you yourself control the process of creating components and you can perform dynamic behavior adding.
以下是不使用Spring的实现示例:
Below is an example of implementation without using Spring:
public class SimpleDecoratorApp {
public static void main(String[] args) {
SimplePrinter simplePrinter = new SimplePrinter();
Printer decorated = new UpperCasePrinterDecorator(
new AddAsterisksPrinterDecorator(simplePrinter)
);
decorated.print("hello"); // *** HELLO ***
}
}
interface Printer {
void print(String msg);
}
class SimplePrinter implements Printer {
@Override
public void print(String msg) {
System.out.println(msg);
}
}
abstract class PrinterDecorator implements Printer {
protected Printer printer;
public PrinterDecorator(Printer printer) {
this.printer = printer;
}
}
class UpperCasePrinterDecorator extends PrinterDecorator {
public UpperCasePrinterDecorator(Printer printer) {
super(printer);
}
@Override
public void print(String msg) {
String s = msg.toUpperCase();
this.printer.print(s);
}
}
class AddAsterisksPrinterDecorator extends PrinterDecorator {
public AddAsterisksPrinterDecorator(Printer printer) {
super(printer);
}
@Override
public void print(String msg) {
msg = "*** " + msg + " ***";
this.printer.print(msg);
}
}
我对如何实现相同的示例感兴趣,但需要借助Spring bean.
I am interested in how to implement the same example but with the help of spring beans.
因为我不太了解如何灵活地包装任意数量的装饰器.
Because I don’t quite understand how to maintain flexibility in the ability to simply wrap with any number of decorators.
因为据我所知-它将固定实现在某个单独的组件中,因此我将不得不用我需要的装饰器组合来创建数十个各种单独的组件.
Because as I understand it - it will be implemented fixed in some separate component and I will have to create dozens of various separate components with the combinations of decorators I need.
推荐答案
我还不太了解您在这里遇到的实际问题,但是我还是会尝试的.
假设您有这些课程
I haven't really understood what is your actual problem here, but I'll try anyway.
Say you have these classes
UpperCasePrinterDecorator
LowerCasePrinterDecorator
AddAsterisksPrinterDecorator
每一个都需要一个 Printer
的实例,可以说它是作为Spring @Component
提供的.要将每个装饰器用作Spring Bean,您需要对其进行注册.
Each of these require an instance of a Printer
, which, let's say is provided as a Spring @Component
. To use each decorator as Spring Bean you need to register it.
@Bean
@Qualifier("upperCase")
PrinterDecorator upperCasePrinterDecorator(final Printer printer) { // Injected automatically
return new UpperCasePrinterDecorator(printer);
}
@Bean
@Qualifier("lowerCase")
PrinterDecorator lowerCasePrinterDecorator(final Printer printer) {
return new LoweCasePrinterDecorator(printer);
}
@Bean
@Qualifier("asterisk")
PrinterDecorator addAsterisksPrinterDecorator(final Printer printer) {
return new AddAsterisksPrinterDecorator(printer);
}
然后您可以使用 @Qualifier
批注获取正确的一个 @Autowired
You can then use the @Qualifier
annotation to get the right one @Autowired
@Autowired
@Qualifier("lowerCase")
private PrinterDecorator printer;
这篇关于如何在Spring Boot中实现Decorator模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!