Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

visitor pattern

namespace Wikipedia;

public class ExpressionPrintingVisitor
{
    public void PrintLiteral(Literal literal)
    {
        Console.WriteLine(literal.Value);
    }
    
    public void PrintAddition(Addition addition)
    {
        double leftValue = addition.Left.GetValue();
        double rightValue = addition.Right.GetValue();
        var sum = addition.GetValue();
        Console.WriteLine("{0} + {1} = {2}", leftValue, rightValue, sum);
    }
}

public abstract class Expression
{    
    public abstract void Accept(ExpressionPrintingVisitor v);
    
    public abstract double GetValue();
}

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

    public Literal(double value)
    {
        this.Value = value;
    }
    
    public override void Accept(ExpressionPrintingVisitor v)
    {
        v.PrintLiteral(this);
    }
    
    public override double GetValue()
    {
        return Value;
    }
}

public class Addition : Expression
{
    public Expression Left { get; set; }
    public Expression Right { get; set; }

    public Addition(Expression left, Expression right)
    {
        Left = left;
        Right = right;
    }
    
    public override void Accept(ExpressionPrintingVisitor v)
    {
        Left.Accept(v);
        Right.Accept(v);
        v.PrintAddition(this);
    }
    
    public override double GetValue()
    {
        return Left.GetValue() + Right.GetValue();    
    }
}

public static class Program
{
    public static void Main(string[] args)
    {
        // Emulate 1 + 2 + 3
        var e = new Addition(
            new Addition(
                new Literal(1),
                new Literal(2)
            ),
            new Literal(3)
        );
        
        var printingVisitor = new ExpressionPrintingVisitor();
        e.Accept(printingVisitor);
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to get mouse position c# 
Csharp :: c# unit test for throwing exception method 
Csharp :: c# tuple 
Csharp :: unity deactive all object in list 
Csharp :: audiosource unity 
Csharp :: C# loop through the registry searching for keys containing 
Csharp :: C# random.Next error 
Csharp :: cmd move directory to another directory 
Csharp :: center mouse unity 
Csharp :: preprocessors 
Csharp :: get key in dictionary c# 
Csharp :: select distinct linq mvc 
Csharp :: properties in c# 
Csharp :: c# get random between 0 and 1 
Csharp :: c# console delete last character 
Csharp :: run in new thread C# 
Csharp :: unique field in class model .net core 
Csharp :: Screen.lockcursor unity 
Csharp :: how set format persian data picker to en 
Csharp :: how to check url has parameter in c# 
Csharp :: c# invokerequired wpf 
Csharp :: How to decode Microsoft Local token in service 
Csharp :: unity find all scriptable objects of a type 
Csharp :: find gameobject by name in root 
Csharp :: unity audiosource play 
Csharp :: rgb to console color 
Csharp :: input.getbutton unity 
Csharp :: how to pass function as paraemter of another function pythpn 
Csharp :: oncollisionenter2d 
Csharp :: unity camera.main.screentoworldpoint(input.mouseposition) not working 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =