File tree Expand file tree Collapse file tree 5 files changed +102
-0
lines changed
SandersW-LearningPHPDesignPatterns/adapter/UsingInheritence Expand file tree Collapse file tree 5 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 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 ="€ " ;
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 ();
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ //ITarget.php
3+ //Target
4+ interface ITarget {
5+ function requester ();
6+ }
You can’t perform that action at this time.
0 commit comments