English 中文(简体)
多种格式和多种业务的类型(现有设计模式)?
原标题:Parser for multiple formats and multiple operations (design pattern available ?)

I am currently writing a text file parser for several formats. While the text file is parsed, there are different kinds of operations to do. I am trying to do something clean using OOP. Here is where I am stuck :

abstract class Parser
{
    abstract protected function DoSomeStuff($data);

    public function Parse($src)
    {
        if ( $this->GetFormat($src) == 1 )
        {                       
            $data =$this->GetSomeDataFromFormat1($src);
            DoSomeStuff($data);
        }
        if ( $this->GetFormat($src) == 2 )
        {
            $data = $this->GetSomeDataFromFormat2($src);
            DoSomeStuff($data);
        }
    }   
}

class DoSomething extends Parser
{
    protected function DoSomeStuff($data)
    {
        // Doing some stuff with data
    }
}

class DoSomethingElse extends Parser
{
    protected function DoSomeStuff($data)
    {
        // Doing some other stuff with data
    }
}

$ds = new DoSomething();
$ds->Parse(...);

$dse = new DoSomethingElse();
$dse->Parse(...);

As you can see : all the code for all files formats is in class Parser. What can I do to make this cleaner ?

Thanks Antoine

最佳回答

诸如Strategy patterns可能有助于清理。

另见:StackOverflow: [java] Strategy patterns search

问题回答

遵循每一级档案规则通常是一个好主意。

除此以外,在清理工作方面,还有许多工作要做。

唯一要决定的另一点是,你如何装上每一类档案,可以采取几种方式。 I d 建议在工厂类别中登记。

如果您返回一些诸如“CSV”或“XML”之类的物品,那么就不易了解什么是最佳的。

另一种情况是:

if ( $this->GetFormat($src) == 1 )
{
}
if ( $this->GetFormat($src) == 2 )
{
}

//This should be a switch
$src = $this->getFormat($src);

switch($src){
    case 1:

    break;
    case 2:

    break;
}

这样一来,《刑法》便无法在一劳永逸时装上两倍的方法,而开关的记忆却少于一种情况。

并且最后一点可以找到

GetSomeDataFromFormat1 and GetSomeDataFromFormat2

这可能是你给我们的代码,但摘要类别只应指在其类别内提供的其他摘要方法或方法,这些方法或方法应当要求使用者知道他们必须增加两种方法,而不是像你的代码一样容易阅读,而后者是编码的主要方面。





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签