development:software-architecture:design-patterns:strategy
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
development:software-architecture:design-patterns:strategy [2024/08/14 13:54] – [Tham khảo] tungnt | development:software-architecture:design-patterns:strategy [2024/08/19 09:41] (current) – tungnt | ||
---|---|---|---|
Line 110: | Line 110: | ||
</ | </ | ||
- | **Ví dụ về Strategy | + | **Ví dụ 1:** |
+ | |||
+ | {{ : | ||
+ | |||
+ | <file php> | ||
+ | <?php | ||
+ | |||
+ | namespace RefactoringGuru\Strategy\Conceptual; | ||
+ | |||
+ | /** | ||
+ | * The Context defines the interface of interest to clients. | ||
+ | */ | ||
+ | class Context | ||
+ | { | ||
+ | /** | ||
+ | * @var Strategy The Context maintains a reference to one of the Strategy | ||
+ | * objects. The Context does not know the concrete class of a strategy. It | ||
+ | * should work with all strategies via the Strategy interface. | ||
+ | */ | ||
+ | private $strategy; | ||
+ | |||
+ | /** | ||
+ | * Usually, the Context accepts a strategy through the constructor, | ||
+ | * provides a setter to change it at runtime. | ||
+ | */ | ||
+ | public function __construct(Strategy $strategy) | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Usually, the Context allows replacing a Strategy object at runtime. | ||
+ | */ | ||
+ | public function setStrategy(Strategy $strategy) | ||
+ | { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * The Context delegates some work to the Strategy object instead of | ||
+ | * implementing multiple versions of the algorithm on its own. | ||
+ | */ | ||
+ | public function doSomeBusinessLogic(): | ||
+ | { | ||
+ | // ... | ||
+ | |||
+ | echo " | ||
+ | $result = $this-> | ||
+ | echo implode(",", | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * The Strategy interface declares operations common to all supported versions | ||
+ | * of some algorithm. | ||
+ | * | ||
+ | * The Context uses this interface to call the algorithm defined by Concrete | ||
+ | * Strategies. | ||
+ | */ | ||
+ | interface Strategy | ||
+ | { | ||
+ | public function doAlgorithm(array $data): array; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Concrete Strategies implement the algorithm while following the base Strategy | ||
+ | * interface. The interface makes them interchangeable in the Context. | ||
+ | */ | ||
+ | class ConcreteStrategyA implements Strategy | ||
+ | { | ||
+ | public function doAlgorithm(array $data): array | ||
+ | { | ||
+ | sort($data); | ||
+ | |||
+ | return $data; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class ConcreteStrategyB implements Strategy | ||
+ | { | ||
+ | public function doAlgorithm(array $data): array | ||
+ | { | ||
+ | rsort($data); | ||
+ | |||
+ | return $data; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * The client code picks a concrete strategy and passes it to the context. The | ||
+ | * client should be aware of the differences between strategies in order to make | ||
+ | * the right choice. | ||
+ | */ | ||
+ | $context = new Context(new ConcreteStrategyA()); | ||
+ | echo " | ||
+ | $context-> | ||
+ | |||
+ | echo " | ||
+ | |||
+ | echo " | ||
+ | $context-> | ||
+ | $context-> | ||
+ | </ | ||
+ | |||
+ | **Ví dụ 2:** | ||
<file php PaymentMethodInterface.php> | <file php PaymentMethodInterface.php> | ||
Line 307: | Line 413: | ||
</ | </ | ||
+ | ===== Tham khảo ===== | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
development/software-architecture/design-patterns/strategy.1723643693.txt.gz · Last modified: 2024/08/14 13:54 by tungnt