Filter
A filter takes a message from its input, applies a transformation, and sends the transformed message as output. Filters implement simple transformation or transfer functions to perform operations such as: Conversion, enrichment, filtering, batching, or consolidation.
Pipes
Pipes transport and buffer (data integrity/synchronization) messages between filters.
Key Points
1.The single-filter configuration implements the transformation by using one specialized component. The one hop that exists between input and output and the elimination of the interfilter communication translate into low latency and overhead.
2. The key tradeoffs in choosing between a combination of generic filters and a single specialized filter are reusability and performance.
3. Filters must not be dependent on other filters.
Simple Implementation
public interface IFilter{ T Execute(T input); } public class Pipeline { private readonly List< IFilter > _filters = new List< IFilter >(); public void Register(IFilter filter) { _filters.Add(filter); } public T Execute(T input) { T current = input; try { foreach (IFilter filter in _filters) { current = filter.Execute(current); } } catch (Exception exception) { //Add error handling logic EventLog.WriteEntry(GetType().FullName,exception.Message); throw; } return current; } }
public class LowerCaseString : Filter1BaseClass,IFilter { protected override string Process(string input) { return input.ToLower(); } } public class UpperCaseString : Filter1BaseClass ,IFilter { protected override string Process(string input) { return input.ToUpper(); } }
var input = "Test"; Console.WriteLine("Input: {0}", input); Pipelinepipeline = new Pipeline (); pipeline.Register(new LowerCaseString()); pipeline.Register(new UpperCaseString()); string output = pipeline.Execute(input); Console.WriteLine("Output: {0}", output);
No comments:
Post a Comment