FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Design-patterns/strategy.php at master · LaughingMeMe/Design-patterns · GitHub
LaughingMeMe
/
Design-patterns
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Design-patterns
/
strategy.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
42 lines (35 loc) · 842 Bytes
Breadcrumbs
Design-patterns
/
strategy.php
Copy path
File metadata and controls
42 lines (35 loc) · 842 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
//策略模式,帮助构建的对象不必自身包含逻辑,而是能够根据需要利用其他对象中的算法
class
CD
{
public
$
_title
;
public
$
_band
;
protected
$
_strategy
;
public
function
__construct
(
$
title
,
$
band
){
$
this
->
_title
=
$
title
;
$
this
->
_band
=
$
band
;
}
public
function
setStrategy
(
$
strategy
){
$
this
->
_strategy
=
$
strategy
;
}
public
function
get
(){
$
this
->
_strategy
->
get
(
$
this
);
}
}
interface
strategy{
public
function
get
(
CD
$
cd
);
}
class
BuyStrategy
implements
strategy{
public
function
get
(
CD
$
cd
){
echo
$
cd
->
_title
.
'
Buy
'
;
}
}
class
SellStrategy
implements
strategy{
public
function
get
(
CD
$
cd
){
echo
$
cd
->
_title
.
'
Sell
'
;
}
}
$
cd
=
new
CD
(
'
aaa
'
,
'
xxx
'
);
$
cd
->
setStrategy
(
new
BuyStrategy
());
$
cd
->
get
();
//此结构中CD类不包含逻辑,调用buyStrategy对象来实现需求
?>
Back
|
FazBrowse Home
|
New Git URL