Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactory.php
More file actions
Latest commit
205 lines (176 loc) · 5.59 KB
/
Copy pathfactory.php
File metadata and controls
205 lines (176 loc) · 5.59 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
// define abstract 'ArrayFactory' class
abstractclass ArrayFactory{
abstractpublicfunctioncreateArrayObj($type);
}
// define concrete factory to create numerically-indexed array
//objects
class NumericArrayFactory extends ArrayFactory{
private$context='numeric';
publicfunctioncreateArrayObj($type){
$arrayObj=NULL;
switch($type){
case"uppercase";
$arrayObj=newUppercasedNumericArrayObj();
break;
case"lowercase";
$arrayObj=newLowercasedNumericArrayObj();
break;
default:
$arrayObj=newLowercasedNumericArrayObj();
break;
}
return$arrayObj;
}
}
// define concrete factory to create associative array objects
class AssociativeArrayFactory extends ArrayFactory{
private$context='associative';
publicfunctioncreateArrayObj($type){
$arrayObj=NULL;
switch($type){
case"uppercase";
$arrayObj=newUppercasedAssociativeArrayObj();
break;
case"lowercase";
$arrayObj=newLowercasedAssociativeArrayObj();
break;
default:
$arrayObj=newLowercasedAssociativeArrayObj();
break;
}
return$arrayObj;
}
}
//Read more at http://www.devshed.com/c/a/PHP/The-Basics-of-Using-the-Factory-Pattern-in-PHP-5/1/#KfCG3oZHYm5v2OUE.99
// define abstract 'ArrayObj' class
abstractclass ArrayObj{
abstractpublicfunctiongetArraySize();
abstractpublicfunctiongetArrayElements();
}
// define concrete 'UppercasedNumericArrayObj' class
class UppercasedNumericArrayObj extends ArrayObj{
private$inputArray=array('Element 1','Element 2','Element
3');
publicfunctiongetArraySize(){
returncount($this->inputArray);
}
publicfunctiongetArrayElements(){
returnarray_map(strtoupper,$this->inputArray);
}
}
// define concrete 'LowercasedNumericArrayObj' class
class LowercasedNumericArrayObj extends ArrayObj{
private$inputArray=array('ELEMENT 1','ELEMENT 2','ELEMENT
3');
publicfunctiongetArraySize(){
returncount($this->inputArray);
}
publicfunctiongetArrayElements(){
returnarray_map(strtolower,$this->inputArray);
}
}
// define concrete 'UppercasedAssociativecArrayObj' class
class UppercasedAssociativeArrayObj extends ArrayObj{
private$inputArray=array('Element 1'=>'This is element
1','Element 2'=>'This is element 2','Element 3'=>'This is element
3');
publicfunctiongetArraySize(){
returncount($this->inputArray);
}
publicfunctiongetArrayElements(){
returnarray_map(strtoupper,$this->inputArray);
}
}
// define concrete 'LowercasedAssociativecArrayObj' class
class LowercasedAssociativeArrayObj extends ArrayObj{
private$inputArray=array('ELEMENT 1'=>'THIS IS ELEMENT
1','ELEMENT 2'=>'THIS IS ELEMENT 2','ELEMENT 3'=>'THIS IS ELEMENT
3');
publicfunctiongetArraySize(){
returncount($this->inputArray);
}
publicfunctiongetArrayElements(){
returnarray_map(strtolower,$this->inputArray);
}
}
//Read more at http://www.devshed.com/c/a/PHP/The-Basics-of-Using-the-Factory-Pattern-in-PHP-5/2/#josG1Ho56KgIFw8X.99
// testing
try{
// create lowercased numeric array object
$lowerNumArray=NumericArrayFactory::createArrayObj
('lowercase');
// display array object size
echo'Number of elements of lowercased numeric array is the
following : '.$lowerNumArray->getArraySize();
/*
displays the following:
Number of elements of lowercased numeric array is the
following : 3
*/
// display array object elements
print_r($lowerNumArray->getArrayElements());
/*
displays the following
Array ( [0] => element 1 [1] => element 2 [2] => element 3 )
*/
// create uppercased numeric array object
$upperNumArray=NumericArrayFactory::createArrayObj
('uppercase');
// display array object size
echo'Number of elements of uppercased numeric array is the
following : '.$upperNumArray->getArraySize();
/*
displays the following:
Number of elements of uppercased numeric array is the
following : 3
*/
// display array object elements
print_r($upperNumArray->getArrayElements());
/*
displays the following
Array ( [0] => ELEMENT 1 [1] => ELEMENT 2 [2] => ELEMENT 3 )
*/
// create lowercased associative array object
$lowerAssocArray=AssociativeArrayFactory::createArrayObj
('lowercase');
// display array object size
echo'Number of elements of lowercased associative array is
the following : '.$lowerAssocArray->getArraySize();
/*
displays the following:
Number of elements of lowercased associative array is the
following : 3
*/
// display array object elements
print_r($lowerAssocArray->getArrayElements());
/*
displays the following
Array ( [ELEMENT 1] => this is element 1 [ELEMENT 2] => this
is element 2 [ELEMENT 3] => this is element 3 )
*/
// create uppercased associative array object
$upperAssocArray=AssociativeArrayFactory::createArrayObj
('uppercase');
// display array object size
echo'Number of elements of uppercased associative array is
the following : '.$upperAssocArray->getArraySize();
/*
displays the following:
Number of elements of uppercased associative array is the
following : 3
*/
// display array object elements
print_r($upperAssocArray->getArrayElements());
/*
displays the following
Array ( [Element 1] => THIS IS ELEMENT 1 [Element 2] => THIS
IS ELEMENT 2 [Element 3] => THIS IS ELEMENT 3 )
*/
}
catch(Exception$e){
throw$e->getMessage();
exit();
}
//Read more at http://www.devshed.com/c/a/PHP/The-Basics-of-Using-the-Factory-Pattern-in-PHP-5/3/#LFWVuMjAfLiLkE7w.99
?>