我有几个共享公共逻辑的类。例如,class AddEmployee api负责向中添加一名员工。AddLocation负责添加人员的位置。类如下:
class AddEmployee
{
public function Add()
{
$apiUrl = "https://api.abc.com/Employees";;
$authParameters = array(
'oauth_consumer_key' => $this->CONSUMER_KEY,
);
$xml .= <<<EOM
<SuperFund>
<ABN>$obj->abn</ABN>
<Type>REGULATED</Type>
</SuperFund>
EOM;
$queryParameters = array(
'xml' => $xml
);
$response = $this->ProcesssRequestAndGetResponse('POST',$apiUrl,$authParameters,$queryParameters,$oauth_secret);
return $response;
}
}
class AddLocation
{
public function Add()
{
$apiUrl = "https://api.abc.com/Locations";;
$authParameters = array(
'oauth_consumer_key' => $this->CONSUMER_KEY,
);
$xml .= <<<EOM
<Location>
<Address1>$obj->abn</Address1>
<City>Dhaka</Citry>
</Location>
EOM;
$queryParameters = array(
'xml' => $xml
);
$response = $this->ProcesssRequestAndGetResponse('POST',$apiUrl,$authParameters,$queryParameters,$oauth_secret);
return $response;
}
}在上述两个类中,只有xml部分不同,其他部分相同。将来还会添加其他新类,其中只有xml会有所不同。
我的问题是,我如何重构以从每个类中删除重复的内容?
哪种设计模式可以去掉重复的代码?
https://stackoverflow.com/questions/38576561
复制相似问题