😒Proxy in PHP (ok)

https://sourcemaking.com/design_patterns/proxy/php

C:\xampp8\htdocs\lva\index.php

<?php
include("Book.php");
include("BookList.php");
include("ProxyBookList.php");
writeln('BEGIN TESTING PROXY PATTERN');
writeln('');
$proxyBookList = new ProxyBookList();
$inBook = new Book('PHP for Cats', 'Larry Truett');
$proxyBookList->addBook($inBook);
writeln('test 1 - show the book count after a book is added');
writeln($proxyBookList->getBookCount());
writeln('');
writeln('test 2 - show the book');
$outBook = $proxyBookList->getBook(1);
writeln($outBook->getAuthorAndTitle());
writeln('');
$proxyBookList->removeBook($outBook);
writeln('test 3 - show the book count after a book is removed');
writeln($proxyBookList->getBookCount());
writeln('');
writeln('END TESTING PROXY PATTERN');
function writeln($line_in)
{
  echo $line_in . "<br/>";
}

C:\xampp8\htdocs\lva\Book.php

C:\xampp8\htdocs\lva\BookList.php

C:\xampp8\htdocs\lva\ProxyBookList.php

Trong mẫu proxy, một lớp đại diện cho và xử lý tất cả quyền truy cập vào lớp khác.

Điều này có thể là do đối tượng thực ở một vị trí khác (máy chủ, nền tảng, v.v.), đối tượng thực sử dụng nhiều cpu hoặc bộ nhớ để tạo và chỉ được tạo nếu cần thiết hoặc để kiểm soát quyền truy cập vào đối tượng thực. Một proxy cũng có thể được sử dụng để thêm chức năng truy cập bổ sung, chẳng hạn như ghi lại số lần chủ thể thực sự được gọi.

Trong ví dụ này, ProxyBookList được tạo thay cho BookList sử dụng nhiều tài nguyên hơn. ProxyBookList sẽ chỉ khởi tạo BookList khi lần đầu tiên một phương thức trong BookList được gọi.

In the proxy pattern one class stands in for and handles all access to another class.

This can be because the real subject is in a different location (server, platform, etc), the real subject is cpu or memory intensive to create and is only created if necessary, or to control access to the real subject. A proxy can also be used to add additional access functionality, such as recording the number of times the real subject is actually called.

In this example, the ProxyBookList is created in place of the more resource intensive BookList. ProxyBookList will only instantiate BookList the first time a method in BookList is called.

Output

Last updated