使用访客模式访问成对的访问者

时间:2018-04-09 17:20:54

标签: c# design-patterns visitor

是否有一个干净的模式,允许访问者一次访问两个访问者?

例如,如果我的访问者是二元加法运算符,则需要知道两个可访问输入的数据类型。

我在下面提供了一个解决方案,但感觉它很混乱,因为它要求所有可访问对象都包含所有具体访问者的重载,并且还要求访问者包含虚拟访问以解析r-visitable。

如果访客模式不适合,是否有其他模式更适合?

谢谢

using System;
using System.Diagnostics;

namespace VisitorTest
{

   class Program
   {

      static void Main(string[] args)
      {

         // Given two visitable objects...
         IVisitable stringVisitable = new StringVisitable("987");
         IVisitable numberVisitable = new NumberVisitable(123);

         // And a visitor that performs a "lVisitable + rVisitable" operation...
         PlusOpVisitor plusOpVisitor = new PlusOpVisitor();

         // Test "string + string" == "987987"
         Console.WriteLine
         (  stringVisitable.PairAccept
            (  plusOpVisitor
            ,  stringVisitable
            )
         );

         // Test "string + number" == (convert both to string) == "987123"
         Console.WriteLine
         (  stringVisitable.PairAccept
            (  plusOpVisitor
            ,  numberVisitable
            )
         );

         // Test "number + string" == (convert both to number) == #1110
         Console.WriteLine
         (  numberVisitable.PairAccept
            (  plusOpVisitor
            ,  stringVisitable
            )
         );

         // Test "number + number" == #246
         Console.WriteLine
         (  numberVisitable.PairAccept
            (  plusOpVisitor
            ,  numberVisitable
            )
         );

      }

   }

   interface IPairVisitor
   {  // Messy: Dummies, just to know the l-visitable type, while visiting the r-visitable
      IVisitable Visit(StringVisitable lVisitable, IVisitable rVisitable);
      IVisitable Visit(NumberVisitable lVisitable, IVisitable rVisitable);
      // Actual visitor operations, what to do in each case
      IVisitable Visit(StringVisitable lVisitable, StringVisitable rVisitable);
      IVisitable Visit(NumberVisitable lVisitable, StringVisitable rVisitable);
      IVisitable Visit(StringVisitable lVisitable, NumberVisitable rVisitable);
      IVisitable Visit(NumberVisitable lVisitable, NumberVisitable rVisitable);
   }

   interface IVisitable
   {  // Resolve the l-visitable, include the unresolved r-visitable
      IVisitable PairAccept(IPairVisitor visitor, IVisitable  rVisitable);
      // Messy: Resolve the r-visitable, include the previously resolve l-visitable
      // The visitable-object must know the type of all objects that it can be
      // accepted against (normal visitor pattern doesn't have this restriction)
      IVisitable PairAccept(IPairVisitor visitor, StringVisitable lVisitable);
      IVisitable PairAccept(IPairVisitor visitor, NumberVisitable lVisitable);
   }

   class PlusOpVisitor : IPairVisitor
   {  // Repeat the accept, but for the other visitable
      public IVisitable Visit
      (  StringVisitable lVisitable
      ,  IVisitable      rVisitable
      ){ return rVisitable.PairAccept(this, lVisitable);
      }
      public IVisitable Visit
      (  NumberVisitable lVisitable
      ,  IVisitable      rVisitable
      ){ return rVisitable.PairAccept(this, lVisitable);
      }
      // Perform the actual operation for each pair
      public IVisitable Visit
      (  StringVisitable lVisitable
      ,  StringVisitable rVisitable
      ){ return new StringVisitable
         (  string.Concat
            (  lVisitable.Value
            ,  rVisitable.Value
            )
         );
      }
      public IVisitable Visit
      (  StringVisitable lVisitable
      ,  NumberVisitable rVisitable
      ){ return new StringVisitable
         (  string.Concat
            (  lVisitable.Value
            ,  rVisitable.Value.ToString()
            )
         );
      }
      public IVisitable Visit
      (  NumberVisitable lVisitable
      ,  StringVisitable rVisitable
      ){ return new NumberVisitable
         (  lVisitable.Value
          + int.Parse(rVisitable.Value)
         );
      }
      public IVisitable Visit
      (  NumberVisitable lVisitable
      ,  NumberVisitable rVisitable
      ){ return new NumberVisitable
         (  lVisitable.Value
          + rVisitable.Value
         );
      }
   }

   class StringVisitable : IVisitable
   {  public StringVisitable
      (  string value
      ){ _value = value;
      }
      public string Value
      {  get { return _value; }
      }
      // Visit as an l-visitable, r-visitable still unresolved
      public IVisitable PairAccept
      (  IPairVisitor  visitor
      ,  IVisitable rVisitable
      ){ return visitor.Visit(this, rVisitable);
      }
      // Visit as an r-visitable, l-visitable resolved
      public IVisitable PairAccept
      (  IPairVisitor       visitor
      ,  StringVisitable lVisitable
      ){ return visitor.Visit(lVisitable, this);
      }
      public IVisitable PairAccept
      (  IPairVisitor       visitor
      ,  NumberVisitable lVisitable
      ){ return visitor.Visit(lVisitable, this);
      }
      public override string ToString()
      {  return string.Concat("\"", _value, "\"");
      }
      private string _value;
   }

   class NumberVisitable : IVisitable
   {  public NumberVisitable
      (  int value
      ){ _value = value;
      }
      public int Value
      {  get { return _value; }
      }
      public IVisitable PairAccept
      (  IPairVisitor  visitor
      ,  IVisitable rVisitable
      ){ return visitor.Visit(this, rVisitable);
      }
      public IVisitable PairAccept
      (  IPairVisitor       visitor
      ,  StringVisitable lVisitable
      ){ return visitor.Visit(lVisitable, this);
      }
      public IVisitable PairAccept
      (  IPairVisitor       visitor
      ,  NumberVisitable lVisitable
      ){ return visitor.Visit(lVisitable, this);
      }
      public override string ToString()
      {  return string.Concat("#", _value);
      }
      private int _value;
   }

}

1 个答案:

答案 0 :(得分:0)

看起来你实际上正在寻找interpreter pattern。您可能还想查看expression trees