public void FolderVisitor_SetEditVisitorAndExecuteTwice_IsModifiedInverted() { var file = new Folder("test"); var visitor = new EditVisitor(); file.Accept(visitor); Assert.True(visitor.IsModified); file.Accept(visitor); Assert.False(visitor.IsModified); }
/// <summary> /// 访问者 /// </summary> static void Visitor() { Console.WriteLine($"访问者模式{Environment.NewLine}-------------------------------------------"); //创建目录结构 Start IFile folderA = new Folder("FolderA"); folderA.Add(new Txt("TxtA")); folderA.Add(new Png("PngA")); IFile folderB = new Folder("FolderB"); folderB.Add(new Txt("TxtB")); folderB.Add(new Png("PngB")); IFile folderC = new Folder("FolderC"); folderC.Add(new Txt("TxtC")); folderC.Add(new Png("PngC")); folderB.Add(folderC); IFile folder = new Folder("Folder"); folder.Add(folderA); folder.Add(folderB); //创建目录结构 End FolderSumVisitor folderSumVisitor = new FolderSumVisitor(); folder.Accept(folderSumVisitor); Console.WriteLine($"共有文件夹{folderSumVisitor.Sum}个"); PngSumVisitor pngSumVisitor = new PngSumVisitor(); folder.Accept(pngSumVisitor); Console.WriteLine($"共有png文件{pngSumVisitor.Sum}个"); TxtNameVisitor txtNameVisitor = new TxtNameVisitor(); folder.Accept(txtNameVisitor); Console.WriteLine($"{Environment.NewLine}所有的txt文件名如下:"); txtNameVisitor.NameList.ForEach(t => Console.WriteLine(t)); Console.ReadKey(); }