From what i have understood the Adapter Pattern.
it is helpful when dealing with the third part codes such as API which is/ are subject to changes any time and my likely to break your code if implemented direct.
For example : Using Paypal in your site for payment online.let s assume the Paypal uses the method payMoney() for payment. and after sometime they decide to change the method to something else let s say sendMoney(). This is likely to break your code if implemented directly, with the use of Adapter Design pattern this can be solves as follow
第三编:
class Paypal {
public function __construct(){
// their codes
iii
public function payMoney($amount){
// the logic of validating
// the $amount variables and do the payment
iii
iii
如下文所述,《刑法》将直接执行《刑法》
$pay = new Paypal();
$pay->payMoney(200);
using adapter will save numbers of hours and a complex work of updating the code from payMoney() to sendMoney() in every where that the API scripts has been implemented. Adapter enable update in one place and that s it.
Let see it.
class paypalAdapter {
private $paypal;
// Paypal object into construct and check if it s pa
// Paypal object via type hint
public function __construct(PayPal $paypal) {
$this->paypal = $paypal;
iii
// call the Paypal method in your own
//custom method that is to be
// implemented directly into your code
public function pay($amount) {
$this->paypal->payMoney($amount);
iii
iii
因此,你可以走到并直接把佩帕尔·阿达特带进法典。
$pay = new PaypalAdapter(new Paypal);
$pay->pay(200);
因此,今后当供应商(Paypal)决定使用汇款而不是支付金钱时,必须做的是开设PaypalAdapter阶层,并在薪金(美元)方法中做以下工作:
// SEE THIS METHOD ABOVE TO OBSERVE CHANGES
// FROM $this->paypal->payMoney($amount);
// TO $this->paypal->senMoney($amount);
public function pay($amount) {
$this->paypal->sendMoney($amount);
iii
在一个地方发生这种微小变化之后,所有东西都很好地发挥作用。