此编程任务的模式(PHP)?

此编程任务的模式(PHP)?,php,function,design-patterns,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

我有一些函数,它们共享很多代码。但是,有些代码对于每个方法都不同,无法封装。这是一个例子:

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
}
所以我的想法是提取与我在每个函数中调用的其他函数相同的代码。有没有更好的办法解决这个问题


谢谢

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

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

public function function2(){
    $this->sameCodeCode1();
    this differs
    this differs
    $this->sameCode3();
}
然后再次对其进行迭代,因为可以看出,您所称的相同代码2也是不同的:

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
}

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

函数是一组执行特定任务的代码。如果您使用相同的代码,您可以将其放入几个常用函数中,并在相关点调用它们。如果不是这样的话,您可以使用单独的公共文件中的include语句。

我更喜欢这个过程

$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 ();

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

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);

怎么可能有更好的方法呢?我不知道,这就是为什么我要问:)。即使有,你也应该首先删除重复的代码。第一个关键点。如果仍然需要,您可以稍后继续重构。
Running code1
Running differs
Running code2
Running code3
Sample Function

Running code1
Running differs
Running differs
Running code3
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);