Repository navigation
Expand file tree
/
Copy pathstrategy.php
More file actions
executable file
·160 lines (144 loc) · 3.17 KB
/
Copy pathstrategy.php
File metadata and controls
executable file
·160 lines (144 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
class duck{
public $fly_obj;
public $qua_obj;
public function __construct($fly_obj, $qua_obj){
$this->fly_obj = $fly_obj;
$this->qua_obj = $qua_obj;
}
public function get_name(){
echo "default";
}
public function get_fly(){
$this->fly_obj->get_fly();
}
public function get_qua(){
$this->qua_obj->get_qua();
}
}
class a_duck extends duck{
public function __construct($fly_obj, $qua_obj){
parent::__construct($fly_obj, $qua_obj);
}
public function get_name(){
echo "a duck";
}
}
class b_duck extends duck{
public function get_name(){
echo "b duck";
}
}
interface fly{
public function get_fly();
}
class a_fly implements fly{
public function get_fly(){
echo "fly_with wing";
}
}
class b_fly implements fly{
public function get_fly(){
echo "fly with with wing";
}
}
interface qua{
public function get_qua();
}
class a_qua implements qua{
public function get_qua(){
echo "que wing";
}
}
class b_qua implements qua{
public function get_qua(){
echo "qua with wing";
}
}
$fly = new a_fly();
$qua = new a_qua();
$duck = new a_duck($fly, $qua);
$duck->get_fly();
$duck->get_qua();
/*
class StrategyContext {
private$strategy = NULL;
//bookList is not instantiated at construct time
public function __construct($strategy_ind_id) {
switch ($strategy_ind_id) {
case"C":
$this->strategy = new StrategyCaps();
break;
case"E":
$this->strategy = new StrategyExclaim();
break;
case"S":
$this->strategy = new StrategyStars();
break;
}
}
public function showBookTitle($book) {
return$this->strategy->showTitle($book);
}
}
interface StrategyInterface {
public function showTitle($book_in);
}
class StrategyCaps implements StrategyInterface {
public function showTitle($book_in) {
$title = $book_in->getTitle();
$this->titleCount++;
return strtoupper ($title);
}
}
class StrategyExclaim implements StrategyInterface {
public function showTitle($book_in) {
$title = $book_in->getTitle();
$this->titleCount++;
return Str_replace(' ','!',$title);
}
}
class StrategyStars implements StrategyInterface {
public function showTitle($book_in) {
$title = $book_in->getTitle();
$this->titleCount++;
return Str_replace(' ','*',$title);
}
}
class Book {
private$author;
private$title;
function __construct($title_in, $author_in) {
$this->author = $author_in;
$this->title = $title_in;
}
function getAuthor() {
return$this->author;
}
function getTitle() {
return$this->title;
}
function getAuthorAndTitle() {
return$this->getTitle() . ' by ' . $this->getAuthor();
}
}
writeln('BEGIN TESTING STRATEGY PATTERN');
writeln('');
$book = new Book('PHP for Cats','Larry Truett');
$strategyContextC = new StrategyContext('C');
$strategyContextE = new StrategyContext('E');
$strategyContextS = new StrategyContext('S');
writeln('test 1 - show name context C');
writeln($strategyContextC->showBookTitle($book));
writeln('');
writeln('test 2 - show name context E');
writeln($strategyContextE->showBookTitle($book));
writeln('');
writeln('test 3 - show name context S');
writeln($strategyContextS->showBookTitle($book));
writeln('');
writeln('END TESTING STRATEGY PATTERN');
function writeln($line_in) {
echo$line_in."<br/>";
}
*/