Let"s suppose you are a darkedeneurope.comeloper working on an eCommerce, và you"re given the responsibility for this feature:
In the Cart screen, user should be able to add more of an existing sản phẩm there, và change its color.
Bạn đang xem: Design patterns: prototype in php
One intuitive solution in this context could be instantiating a new hàng hóa object, and set all attributes khổng lồ be equal the original product. But this seems too verbose, and you also have to lớn know all the internals of the hàng hóa code, which seems lượt thích breaking the encapsulation.
Prototype to lớn the rescue!

A collection of design patterns written in PHP
The Cart class doesn"t make part of the design pattern but it"s here just to lớn demonstrate how it fits in a real world application:
final class Cart /** *
var ProductPrototype<> */ private array $products; public function addProduct(ProductPrototype $product): void $this->products<> = $product; /** *
return ProductPrototype<> */ public function getProducts(): array return $this->products;
Enter fullscreen mode Exit fullscreen mode
As you can see, we are able to địa chỉ a new product and see which products are already there.
Xem thêm: phần mềm jacket
abstract class ProductPrototype protected int $id; protected string $name; protected string $color; public function getId(): int return $this->id; public function getName(): string return $this->name; public function getColor(): string return $this->color; public function setColor(string $color): void $this->color = $color;
Enter fullscreen mode Exit fullscreen mode
PHP already implements the Prototype design pattern natively because every object already has a built-in clone operator support. In this example, I created the ProductPrototype class just to illustrate a real use case: for Cart class is doesn"t matter which specific kind of product it has, as long as it has the attributes id, name and color.
Just khổng lồ simplify this example, I will showroom only two types of product: điện thoại cảm ứng and Laptop:
final class smartphone extends ProductPrototype public function __construct() $this->id = 1; $this->name = "Smartphone"; $this->color = "Default color"; final class máy vi tính extends ProductPrototype public function __construct() $this->id = 2; $this->name = "Smartphone"; $this->color = "Default color";
Enter fullscreen mode Exit fullscreen mode
Explaining Prototype with Unit test

Thanks for reading & hope you could get a better picture of this kiến thiết pattern and will remember khổng lồ use it whenever the opportunity appears!
Discussion (0)
Subscribe

Upload image
Templates Editor guide
Personal Moderator

Create template Templates let you quickly answer FAQs or store snippets for re-use.
Submit Preview Dismiss
Code of Conduct • Report abuse
Are you sure you want to lớn 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
Read next

How lớn seed your darkedeneurope.com Postgres DB with your prod DB with RepliByte
Romaric p - Mar 31
