Pipes-and-Filters
This post is a cross-post from www.ModernesCpp.com.
The Pipes-and-Filters architecture pattern describes the structure of systems that process data streams.
The Pipes-and-Filters pattern is similar to the Layers Pattern. The idea of the Layers Pattern is to structure the system in layers so that higher layers are based on the services of lower layers. The Pipes-and-Filters naturally extend the Layers Pattern, using the layers as filters and the data flow as pipes.
Pipes-and-Filters
Purpose
Solution
Structure
Filter
Pipe
Data Source
Data Sink
The most interesting part about the Pipes-and-Filter is the data flow.
Data Flow
There are several ways to control the data flow.
Push Principle
Pull Principle
Mixed Push/Pull Principle
Active Filters as Independent Processes
Modernes C++ Mentoring
Stay informed: Subscribe.
Example
The most prominent example of the Pipes-and-Filters Pattern is the UNIX Command Shell.
Unix Command Shell
领英推荐
Here are the steps of the pipeline:
Finally, here is the classic of command line processing using pipes from Douglas Mcllroy.
tr -cs A-Za-z '\n' |
tr A-Z a-z |
sort |
uniq -c |
sort -rn |
sed ${1}q
If you want to know what this pipeline does, read the full story behind it in the article "More shell, less egg".
Thanks to the ranges library in C++20, the Pipes-and-Filters Pattern is directly supported in C++.
Ranges
The following program firstTenPrimes.cpp displays the first ten primes starting with 1000.
// firstTenPrimes.cpp
#include <iostream>
#include <ranges>
#include <vector>
bool isPrime(int i) {
for (int j = 2; j * j <= i; ++j){
if (i % j == 0) return false;
}
return true;
}
int main() {
std::cout << '\n';
auto odd = [](int i){ return i % 2 == 1; };
auto vec = std::views::iota(1'000) | std::views::filter(odd) //(1)
| std::views::filter(isPrime) //(2)
| std::views::take(10) //(3)
| std::ranges::to<std::vector>(); //(4)
for (auto v: vec) std::cout << v << " ";
}
The data source (std::views::iota(1'000))?creates the natural number, starting with 1000. First, the odd numbers are filtered out (line 1), and then the prime numbers (line 2). This pipeline stops after ten values (line 3) and pushes the elements onto the std::vector (line 4). The convenient function std::ranges::to creates a new range (line 4). This function is new with C++23. Therefore, I can only execute the code with the newest windows compiler on the compiler explorer.
Pros and Cons
I use in my following comparison the term universal interface. This means all filters speak the same language, such as xml or jason.
Pros
Cons
What's Next?
The Broker structures distributed software systems that interact with remote service invocations. It is responsible for coordinating the communication, its results, and exceptions. In my next post, I will dive deeper into the architectural pattern Broker.
?
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dr?ge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschl?ger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Dominik Vo??ek, and Rob North.
?
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.
My special thanks to Embarcadero, PVS-Studio, and Tipi.build.
Seminars
I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
Bookable (Online)
German
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
New
Contact Me