docs: update intercepting filter

This commit is contained in:
Ilkka Seppälä committed 2024-05-26 11:32:24 +03:00
1 parent a3080c00f9
commit d991970439
1 file changed
+19 -15
+19 -15
View File
@@ -17,7 +17,7 @@ The Intercepting Filter pattern is intended to provide a pluggable framework for
## Explanation ## Explanation
Real-world Example Real-world example
> Consider entering a secure office building where you pass through several checkpoints: a security desk checks your ID, a metal detector ensures safety, and a registration desk logs your visit. Each checkpoint acts like a filter in the Intercepting Filter pattern, processing and validating your entry step-by-step, similar to how filters handle different aspects of web requests and responses in a software system. > Consider entering a secure office building where you pass through several checkpoints: a security desk checks your ID, a metal detector ensures safety, and a registration desk logs your visit. Each checkpoint acts like a filter in the Intercepting Filter pattern, processing and validating your entry step-by-step, similar to how filters handle different aspects of web requests and responses in a software system.
@@ -31,7 +31,7 @@ Wikipedia says
## Programmatic Example ## Programmatic Example
The Intercepting Filter design pattern is a Java EE pattern that creates pluggable filters to process common services in a standard manner without requiring changes to core request processing code. These filters can perform tasks such as authentication, logging, data compression, and encryption. Intercepting Filter is a pattern that creates pluggable filters to process common services in a standard manner without requiring changes to core request processing code. These filters can perform tasks such as authentication, logging, data compression, and encryption.
In the provided code, we can see an example of the Intercepting Filter pattern in the `App`, `FilterManager`, `Client`, and various `Filter` classes. In the provided code, we can see an example of the Intercepting Filter pattern in the `App`, `FilterManager`, `Client`, and various `Filter` classes.
@@ -95,10 +95,18 @@ public interface Filter {
void execute(String request); void execute(String request);
} }
public class NameFilter implements Filter { public class NameFilter extends AbstractFilter {
public void execute(String request) {
// Implementation details... @Override
} public String execute(Order order) {
var result = super.execute(order);
var name = order.getName();
if (name == null || name.isEmpty() || name.matches(".*[^\\w|\\s]+.*")) {
return result + "Invalid name! ";
} else {
return result;
}
}
} }
// Other Filter implementations... // Other Filter implementations...
@@ -106,10 +114,6 @@ public class NameFilter implements Filter {
In this example, the `App` class sets up a `FilterManager` with various filters and assigns it to a `Client`. When the `Client` sends a request, the `FilterManager` applies all the filters to the request. This is a basic example of the Intercepting Filter pattern, where common processing tasks are encapsulated in filters and applied to requests in a standard manner. In this example, the `App` class sets up a `FilterManager` with various filters and assigns it to a `Client`. When the `Client` sends a request, the `FilterManager` applies all the filters to the request. This is a basic example of the Intercepting Filter pattern, where common processing tasks are encapsulated in filters and applied to requests in a standard manner.
## Class diagram
![Intercepting Filter](./etc/intercepting-filter.png "Intercepting Filter")
## Applicability ## Applicability
Use the Intercepting Filter pattern when Use the Intercepting Filter pattern when
@@ -119,8 +123,8 @@ Use the Intercepting Filter pattern when
## Tutorials ## Tutorials
* [Introduction to Intercepting Filter Pattern in Java](https://www.baeldung.com/intercepting-filter-pattern-in-java) * [Introduction to Intercepting Filter Pattern in Java (Baeldung)](https://www.baeldung.com/intercepting-filter-pattern-in-java)
* [TutorialsPoint - Intercepting Filter](http://www.tutorialspoint.com/design_pattern/intercepting_filter_pattern.htm) * [Design Pattern - Intercepting Filter Pattern (TutorialsPoint)](http://www.tutorialspoint.com/design_pattern/intercepting_filter_pattern.htm)
## Known Uses ## Known Uses
@@ -144,11 +148,11 @@ Trade-offs:
## Related Patterns ## Related Patterns
[Decorator](https://java-design-patterns.com/patterns/decorator/): Filters in the Intercepting Filter pattern can be considered as decorators that add additional responsibilities to request handling. They modify the request/response without altering their fundamental behavior. * [Decorator](https://java-design-patterns.com/patterns/decorator/): Filters in the Intercepting Filter pattern can be considered as decorators that add additional responsibilities to request handling. They modify the request/response without altering their fundamental behavior.
[Chain of Responsibility](https://java-design-patterns.com/patterns/chain-of-responsibility/): Filters are linked in a chain, where each filter processes the request or response and optionally passes it to the next filter in the chain, similar to how responsibilities are passed along in the Chain of Responsibility pattern. * [Chain of Responsibility](https://java-design-patterns.com/patterns/chain-of-responsibility/): Filters are linked in a chain, where each filter processes the request or response and optionally passes it to the next filter in the chain, similar to how responsibilities are passed along in the Chain of Responsibility pattern.
## Credits ## Credits
* [Core J2EE Patterns: Best Practices and Design Strategies](https://amzn.to/4cAbDap)
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI) * [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR) * [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
* [Core J2EE Patterns: Best Practices and Design Strategies](https://amzn.to/4cAbDap)