《模式——工程化实现及扩展》(设计模式C# 版)《迭代器模式 Iterator》——“自我检验" 参考答案
转自:《模式——工程化实现及扩展》(设计模式C# 版)
http://www.cnblogs.com/callwangxiang/
1、 请遍历当前文件夹列举所有最近3天创建或修改的文件的全路径
参考答案
using System;
using System.Diagnostics;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MarvellousWorks.PracticalPattern.Iterator.Tests.Exercise
{
[TestClass]
public class IterateFileFixture
{
const string Path = @".";
[TestMethod]
public void TestListModifiedAndNewCreatedFiles()
{
Func<DateTime, bool> lessThanThreeDaysHandler = x => (DateTime.Now - x).Days <= 3;
var files =
new DirectoryInfo(Path).GetFiles().Where(x =>
lessThanThreeDaysHandler(x.LastWriteTime) ||
lessThanThreeDaysHandler(x.CreationTime)
);
// 当前单元测试目录下一定会有些新编译的文件
Assert.IsTrue(files.Count() > 0);
// 输出匹配信息
foreach(var file in files)
{
Trace.Write(file.FullName);
if(lessThanThreeDaysHandler(file.LastWriteTime))
Trace.Write("\tM");
if (lessThanThreeDaysHandler(file.CreationTime))
Trace.Write("\tC");
Trace.WriteLine("");
}
}
}
}
using System.Diagnostics;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MarvellousWorks.PracticalPattern.Iterator.Tests.Exercise
{
[TestClass]
public class IterateFileFixture
{
const string Path = @".";
[TestMethod]
public void TestListModifiedAndNewCreatedFiles()
{
Func<DateTime, bool> lessThanThreeDaysHandler = x => (DateTime.Now - x).Days <= 3;
var files =
new DirectoryInfo(Path).GetFiles().Where(x =>
lessThanThreeDaysHandler(x.LastWriteTime) ||
lessThanThreeDaysHandler(x.CreationTime)
);
// 当前单元测试目录下一定会有些新编译的文件
Assert.IsTrue(files.Count() > 0);
// 输出匹配信息
foreach(var file in files)
{
Trace.Write(file.FullName);
if(lessThanThreeDaysHandler(file.LastWriteTime))
Trace.Write("\tM");
if (lessThanThreeDaysHandler(file.CreationTime))
Trace.Write("\tC");
Trace.WriteLine("");
}
}
}
}
------ Test started: Assembly: Iterator.Tests.dll ------
E:\MarvelousWorks\unitTest\Iterator.Tests\bin\Debug\Iterator.dll C
E:\MarvelousWorks\unitTest\Iterator.Tests\bin\Debug\Iterator.pdb C
E:\MarvelousWorks\unitTest\Iterator.Tests\bin\Debug\Iterator.Tests.dll M C
E:\MarvelousWorks\unitTest\Iterator.Tests\bin\Debug\Iterator.Tests.pdb M C
1 passed, 0 failed, 0 skipped, took 0.48 seconds (MSTest 10.0).
E:\MarvelousWorks\unitTest\Iterator.Tests\bin\Debug\Iterator.dll C
E:\MarvelousWorks\unitTest\Iterator.Tests\bin\Debug\Iterator.pdb C
E:\MarvelousWorks\unitTest\Iterator.Tests\bin\Debug\Iterator.Tests.dll M C
E:\MarvelousWorks\unitTest\Iterator.Tests\bin\Debug\Iterator.Tests.pdb M C
1 passed, 0 failed, 0 skipped, took 0.48 seconds (MSTest 10.0).
2、 请以Attribute方式通过标识不同对象主题,确保迭代器可以根据不同的主题遍历相关内容
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MarvellousWorks.PracticalPattern.Iterator.Tests.Exercise
{
[TestClass]
public class AttributSubjectedIteratorFixture
{
[Serializable]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class IterableAttribute : Attribute{}
class SubjectAAttribute : IterableAttribute{}
class SubjectBAttribute : IterableAttribute { }
static class SubjectedIterator
{
/// <summary>
/// 按照指定的主题——即Attribute遍历
/// </summary>
/// <typeparam name="TElement">集合元素类型</typeparam>
/// <typeparam name="TAttribute">遍历所针对的Attribute类型</typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static IEnumerable<TElement> GetEnumerator<TElement, TAttribute>(IEnumerable<TElement> data)
where TAttribute : IterableAttribute
{
if(data == null) return null;
if (data.Count() == 0) return data;
return data.Where(x =>
x.GetType().GetCustomAttributes(typeof (TAttribute), false).Length > 0);
}
}
[SubjectA]
class A{}
[SubjectB]
class B{}
[SubjectA]
[SubjectB]
class AB{}
class O{}
[TestMethod]
public void TestIterateBySubject()
{
var data = new List<object> {new A(), new B(), new AB(), new A(), new O {}, new O {}, new AB()};
var subjectA = SubjectedIterator.GetEnumerator<object, SubjectAAttribute>(data);
Assert.AreEqual<int>(4, subjectA.Count()); // 两个A两个AB
var subjectB = SubjectedIterator.GetEnumerator<object, SubjectBAttribute>(data);
Assert.AreEqual<int>(3, subjectB.Count()); // 1个B两个AB
}
}
}
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MarvellousWorks.PracticalPattern.Iterator.Tests.Exercise
{
[TestClass]
public class AttributSubjectedIteratorFixture
{
[Serializable]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class IterableAttribute : Attribute{}
class SubjectAAttribute : IterableAttribute{}
class SubjectBAttribute : IterableAttribute { }
static class SubjectedIterator
{
/// <summary>
/// 按照指定的主题——即Attribute遍历
/// </summary>
/// <typeparam name="TElement">集合元素类型</typeparam>
/// <typeparam name="TAttribute">遍历所针对的Attribute类型</typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static IEnumerable<TElement> GetEnumerator<TElement, TAttribute>(IEnumerable<TElement> data)
where TAttribute : IterableAttribute
{
if(data == null) return null;
if (data.Count() == 0) return data;
return data.Where(x =>
x.GetType().GetCustomAttributes(typeof (TAttribute), false).Length > 0);
}
}
[SubjectA]
class A{}
[SubjectB]
class B{}
[SubjectA]
[SubjectB]
class AB{}
class O{}
[TestMethod]
public void TestIterateBySubject()
{
var data = new List<object> {new A(), new B(), new AB(), new A(), new O {}, new O {}, new AB()};
var subjectA = SubjectedIterator.GetEnumerator<object, SubjectAAttribute>(data);
Assert.AreEqual<int>(4, subjectA.Count()); // 两个A两个AB
var subjectB = SubjectedIterator.GetEnumerator<object, SubjectBAttribute>(data);
Assert.AreEqual<int>(3, subjectB.Count()); // 1个B两个AB
}
}
}
贸易电子化,技术全球化
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述