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

本文是小编为大家收集整理的关于我应该使用什么设计模式进行导入/导出?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个日历事件对象.我计划使其与Caldav/ical/vcal协议/文件格式兼容,该格式需要序列化和从不同格式进行序列化和隔离.

我可以编写一种导入的,导出的,importvcal,eartevcal等.但是,这似乎不是一个很好的方法,因为如果更新了VCAL格式,等等.

.

.

以前有人处理过这种类型的进出口情况吗?如果是这样,哪种设计模式(如果有)通常最好?

感谢您的帮助!

推荐答案

我并不特别熟悉这些格式,但是我会创建一个简单的数据传输对象,代表您的Genereric日历事件对象.除了持有数据(伪代码)外,它无能为力:

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

然后,您为CalendareVentReader和CalendareVentWriter创建一个接口(策略模式,也许是 builder 模式,有点):

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);
}

然后,实际实现实现了上述接口.每种格式之一.您甚至可以考虑让读者和作家参加同一班级:

class CalDavConverter : ICalenderEventWriter, ICalendarEventReader
{
    ...
}

您将拥有一个存储库(可能是 Factory 模式使用 singleton ),该存储库维护了不同格式的iCalendereVentReader/Writer实现列表:

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

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

其他推荐答案

使用单个公共接口安排多个实现(日历协议)的常规方法为桥模式.

其他推荐答案

如果更新了VCAL格式,则必须更改所使用的任何设计模式的任何代码(除非他们决定切换到ASN.1升级的地方).

i将创建一个使用导入和导出方法的格式接口,可能是元数据和测试随机位XML是否可能是该格式的方法.然后,对于每种不同格式,您都有一个实现该接口的对象.这是一种"策略设计模式",但是每种格式都代表了做一组凝聚力集(进出口,导出,检测)而不是具有单独策略对象的几种策略.

本文地址:https://www.itbaoku.cn/post/627387.html

问题描述

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 ICalenderEventWriter GetWriter(string formatName /*or any other data upon wich to decide wich format is needed*/)
    { 
    ...
    }
}

其他推荐答案

Usual way to arrange multiple implementations (calendar protocols in your case) with a single common interface is Bridge Pattern.

其他推荐答案

If the vCal format is updated, you will have to change whatever code you have written whichever design pattern you use (unless they decide to switch to something like ASN.1 where upgrades are baked in).

I would create a format interface with import and export methods, and possibly metadata and methods for testing whether a random bit of XML is likely to be that format. Then for each different format, you have an object which implements that interface. This is sort of a 'strategy design pattern', but each format represents several strategies for doing a cohesive set of things (import,export,detection) rather than having separate strategy objects.