doucuo9126 2010-02-20 05:13
浏览 35
已采纳

配置解析器 - 静态与否?

Imagine a strategy whereby you might want parsers for several formats of config file, say INI, XML and YAML for example. Parsing these will result in an instance of a common class say 'Config_Data'.

My thoughts are that as the parser has a very simple mandate, and no state that I can see, it is best implemented as a static class, so you could employ the following:

Parser_Ini::parse(...);
Parser_Xml::parse(...);
...

This doesn't feel entirely correct to me as of course these static classes can't implement an interface etc, but they would seem to make good sense.

I would be interested to know your thoughts on the matter :).

Thanks, James

  • 写回答

2条回答 默认 最新

  • doulu7921 2010-02-20 05:22
    关注

    Creating static classes have a lot of drawbacks, and I don't see what you gain from it, other than a feeling of convenience, which I find is misleading you.

    If you do the classes as regular instance classes, and implement an interface, you will have the freedom of changing implementation on the fly.

    The classes that depend on your parser can just take an implementation of your interface as a parameter, and you can thereby inject whatever parser-type you need on that class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部