예제 #1
0
        static void Main(string[] args)
        {
            var e = new AdditionExpression(
                left: new DoubleExpression(1),
                right: new AdditionExpression(
                    left: new DoubleExpression(2),
                    right: new DoubleExpression(3)
                    )
                );

            // INTRUSIVE EXPRESSION PRINTING - NOT VISITOR PATTERN

            //var sb = new StringBuilder();

            //e.Print(sb);

            //Console.WriteLine(sb);

            // what is more likely: new type or new operation

            // REFLECTION BASED PRINTING - STILL NOT USING VISITOR PATTERN

            //var sb = new StringBuilder();

            //ExpressionPrinter.Print2(e, sb);
            //Console.WriteLine(sb);

            // CLASSIC VISITOR

            //var ep = new ExpressionPrinter();
            //ep.Visit(e);

            //Console.WriteLine(ep.ToString());

            //var calc = new ExpressionCalculator();
            //calc.Visit(e);

            //Console.WriteLine($"{ep} = {calc.Result}");

            // DYNAMIC VISITOR

            var ep = new ExpressionPrinter();
            var sb = new StringBuilder();

            ep.Print((dynamic)e, sb);

            Console.WriteLine(sb);

            // disadvantages:

            // 1) Performance penalty
            // 2) Runtime error on missing visitor
            // 3) Problematic w.r.t. inheritance

            Console.ReadLine();
        }
예제 #2
0
        void Main(string[] args)
        {
            Expression e = new AdditionExpression(
                new DoubleExpression(1),
                new AdditionExpression(
                    new DoubleExpression(2),
                    new DoubleExpression(3)));

            var ep = new ExpressionPrinter();
            var sb = new StringBuilder();

            ep.Print((dynamic)e, sb);
            Console.WriteLine(sb);
        }