您的位置:首页 > 编程语言 > C#

《模式——工程化实现及扩展》(设计模式C# 版)《迭代器模式 Iterator》——“自我检验" 参考答案

2011-05-10 16:57 1231 查看
转自:《模式——工程化实现及扩展》(设计模式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("");
}
}
}
}

------ 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).

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
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐