Pipeline design pattern comes in handy when we need to execute sequences of steps over an input (request), were each step must be handled by differente software logic.
For instance, imagine a sequence of steps needed to approve a credit, a sequence of tests scores checking before decide if an student go up a year, medical exams results, etc...
Let's dive into a simple credit approval imaginary flow using typescript. You can define your own logic but for the sake of simplicity i'll keep it simple.
For this we'll need the following domain:
a card issuer represented by the enum CardIssuer, a person (Person Interface) and a Credit domain class (credit interface), see bellow:
So with the basic model created let's define our Pipeline implementation, but before that i'll need a CreditCardEvaluation class simply to wrap our domain so handling all related information becomes easier.
Finally an interface that defines our handlers contract (the contract to handle a request), and the pipeline class.
Now we must define what we want to validate/check. For that i defined a basic credit validator (BasicCreditProcessor), a guarantee validator and a credit card message processor, classes are as follow:
Now that we have our structure, let's test it:
and the result is:
a second test:
with the result:
by using the pipeline design pattern we avoid having a single class with multiple responsabilities and adding more processors "step/executors" are as easy as writing them and placing them inside the pipeline. that is it.