我应该使用什么设计模式进行导入/导出? [英] What design pattern should I use for import/export?

查看:257
本文介绍了我应该使用什么设计模式进行导入/导出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日历事件对象。我有计划使其与CalDAV / iCal / vCal协议/文件格式兼容,这需要将事件序列化和从不同格式进行序列化。



I可以写一个ImportICal,ExportICal,ImportVCal,ExportVCal等方法,但这似乎不是一个很好的方法,因为如果vCal格式被更新等等。



有没有人处理这种类型的进出口情况?如果是这样,什么设计模式(如果有的话)一般都是最好的?



感谢您的帮助!

解决方案

我不是特别熟悉这些格式,但我会创建一个简单的数据传输对象,代表您的基础日历事件对象。它只是持有数据(伪代码):

  class CalendarEvent 
{
DateTime Date {get ; }
string标题{get; }
string描述{get; }
}

然后你创建一个CalendarEventReader和CalendarEventWriter的接口(它是策略模式,可能是 Builder 模式,

 界面ICalendarEventReader 
{
CalendarEvent Read(Stream data);
//如果需要,可以添加其他方法,例如:
string GetTitleOnly(Stream data);
}
接口ICalendarEventWriter
{
流写入(CalendarEvent事件);
//如果需要,请添加其他方法,例如:
Stream WriteSummaryOnly(CalendarEvent event);
}

然后实际实现实现上述接口。每种格式一种你甚至可以考虑让读者和作家在同一个班上:

  class CalDavConverter:ICalenderEventWriter,ICalendarEventReader 
{
...
}

然后你会有一个存储库(这是工厂模式可能采用 Singleton ),其维护不同格式的ICalenderEventReader / Writer实现列表:

  static class CalenderEventConverterRepository 
{
static ICalendarEventReader GetReader(string formatName / *或任何其他数据,以决定需要的格式* /)
{
...
}

static ICalendarEventReader GetWriter(string formatName / *或任何其他数据,以决定需要的格式* /)
{
...
}
}


I have a calendar event object. I have plans to make it compatible with CalDAV/iCal/vCal protocols/file formats, which require the event be serialized and de-serialized to and from different formats.

I could write an ImportICal, ExportICal, ImportVCal, ExportVCal, etc. set of methods, but that doesn't seem like a very good approach, because what if the vCal format is updated, etc.

Has anyone dealt with this type of import/export situation before? If so, what design pattern (if any) is generally best?

Thanks for your help!

解决方案

I am not particulary familiar with those formats but I'd create an simple data transfer object that represents your genereric calendar event object. It does nothing but holding the data (pseudocode):

class CalendarEvent
{
    DateTime Date { get; }
    string Title { get; }
    string Description { get; }
}

Then you create an interface for CalendarEventReader and CalendarEventWriter (it's Strategy pattern and maybe the Builder pattern, sort of):

interface ICalendarEventReader
{
     CalendarEvent Read(Stream data);
     // Add additional methods if needed e.g.:
     string GetTitleOnly(Stream data);
}
interface ICalendarEventWriter
{
     Stream Write(CalendarEvent event);
     // Add additional methods if needed e.g.:
     Stream WriteSummaryOnly(CalendarEvent event);
}

Then have actual implementations implement the above interfaces. One for each format. You can even think about having reader and writer in the same class:

class CalDavConverter : ICalenderEventWriter, ICalendarEventReader
{
    ...
}

You'd then have a Repository (it's the Factory pattern maybe with Singleton) that maintains a list of ICalenderEventReader/Writer implementations for the different formats:

static class CalenderEventConverterRepository
{
    static ICalendarEventReader GetReader(string formatName /*or any other data upon wich to decide wich format is needed*/)
    { 
    ...
    }

    static ICalendarEventReader GetWriter(string formatName /*or any other data upon wich to decide wich format is needed*/)
    { 
    ...
    }
}

这篇关于我应该使用什么设计模式进行导入/导出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆