我有幾個共享公共邏輯的類。例如,類AddEmployee負責向api添加員工。 添加位置負責添加一個人的位置。分類如下:重構代碼刪除重複的代碼
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會有所不同。
我的問題是我該如何重構刪除每個類的重複?
哪種設計模式可以刪除重複的代碼?