Answer the question
In order to leave comments, you need to log in
What is the difference between Strategy and Builder patterns?
The builder is essentially a set of predefined algorithms with a common interface that the "director" uses, the director receives a builder and uses it to create/modify some kind of object.
The strategy is also essentially a set of predefined algorithms with a common interface that the "context" uses, the context receives the algorithm and performs certain operations with its help.
So what's the conceptual difference? Even their diagrams are almost identical. The only thing that I could note myself is that the builder composes and returns the .
I would like an answer with examples.
Answer the question
In order to leave comments, you need to log in
essentially a set of predefined algorithms with a common interface
class Builder {
protected $obj;
public function create(){
$this->obj = new HtmlDocument();
}
public function addHeader(){
// добавляем хедер в $this->obj
}
public function addFooter(){
// добавляем футер в $this->obj
}
public function getObj(){
return $this->obj;
}
}
interface CalculateDiscountInterface {
public function execute($price);
}
class ClientCalculateDiscount implements CalculateDiscountInterface {
public function execute($price){
// рассчитываем цену по одному алгоритму
}
}
class ParterCalculateDiscount implements CalculateDiscountInterface {
public function execute($price){
// рассчитываем цену по другому алгоритму
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question