0

我有一些共享大量代码的功能。但是,某些代码因每种方法而异,无法封装。这是一个例子:

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

4 回答 4

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

于 2012-05-02T09:49:48.677 回答
0

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

于 2012-05-02T09:50:18.940 回答
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
于 2012-05-02T10:11:55.313 回答
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/

于 2012-05-02T10:50:41.630 回答