logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ design patterns: prototype in php

Design patterns: prototype in php

by Admin _ April 17, 2022

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!

*
fabiothiroki / php-design-patterns

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


*

Paracetamol.js

Share Tweet Linkedin Pinterest
Previous Post

Www scimagojr com index php

Next Post

How to install php on ubuntu 18

CÙNG CHUYÊN MỤC

form register php

Form register php

09/04/2021
fantastic blog (cms) in php with source code

Fantastic blog (cms) in php with source code

28/04/2021
validate form php javascript

Validate form php javascript

28/04/2021
http diendanlequydon com viewtopic php style 6

Http diendanlequydon com viewtopic php style 6

28/04/2021
free php encoder

Free php encoder

28/05/2022
xuất file pdf trong php

Xuất file pdf trong php

28/05/2022
php

Php

28/05/2022
học php cần phần mềm gì

Học php cần phần mềm gì

27/05/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Bí quyết bán hàng online thành công

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.