Home >Java >javaTutorial >What are the implementation methods of decorator pattern in java framework?
The decorator pattern can be implemented in the Java framework in the following three ways: 1. Interface decorator: Create a new class that implements a specific interface and add behavior to an existing class. 2. Class decorator: Create new classes that inherit from existing classes and add new behaviors. 3. Annotation decorator: Use annotations to extend existing classes and add behaviors through custom annotations. This pattern is widely used in logging frameworks, such as filtering logs or adding timestamps.
How to implement the decorator pattern in the Java framework
The decorator pattern is a structural design pattern that allows Dynamically add new functions to objects without modifying the original class. In the Java framework, the decorator pattern has the following implementation methods:
1. Interface decorator
This implementation method implements a specific interface by creating an A new class that extends an existing class. New classes can add additional behavior to existing classes without modifying the existing class's code.
public interface Shape { void draw(); } public class Rectangle implements Shape { @Override public void draw() { System.out.println("Drawing a rectangle"); } } public class DecoratedRectangle implements Shape { private Shape shape; public DecoratedRectangle(Shape shape) { this.shape = shape; } @Override public void draw() { shape.draw(); System.out.println("Adding extra decoration to rectangle"); } }
2. Class Decorator
This implementation extends an existing class by creating a new class that inherits from the existing class and adds new the behavior of.
public class Shape { public void draw() { System.out.println("Drawing a shape"); } } public class DecoratedShape extends Shape { @Override public void draw() { super.draw(); System.out.println("Adding extra decoration to shape"); } }
3. Annotation decorator
This implementation uses Java annotations to extend existing classes. New behavior can be added by creating a custom annotation and then applying the annotation to an existing class.
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Decorated { String value(); } public class Shape { public void draw() { System.out.println("Drawing a shape"); } } @Decorated("rectangle") public class Rectangle extends Shape { @Override public void draw() { super.draw(); System.out.println("Drawing a rectangle"); } }
Practical Case
In the logging framework, the decorator pattern is used to add additional functionality to the logs, such as filtering, adding timestamps, or sending the logs to Different destinations. For example, the following uses the Log4j framework to show a practical case of the decorator pattern:
import org.apache.log4j.Logger; import org.apache.log4j.Layout; import org.apache.log4j.Appender; import org.apache.log4j.varia.LevelMatchFilter; public class MyLoggingAppender extends AppenderSkeleton { private Appender appender; public MyLoggingAppender(Layout layout, Appender appender) { super(layout); this.appender = appender; } @Override public void close() { appender.close(); } @Override public boolean requiresLayout() { return false; } @Override protected void append(LoggingEvent event) { if (LevelMatchFilter()) { appender.doappend(new LoggingEvent(event.getLoggerName(), event.getLevel(), event.getMessage(), event.getThreadName(), event.getTimeStamp(), event.getLocationInfo(), event.getThrowableInformation(), event.getNDC(), event.getMDCCopy())); } } }
In this case, the MyLoggingAppender
class is a decorator that extends the Appender class by adding filtering functionality . It delivers log events to the original Appender, but only if the log events match the specified filter.
In short, by using the decorator pattern, you can extend the functionality of Java objects without modifying the original class, thereby improving the scalability and flexibility of the code.
The above is the detailed content of What are the implementation methods of decorator pattern in java framework?. For more information, please follow other related articles on the PHP Chinese website!