Skip to content

Commit b05cfce

Browse files
committed
Adapter pattern using Inherientece - Complete
1 parent 481c6d4 commit b05cfce

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
//Client.php
3+
//Client
4+
include_once('EuroAdapter.php');
5+
include_once('DollarCalc.php');
6+
7+
class Client {
8+
9+
private $requestNow;
10+
11+
private $dollarRequest;
12+
13+
public function __construct() {
14+
$this->requestNow=new EuroAdapter();
15+
$this->dollarRequest=new DollarCalc();
16+
$euro="&#8364;";
17+
18+
echo "Euros: $euro" . $this->makeAdapterRequest($this->requestNow) .
19+
"<br/>";
20+
echo "Dollars: $" . $this->makeDollarRequest($this->dollarRequest);
21+
}
22+
23+
private function makeAdapterRequest(ITarget $req) {
24+
return $req->requestCalc(40, 50);
25+
}
26+
27+
private function makeDollarRequest(DollarCalc $req) {
28+
return $req->requestCalc(40, 50);
29+
}
30+
}
31+
32+
$worker=new Client();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
//DollarCalc.php
3+
class DollarCalc {
4+
private $dollar;
5+
private $product;
6+
private $service;
7+
public $rate = 1;
8+
9+
public function requestCalc($productNow, $serviceNow) {
10+
$this->product = $productNow;
11+
$this->service = $serviceNow;
12+
13+
$this->dollar = $this->product + $this->service;
14+
15+
return $this->requestTotal();
16+
}
17+
18+
public function requestTotal() {
19+
$this->dollar *= $this->rate;
20+
21+
return $this->dollar;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
//EuroAdapter.php
3+
//Adapter
4+
include_once('EuroCalc.php');
5+
include_once('ITarget.php');
6+
7+
class EuroAdapter extends EuroCalc implements ITarget {
8+
9+
public function __construct() {
10+
$this->requester();
11+
}
12+
13+
function requester() {
14+
$this->rate = .8111;
15+
return $this->rate;
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
//EuroCalc.php
3+
class EuroCalc {
4+
5+
private $euro;
6+
private $product;
7+
private $service;
8+
public $rate=1;
9+
10+
public function requestCalc($productNow, $serviceNow) {
11+
$this->product = $productNow;
12+
$this->service = $serviceNow;
13+
14+
$this->euro = $this->product + $this->service;
15+
16+
return $this->requestTotal();
17+
}
18+
19+
public function requestTotal() {
20+
$this->euro *= $this->rate;
21+
22+
return $this->euro;
23+
}
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
//ITarget.php
3+
//Target
4+
interface ITarget {
5+
function requester();
6+
}

0 commit comments

Comments
 (0)