访问者模式是一种对象行为型设计模式,允许一个或多个操作应用到对象上,解耦操作和对象本身。从网站上爬取大量的文件,分为三种格式:PDF,PPT,Word三种,开发一个工具抽取和压缩这些文件。实例如下:
namespace Designmode.Visitor
{
/// <summary>
/// 访问者接口
/// </summary>
public interface IVisitor
{
/// <summary>
/// 访问Pdf文件
/// </summary>
/// <param name="file"></param>
void Visit(PdfFile file);
/// <summary>
/// 访问PPT文件
/// </summary>
/// <param name="file"></param>
void Visit(PPTFile file);
/// <summary>
/// 访问Word文件
/// </summary>
/// <param name="file"></param>
void Visit(WordFile file);
}
}
namespace Designmode.Visitor
{
/// <summary>
/// 文件抽象类
/// </summary>
public abstract class ResourceFile
{
/// <summary>
/// 文件路径
/// </summary>
protected string _filePath;
/// <summary>
/// 构造一个资源文件对象
/// </summary>
/// <param name="filePath">路径</param>
public ResourceFile(string filePath)
{
this._filePath = filePath;
}
abstract public void accept(IVisitor visitor);
}
}
namespace Designmode.Visitor
{
/// <summary>
/// PDF文件
/// </summary>
public class PdfFile : ResourceFile
{
/// <summary>
/// 构造一个pdf文件的对象
/// </summary>
/// <param name="filePath">文件路径</param>
public PdfFile(string filePath) : base(filePath)
{
}
/// <summary>
/// 接受方法
/// </summary>
/// <param name="extractor"></param>
public override void accept(IVisitor visitor)
{
visitor.Visit(this);
}
}
}
namespace Designmode.Visitor
{
/// <summary>
/// PPT文件
/// </summary>
public class PPTFile : ResourceFile
{
/// <summary>
/// 构造一个PPT文件的对象
/// </summary>
/// <param name="filePath">文件路径</param>
public PPTFile(string filePath) : base(filePath)
{
}
/// <summary>
/// 接受方法
/// </summary>
/// <param name="extractor"></param>
public override void accept(IVisitor visitor)
{
visitor.Visit(this);
}
}
}
namespace Designmode.Visitor
{
/// <summary>
/// Word文件
/// </summary>
public class WordFile : ResourceFile
{
/// <summary>
/// 构造一个Word文件的对象
/// </summary>
/// <param name="filePath">文件路径</param>
public WordFile(string filePath) : base(filePath)
{
}
/// <summary>
/// 接受方法
/// </summary>
/// <param name="extractor"></param>
public override void accept(IVisitor visitor)
{
visitor.Visit(this);
}
}
}
namespace Designmode.Visitor
{
public class Extractor: IVisitor
{
/// <summary>
/// 抽取PDF文件
/// </summary>
/// <param name="file">文件</param>
public void Visit(PdfFile file)
{
Console.WriteLine("Extract Pdf.");
}
/// <summary>
/// 抽取PPT文件
/// </summary>
/// <param name="file">文件</param>
public void Visit(PPTFile file)
{
Console.WriteLine("Extract PPT.");
}
/// <summary>
/// 抽取Word文件
/// </summary>
/// <param name="file">文件</param>
public void Visit(WordFile file)
{
Console.WriteLine("Extract Word.");
}
}
}
namespace Designmode.Visitor
{
/// <summary>
/// Compress文件
/// </summary>
public class Compressor:IVisitor
{
/// <summary>
/// Compress PDF文件
/// </summary>
/// <param name="file">文件</param>
public void Visit(PdfFile file)
{
Console.WriteLine("Compress Pdf.");
}
/// <summary>
/// Compress PPT文件
/// </summary>
/// <param name="file">文件</param>
public void Visit(PPTFile file)
{
Console.WriteLine("Compress PPT.");
}
/// <summary>
/// Compress Word文件
/// </summary>
/// <param name="file">文件</param>
public void Visit(WordFile file)
{
Console.WriteLine("Compress Word.");
}
}
}
using Designmode.Visitor;
namespace Designmode
{
class Program
{
static void Main(string[] args)
{
#region 访问者设备
Extractor extractor = new Extractor();
Compressor compressor = new Compressor();
List<ResourceFile> resources = new List<ResourceFile>();
resources.Add(new PdfFile("1"));
resources.Add(new PPTFile("2"));
resources.Add(new WordFile("3"));
foreach (var file in resources)
{
file.accept(extractor);
file.accept(compressor);
}
#endregion
}
}
}