开发者

Is this Factory Method the good one for what I'am trying to do?

Here what I'am trying to achieve :

- this a text file parser

- depending on the first chars I create the correct parser object

- I wish to do so using the right factory pattern

- could you tell me if my code below fits correctly the factory pattern ?

- Thank you ! :-)

class Parser
{
    protected $src;

    public function __construct($src)
    {
        $this->src = $src;
    }   
}

class Format1Parser extends Parser
{
    public function Parse()
    {
        // Parsing format 1
        // ...
    }
}

class Format2Parser extends Parser
{
    public function Parse()
    {
        // Parsing format 2
        // ...
    }
}

class ParserFactory
{
    public static function GetParser($src)
    {
        $header = substr($src,0,7);
        if ( $header == "format1" )
        {
            return( new Format1Parser($src) );
        }
        if ( $header == "format2" )
        {
            return( new Format2Parser($src) );
        }
        return(false);
    }   
}

$parse开发者_JAVA百科r = ParserFactory::GetParser( file_get_contents("file.txt") );
$parser->Parse();


First, I would use a suffix (Parser_Format1) instead of a prefix (Format1Parser), because IMHO it's clearer.

As for the factory method itself, you could use dynamic instantiation:

class ParserFactory {
   static public function getParser($src) {
      // may want to change the following line, because it assumes your parser
      // type is always 7 characters long.
      $type = substr($src, 0, 7); 

      $pattern = 'Parser_%type';
      $className = str_replace('%type', $type, $pattern);
      if (!class_exists($className)) {
         throw new InvalidArgumentException("Invalid parser $type");

      return new $className;
   } 
}

Another thing, your Parser class should be abstract and define an abstract function Parse():

abstract class Parser {
    protected $src;

    public function __construct($src)
    {
        $this->src = $src;
    }   

    abstract public function Parse();
}

Defining abstract methods within a base abstract class ensure that most errors (i.e.: a missing Parse method) are caught when the class is parsed (at the beginning of the program), as opposed as to when it's called (in the middle of runtime).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜