Home Backend Development PHP Tutorial php设计模式 Proxy (代理模式)_php技巧

php设计模式 Proxy (代理模式)_php技巧

May 17, 2016 am 09:17 AM
php design patterns proxy proxy mode

代理,指的就是一个角色代表另一个角色采取行动,就象生活中,一个红酒厂商,是不会直接把红酒零售客户的,都是通过代理来完成他的销售业务。而客户,也不用为了喝红酒而到处找工厂,他只要找到厂商在当地的代理就行了,具体红酒工厂在那里,客户不用关心,代理会帮他处理。

代理模式,就是给某一对象提供代理对象,并由代理对象控制具体对象的引用。

代理模式涉及的角色:

抽象主题角色,声明了代理主题和真实主题的公共接口,使任何需要真实主题的地方都能用代理主题代替。
代理主题角色,含有真实主题的引用,从而可以在任何时候操作真实主题,代理主题功过提供和真实主题相同的接口,使它可以随时代替真实主题。代理主题通过持有真实主题的引用,不但可以控制真实主题的创建或删除,可以在真实主题被调用前进行拦截,或在调用后进行某些操作。
真实代理对象,定义了代理角色所代表的具体对象。
参考一下代码:

复制代码 代码如下:

/**
* 代理模式
*
* 为其他对象提供一个代理以控制这个对象的访问
*
*/
interface Proxy
{
public function request();
public function display();
}

class RealSubject
{
public function request()
{
echo "RealSubject request
";
}

public function display()
{
echo "RealSubject display
";
}
}

class ProxySubject
{
private $_subject = null;
public function __construct()
{
$this->_subject = new RealSubject();
}

public function request()
{
$this->_subject->request();
}

public function display()
{
$this->_subject->display();
}
}

$objProxy = new ProxySubject();
$objProxy->request();
$objProxy->display();

代理模式的工作方式:首先,因为代理主题和真实主题都实现了共同的接口,这使我们可以在不改变原来接口的情况下,只要用真实主题对象的地方,都可以用代理主题来代替。其次,代理主题在客户和真实主题之间起了一个中介作用,利用这个中介平台,我们可以在把客户请求传递给真实主题之前做一些必要的预处理。

还有一个很常见的代理模式的使用例子就是对大幅图片浏览的控制。在我们常见的网站上面浏览图文的信息时,不知道你有没有注意到,图片位置放置的是经过缩小的,当有人要仔细的查看这个图片时,可以通过点击图片来激活一个链接,在一个新的网页打开要看的图片 。这样对于提高浏览速度是很有好处的,因为不是每个人都要去看仔细图上的信息。这种情况就可以使用代理模式来全面实现。这里我将思路表述出来,至于实现由于工作原因,就不表述了,至于这种方式在B/S模式下的真实可行性,我没有确认过,只是凭空的想象。如果不是可行的方式,那这个例子可以放到一个C/S下来实现,这个是绝对没有问题的,而且在很多介绍设计模式的书和文章中使用。两种方式的实现有兴趣的可以来尝试一下:)

我们在浏览器中访问网页时是调用的不是真实的装载图片的方法,而是在代理对象中的方法,在这个对象中,先使用一个线程向浏览器装载了一个缩小版的图片,而在后台使用另一个线程来调用真实的装载大图片的方法将图片加载到本地,当你要浏览这个图片的时候,将其在新的网页中显示出来。当然如果在你想浏览的时候图片尚未加载成功,可以再启动一个线程来显示提示信息,直到加载成功。

这样代理模式的功能就在上面体现的淋漓尽致——通过代理来将真实图片的加载放到后台来操作,使其不影响前台的浏览。

代理模式能够协调调用者和被调用者,能够在一定程度上降低系统的耦合度。不过一定要记住前面讲的使用代理模式的条件,不然的话使用了代理模式不但不会有好的效果,说不定还会出问题的。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Nginx Proxy Manager configuration analysis and optimization Nginx Proxy Manager configuration analysis and optimization Sep 26, 2023 am 09:24 AM

Overview of NginxProxyManager configuration analysis and optimization: NginxProxyManager is a reverse proxy management tool based on Nginx, which can help us easily configure and manage reverse proxy servers. In the process of using NginxProxyManager, we can improve the performance and security of the server by parsing and optimizing its configuration. Configuration analysis: Configuration file location and structure: NginxProxyManag

How to configure and use proxy protocol in nginx How to configure and use proxy protocol in nginx May 18, 2023 am 08:47 AM

When proxyprotocol is used in nginx, we know that nginx is a web server and proxy server. It generally works behind proxyserver or load balancing software (Haproxy, Amazon Elastic LoadBalancer (ELB)). The client first requests proxyserver or LSB load balancing software, and then to nginx Perform real web access. Because it has gone through multiple layers of software, some client information such as IP address, port number, etc. may be hidden, which is detrimental to our problem analysis and data statistics. Because for nginx, We want to be able to get real clients

Deployment strategy of containers and microservices under Nginx Proxy Manager Deployment strategy of containers and microservices under Nginx Proxy Manager Sep 27, 2023 pm 01:06 PM

The deployment strategy of containers and microservices under NginxProxyManager requires specific code examples. Summary: With the popularity of microservice architecture, containerization technology has become an important part of modern software development. In the microservice architecture, NginxProxyManager plays a very important role, used to manage and proxy the traffic of microservices. This article will introduce how to use NginxProxyManager to deploy and manage containerized microservices, and provide relevant code examples.

Nginx Proxy Manager Tutorial: Quick Start Guide Nginx Proxy Manager Tutorial: Quick Start Guide Sep 27, 2023 pm 05:39 PM

NginxProxyManager Tutorial: Quick Start Guide, Specific Code Examples Needed Introduction: With the development of network technology, proxy servers have become a part of our daily use of the Internet. NginxProxyManager is a proxy server management platform based on Nginx, which can help us quickly establish and manage proxy servers. This article will introduce you to the quick start guide of NginxProxyManager, as well as some specific code examples. one

How to use Nginx Proxy Manager to achieve load balancing of multiple servers How to use Nginx Proxy Manager to achieve load balancing of multiple servers Sep 27, 2023 pm 09:42 PM

How to use NginxProxyManager to achieve load balancing of multiple servers. NginxProxyManager is a proxy server management tool developed based on Nginx. It provides a simple and easy-to-use Web interface that can easily configure and manage Nginx proxy servers. In practical applications, we often need to distribute requests to multiple servers to achieve load balancing and improve system performance and availability. This article will introduce how to use NginxProx

How to use Nginx Proxy Manager to quickly cache HTML pages How to use Nginx Proxy Manager to quickly cache HTML pages Sep 28, 2023 am 10:58 AM

How to use NginxProxyManager to achieve fast caching of HTML pages Introduction: In modern network applications, fast loading of web pages is an important requirement. In order to improve user experience, we can use NginxProxyManager to quickly cache HTML pages. This article will show you how to use NginxProxyManager to achieve this goal, and provide specific code examples. Part 1: Install and configure NginxProxy

Nginx Proxy Manager Principles and Practice: The Key to Optimizing Website Performance Nginx Proxy Manager Principles and Practice: The Key to Optimizing Website Performance Sep 26, 2023 am 11:06 AM

NginxProxyManager Principle and Practice: The key to optimizing website performance, specific code examples are required Introduction: In today's Internet era, website performance is crucial to user experience and search engine optimization. In order to improve the performance of the website, an effective method is to use Nginx as a reverse proxy server to manage and distribute traffic. This article will introduce the principles and practices of NginxProxyManager and show how to optimize the website through NginxProxyManager

Nginx Proxy Manager implements cache acceleration of HTTP requests Nginx Proxy Manager implements cache acceleration of HTTP requests Sep 26, 2023 am 10:53 AM

NginxProxyManager is a tool for managing Nginx proxy servers. By using it, you can achieve cache acceleration of HTTP requests. The following will introduce in detail how to use NginxProxyManager to implement cache acceleration and provide specific code examples. 1. Install and configure NginxProxyManager Install NginxProxyManager: $npminstall-g@nginx

See all articles