If a class has a constructor with a lot of arguments, the problem is in this class. Using a builder pattern just masks the problem, which is even worse. So I consider a builder an anti-pattern. I'd suggest to use a decorator instead:
interface Burger { } class BreadWithCumin implements Burger { private $size; public function __construct(Size $size) { $this->size = $size; } } class WithTomatoes implements Burger { public function __construct(Burger $burger) { } } class WithCheese implements Burger { public function __construct(Burger $burger) { } }
If you object that Bread is a Burger, how about that:
Bread
Burger
class EmptyBurger implements Burger { private $bread; public function __construct(Bread $bread) { $this->bread = $bread; } } class WithCumin implements Bread { private $size; public function __construct(Size $size) { $this->size = $size; } }
This code is highly composable, which probably means that it's good in terms of OOP.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
If a class has a constructor with a lot of arguments, the problem is in this class. Using a builder pattern just masks the problem, which is even worse. So I consider a builder an anti-pattern. I'd suggest to use a decorator instead:
If you object that
Bread
is aBurger
, how about that:This code is highly composable, which probably means that it's good in terms of OOP.