Created
December 20, 2013 09:04
-
-
Save mrcnkoba/8052236 to your computer and use it in GitHub Desktop.
prosty pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace pipeandfilters | |
{ | |
internal class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var str = "zOSia"; | |
var transformer = new StringTransformer(str); | |
transformer.Register(new TransformToUpper()).Register(new TransformToLower()); | |
Console.WriteLine(transformer.Run()); | |
// or | |
var concretePipelineChain = new DoWorkOnStringPipeline(str); | |
Console.WriteLine(concretePipelineChain.Run()); | |
} | |
} | |
public interface ITransform<T> | |
{ | |
T Execute(T @object); | |
} | |
public abstract class StringTransform : ITransform<string> | |
{ | |
public abstract string Execute(string @object); | |
} | |
public class TransformToUpper : StringTransform | |
{ | |
public override string Execute(string @object) | |
{ | |
return @object.ToUpper(); | |
} | |
} | |
public class TransformToLower : StringTransform | |
{ | |
public override string Execute(string @object) | |
{ | |
return @object.ToLower(); | |
} | |
} | |
public interface ITransformer<T> | |
{ | |
ITransformer<T> Register(ITransform<T> transform); | |
T Run(); | |
} | |
public abstract class GenericTransformer<T> : ITransformer<T> | |
{ | |
private T _object; | |
private readonly List<ITransform<T>> _transforms; | |
protected GenericTransformer(T @object) | |
{ | |
this._transforms = new List<ITransform<T>>(); | |
this._object = @object; | |
} | |
public virtual ITransformer<T> Register(ITransform<T> transform) | |
{ | |
this._transforms.Add(transform); | |
return this; | |
} | |
public virtual T Run() | |
{ | |
foreach (var transform in this._transforms) | |
{ | |
this._object = transform.Execute(this._object); | |
} | |
return this._object; | |
} | |
} | |
public class StringTransformer : GenericTransformer<string> | |
{ | |
public StringTransformer(string @object) : base(@object) | |
{ | |
} | |
} | |
public class DoWorkOnStringPipeline : IPipeline<string> | |
{ | |
private readonly StringTransformer _transformer; | |
public DoWorkOnStringPipeline(string @object) | |
{ | |
this._transformer = new StringTransformer(@object); | |
this._transformer.Register(new TransformToUpper()); | |
} | |
public string Run() | |
{ | |
return this._transformer.Run(); | |
} | |
} | |
public interface IPipeline<T> | |
{ | |
T Run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment