这个编程任务的模式(PHP)?

时间:2012-05-02 09:39:17

标签: php function design-patterns

我有一些共享大量代码的函数。但是,某些代码对于每种方法都有所不同,并且无法封装。这是一个例子:

public function function1(){
    same code
    same code1
    this differs
    same code2
    this differs
    same code 3
}

public function function2(){
    same code
    same code1
    this differs
    this differs
    same code 3
}

所以我的想法是提取与我在每个函数中调用的其他函数相同的代码。有没有更好的方法来解决这个问题?

谢谢!

4 个答案:

答案 0 :(得分:1)

第一步:将重复的代码移动到它自己的函数中:

public function function1(){
    $this->sameCodeCode1();
    this differs
    same code2
    this differs
    $this->sameCode3();
}

public function function2(){
    $this->sameCodeCode1();
    this differs
    this differs
    $this->sameCode3();
}

然后再次迭代它,因为可以看出你所谓的相同的code2 也是差异:

public function function1() {
    $this->inverse(function() {
        this differs
        same code2
        this differs
    });
}

public function function2(){
    $this->inverse(function() {        
        this differs
        this differs
    }
}

private function inverse($function)
{
    same code
    same code1
    $function();
    same code 3
}

这可能会引导您对代码进行进一步的改进,例如创建方法对象而不是共享基类的函数。请参阅以下重构模式:Replace Method with Method Object

答案 1 :(得分:0)

功能是代表特定任务的代码集。如果使用相同的代码,可以将其放入几个常用函数中,并在相关点调用它们。如果不是这种情况,您可以使用包含在单独公共文件中的include语句。

答案 2 :(得分:0)

我更喜欢这个过程应该放松

$task1 = new Task ( array (
        "code",
        "code1",
        "differs",
        "code2",
        "code3",
        function () {
            echo "Sample Function";
        } 
) );

$task2 = new Task ( array (
        "code",
        "code1",
        "differs",
        "differs",
        "code3",
        array (
                "Code2,Code4" 
        ) 
) );

echo "<pre />";
$process = new Process ( $task1 );
$process->run ();

echo PHP_EOL;
echo PHP_EOL;

$process = new Process ( $task2 );
$process->run ();

班级

class Task {
    private $process = array ();
    function __construct($process) {
        $this->process = $process;
    }

    function add($name) {
        $this->process [] = $name;
    }

    function getProcess($i = null) {
        return ($i == null) ? $this->process : $this->process [$i];
    }

    function getTotal() {
        return count ( $this->process );
    }

}

class Process {
    private $task;
    function __construct(Task $task) {
        $this->task = $task;
    }

    function run() {

        for($i = 0; $i < $this->task->getTotal (); $i ++) {
            $run = $this->task->getProcess ( $i );
            if (is_callable ( $run )) {
                $run ();
            } else if (is_string ( $run )) {
                echo "Running " . $run, PHP_EOL;
            }
        }
    }
}

输出

Running code1
Running differs
Running code2
Running code3
Sample Function

Running code1
Running differs
Running differs
Running code3

答案 3 :(得分:0)

这看起来像是战略模式的候选人。

public function coreProcess(uniquePart $unique){
    same code
    same code1
    $unique->part1();
    $unique->part2();
    $unique->part3();
    same code 3
}

interface uniquePart
{
  function part1();

  function part2();

  function part3();
}

class process1 implements uniquePart
{
  function part1()
  {
     //some unique code
  }

  function part2()
  {
     //do nothing
  }

  function part3()
  {
    //unique code
  }
}  

class process2 implements uniquePart
{
  function part1()
  {
    //some unique code
  }

  function part2()
  {
     //some unique code
  }

  function part3()
  {
    //some unique code
  }
}

//run process 1
coreProcess(new process1);

//run process 2
coreProcess(new process2);

http://www.ibm.com/developerworks/library/os-php-designptrns/