Skip to content

Instantly share code, notes, and snippets.

@Kikimora
Created June 11, 2020 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kikimora/edb8c23ccefc9ddf327c23f24fa079cb to your computer and use it in GitHub Desktop.
Save Kikimora/edb8c23ccefc9ddf327c23f24fa079cb to your computer and use it in GitHub Desktop.
//Algebras case
//Suppose we have few expressions with Eval operation and parser in library.dll
interface IExpAgl<T>
{
T lit(int i);
T add(T x, T y);
}
public interface IEval { string Eval(); }
public class EvalAlg : IExpAgl<IEval>{}
class Parser { T Parse<T>(IExpAgl<T> a, String s); }
public static class ParserProvider { public static Parser GetParser() => new Parser(); }
//There is client.dll
public static class Client
{
public static void Main(string[] args)
{
var e = ParserProvider.GetParser().Parse(new EvalAlg(), s[0]);
Console.WriteLine(e.Eval());
}
}
//======= Suppose we want to add BoolExp with Algebras =======
//(1) add bool exp and eval operation on it
interface IBoolAlg<T> : IExpAgl<T> { T bool(bool i); }
public class BoolEvalAlg : EvalAlg<IEval>, IBoolAgl<IEval> {}
//(2) modify parser to support bool. ParserProvider RETURN TYPE CHANGE.
class Parser { T Parse<T>(IBoolAgl<T> a, String s); }
public static class ParserProvider { public static BoolParser GetParser() => new BoolParser(); }
//(3) CLIENT HAS TO CHANGE !!!
public static class Main
{
public static void Main(string[] args)
{
var e = ParserProvider.GetParser().Parse(new BoolEvalAlg(), s[0]);
Console.WriteLine(e.Eval());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment