2014-02-27 94 views
1

我需要为我的简单项目提供建议。这是一个记录系统。我有以下文件/类需要针对设计模式的建议

文件 - 检查文件是否存在,是否可读等

ConfigParser - 这是一个抽象类,我用__construct的方法做一些事情。该类从不直接实例化。这是一个装饰者。

abstract class ConfigParser 
{ 
    protected $filename; 

    public function __construct($file) 
    { 
     // Here I want to invoke validate() method of File class 
     // I know that I can do $file = new File(); $file->validate() 
     // but the idea is to keep the things decoupled 
     // Also I know that I can extend the File class ConfigParser extends File 
     // but this doesn't make sence 
     // I can pass the File object on child class new XmlParser('file.xml', $fileObj) 
     // but this doesn't make sence too. 
     // I don't want to use traits as it's bizarre for me 

    } 
} 

XmlParser的 - 扩展ConfigParser

IniParser - 扩展ConfigParser

由于该项目的目标是保持脱钩我无法弄清楚如何实施此方案中的File类的类。

+0

我不知道这可能更适合在http://codereview.stackexchange.com/ – kkuilla

+0

您可能会发现这个答案相关:http://stackoverflow.com/a/18682856/727208 –

回答

0

我不认为这是一个很好的设计在构造函数中进行验证。一个构造函数应该简单地创建Parser并且可能做简单的事情。

你可以做的是使用模板方法模式,然后在父类中进行验证并将真正的解析委托给实际的解析器类。

abstract class ConfigParser { 

    final function parse(){ 
    $file->validate(); 
    $this->conreteParse(); 
    } 

    abstract function concreteParse(); 
}