确保函数被调用
Make sure that functions are called
我正在处理一个项目,我们在整个代码库中创建许多对象。
因此,对于某些对象,我们决定使用工厂来控制创建对象及其所有依赖项的过程。这是我们正在尝试做的一个例子:
class CreateBranchFactory implements CreateBranchInterface {
private $branch;
public function __construct() {
$this->branch = new Branch();
$this->branch->scenario = 'create';
}
public function createBranch($branchForm) {
$this->branch->attributes = $branchForm->attributes;
//Example of all the things that I need to do after creating my object
$this->setOfficialNameAndPrinterName($branchForm->official_name, $branchForm->printer_name, $branchForm->name);
$this->createDependencies1();
$this->someOtherFunction();
return $this->branch;
}
public function setOfficialNameAndPrinterName($offName, $printerName, $branchName) {
$this->branch->official_name = $offName ?? $branchName;
$this->branch->printer_name = $printerName ?? $branchName;
$this->branch->save();
}
public function createDependencies1() {
}
为了有一个合适的合同,我为此创建了一个接口。该接口指定了应该定义的函数
interface CreateBranchInterface {
public function setOfficialNameAndPrinterName(String $offName, String $printerName, String $branchName);
public function createDependencies1();
}
我的问题是,合约指定了所有 应该定义的函数 ,但没有控制 应该调用哪些函数 .有没有我可以使用的设计模式,以确保调用这些函数?
您不能使用接口创建此类合同 - 接口指定了您可以调用哪些方法以及如何调用。调用所有这些方法是实现的一部分,不能由接口提供。您需要创建带有实现的抽象 class 并使用 final
关键字来禁止覆盖它:
abstract class CreateBranch {
abstract protected function getBranch(): Branch;
final public function createBranch($branchForm) {
$this->getBranch()->attributes = $branchForm->attributes;
//Example of all the things that I need to do after creating my object
$this->setOfficialNameAndPrinterName($branchForm->official_name, $branchForm->printer_name, $branchForm->name);
$this->createDependencies1();
$this->someOtherFunction();
return $this->getBranch();
}
abstract public function setOfficialNameAndPrinterName(String $offName, String $printerName, String $branchName);
abstract public function createDependencies1();
abstract public function someOtherFunction();
}
我正在处理一个项目,我们在整个代码库中创建许多对象。
因此,对于某些对象,我们决定使用工厂来控制创建对象及其所有依赖项的过程。这是我们正在尝试做的一个例子:
class CreateBranchFactory implements CreateBranchInterface {
private $branch;
public function __construct() {
$this->branch = new Branch();
$this->branch->scenario = 'create';
}
public function createBranch($branchForm) {
$this->branch->attributes = $branchForm->attributes;
//Example of all the things that I need to do after creating my object
$this->setOfficialNameAndPrinterName($branchForm->official_name, $branchForm->printer_name, $branchForm->name);
$this->createDependencies1();
$this->someOtherFunction();
return $this->branch;
}
public function setOfficialNameAndPrinterName($offName, $printerName, $branchName) {
$this->branch->official_name = $offName ?? $branchName;
$this->branch->printer_name = $printerName ?? $branchName;
$this->branch->save();
}
public function createDependencies1() {
}
为了有一个合适的合同,我为此创建了一个接口。该接口指定了应该定义的函数
interface CreateBranchInterface {
public function setOfficialNameAndPrinterName(String $offName, String $printerName, String $branchName);
public function createDependencies1();
}
我的问题是,合约指定了所有 应该定义的函数 ,但没有控制 应该调用哪些函数 .有没有我可以使用的设计模式,以确保调用这些函数?
您不能使用接口创建此类合同 - 接口指定了您可以调用哪些方法以及如何调用。调用所有这些方法是实现的一部分,不能由接口提供。您需要创建带有实现的抽象 class 并使用 final
关键字来禁止覆盖它:
abstract class CreateBranch {
abstract protected function getBranch(): Branch;
final public function createBranch($branchForm) {
$this->getBranch()->attributes = $branchForm->attributes;
//Example of all the things that I need to do after creating my object
$this->setOfficialNameAndPrinterName($branchForm->official_name, $branchForm->printer_name, $branchForm->name);
$this->createDependencies1();
$this->someOtherFunction();
return $this->getBranch();
}
abstract public function setOfficialNameAndPrinterName(String $offName, String $printerName, String $branchName);
abstract public function createDependencies1();
abstract public function someOtherFunction();
}