-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrategy.php
58 lines (42 loc) · 1.38 KB
/
Strategy.php
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
<?php
class Product1 {
public function sendToMarketplace(){
if ($this->marketplace == 'Amazon') {
//huge code block
} elseif ($this->marketplace == 'Via Varejo') {
//huge code block
} elseif ($this->marketplace == 'Magalu') {
//do I need to say it again? :0
}
// this code expands to infinity. Every time a new marketplace is added
// you need to change this :( (SRP violation, OCP violation)
}
}
interface ProductContract {
// this is what the product shows to the external world
}
interface MarketplaceDeliveryStrategy {
public function sendToMarketplace(ProductContract $product);
}
class AmazonDeliveryStrategy implements MarketplaceDeliveryStrategy {
// invert any dependencies, like http libraries, here
public function __construct($dependencies = null){
}
public function sendToMarketplace(ProductContract $product){
// succient code to execute delivery
}
}
class MarketplaceDeliveryStrategyFactory {
public function getStrategy($marketplace){
//determine which strategy should be used
return new AmazonDeliveryStrategy();
}
}
class Product {
public function __construct(MarketplaceDeliveryStrategyFactory $strategyFactory){
$this->deliveryStrategy = $strategyFactory->getStrategy($this->marketplace);
}
public function sendToMarketplace(){
$this->deliveryStrategy->sendToMarketplace($this);
}
}