【问题标题】:Remove duplicate code from abstract children从抽象子代中删除重复代码
【发布时间】:2014-08-12 16:06:40
【问题描述】:

我正在为我们正在构建的软件创建模板引擎。我有一个抽象类Template,有3个扩展类HeaderBodyFooter。 3 个孩子中的每一个都包含相同的构造函数代码,这让我很烦恼,只要看看代码中的重复,构造函数都有这个:

public function __construct($file, array $kvp)
    {
        $this->templates[] = array(
            'context' => $this->getClass($this),
            'file' => $file,
            'kvp' => $kvp
        );
    }

有没有办法将此代码移入父类或减少重复? 以下是完整代码:

<?php

Interface iTemplate
{   
    protected function add(Template $template);
    protected function remove(Template $template);
}

abstract class Template implements iTemplate
{
    protected $html = array();
    protected $templates = array();

    public function add($template)
    {
        array_push($this->templates, $template);
    }

    public function remove($template)
    {
        if(in_array($template, $this->templates))  {
            $key = array_search($template, $this->templates);
            $this->templates[$key] = null;
            $this->templates = array_filter($this->templates);
        }
    }

    protected function getClass($context)
    {
        return get_class($context);
    }

    public function render()
    {
        foreach($this->templates as $template)
        {
            $output = file_get_contents($template->file);

            foreach ($this->template->kvp as $key => $value) {
                $tagToReplace = "[@$key]";
                $output = str_replace($tagToReplace, $value, $output);
            }

            $html[] = $output;
        }

        return implode("\n", $html);
    }
}

class Header extends Template
{   
    public function __construct($file, array $kvp)
    {
        $this->templates[] = array(
            'context' => $this->getClass($this),
            'file' => $file,
            'kvp' => $kvp
        );
    }
}

class Body extends Template
{   
    public function __construct($file, array $kvp)
    {
        $this->templates[] = array(
            'context' => $this->getClass($this),
            'file' => $file,
            'kvp' => $kvp
        );
    }
}

class Footer extends Template
{   
    public function __construct($file, array $kvp)
    {
        $this->templates[] = array(
            'context' => $this->getClass($this),
            'file' => $file,
            'kvp' => $kvp
        );
    }
}


/* Example */

$Template = new Template();
$Template->add(new Header($header_file, $header_kvp));
$Template->add(new Body($body_file, $body_kvp));
$Template->add(new Footer($footer_file, $footer_kvp));
echo $Template->render();

【问题讨论】:

  • 你可以调用父构造函数: parent::_construct($file, $kvp);如果子类中没有提供构造函数,则会自动调用父构造函数。

标签: php abstract-class code-duplication


【解决方案1】:

简单的解决方案是使用特征:


interface iTemplate
{   
    public function add(Template $template);
    public function remove(Template $template);
}

class Template implements iTemplate
{
    protected $html = array();
    protected $templates = array();

    public function add(Template $template)
    {
        //...
    }

    public function remove(Template $template)
    {
        //...
    }

    protected function getClass()
    {
        return get_class($this);
    }

    public function render()
    {
        //...
    }
}

class Header extends Template
{   use ConstructTrait;
}

class Body extends Template
{   use ConstructTrait;
}

class Footer extends Template
{   use ConstructTrait;
}

trait ConstructTrait
{
  public function __construct($file, $kvp)
  {
    $this->templates[] = array(
      'context' => $this->getClass(),
      'file' => $file,
      'kvp' => $kvp
      );
  }
}

【讨论】:

    猜你喜欢
    • 2017-10-01
    • 2011-04-12
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多