Skip to content

Commit

Permalink
pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldplayer1995 committed Oct 25, 2022
1 parent 113c905 commit b4f9bc8
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Behavioral/Visitor/csharp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Text;

namespace DesignPatters
{
using DictType = Dictionary<Type, Action<Expression, StringBuilder>>;

public abstract class Expression
{

}

public class DoubleExpression : Expression
{
public double Value { get; set; }

public DoubleExpression(double value)
{
this.Value = value;
}
}

public class AdditionExpression : Expression
{
public Expression Left;
public Expression Right;

public AdditionExpression(Expression left, Expression right)
{
Left = left ?? throw new ArgumentNullException(paramName: nameof(left));
Right = right ?? throw new ArgumentNullException(paramName: nameof(right));
}
}

public static class ExpressionPrinter
{
private static DictType actions = new DictType
{
[typeof(AdditionExpression)] = (e, sb) =>
{
var ae = (AdditionExpression)e;
sb.Append("(");
Print(ae.Left, sb);
sb.Append("+");
Print(ae.Right, sb);
sb.Append(')');
},
[typeof(DoubleExpression)] = (e, sb) =>
{
var de = (DoubleExpression)e;
sb.Append(de.Value);
}
};
public static void Print(Expression e, StringBuilder sb)
{
actions[e.GetType()](e, sb);
}
}

public class Demo
{
public static void Main()
{
var e = new AdditionExpression(new DoubleExpression(2), new AdditionExpression(new DoubleExpression(2),new DoubleExpression(2)));
var sb = new StringBuilder();
ExpressionPrinter.Print(e, sb);
Console.WriteLine(sb.ToString());

}
}
}

0 comments on commit b4f9bc8

Please sign in to comment.