Home > Backend Development > PHP Tutorial > Detailed explanation of the proxy pattern of PHP design patterns

Detailed explanation of the proxy pattern of PHP design patterns

韦小宝
Release: 2023-03-17 13:38:01
Original
1732 people have browsed it

In software development, there is also a design pattern that can provide similar functions to purchasing websites. For some reasons, the client does not want or cannot directly access an object. At this time, indirect access can be achieved through a third party called a "proxy". The corresponding design pattern of this solution is called proxy mode

Provides a proxy or placeholder for a certain object, and the proxy object controls the original object Access

Detailed explanation of the proxy pattern of PHP design patterns

<?php
/*
 * 代理模式
 */

//代理抽象接口
interface shop
{
    public function buy($title);
}

//原来的CD商店,被代理对象
class CDShop implements shop
{
    public function buy($title)
    {
        echo "购买成功,这是你的《{$title}》唱片" . PHP_EOL;
    }
}

//CD代理
class Proxy implements shop
{
    public function buy($title)
    {
        $this->go();
        $CDshop = new CDshop;
        $CDshop->buy($title);
    }

    public function go()
    {
        echo "跑去香港代购" . PHP_EOL;
    }
}

class client
{
    public static function shoping($goods)
    {
        $proxy = new Proxy;
        $proxy->buy($goods);
    }
}

//许多年后你想买张 醒着做梦 找不到CD商店了,和做梦似的,不得不找了个代理去香港帮你代购。

client::shoping("醒着做梦");
Copy after login

The proxy pattern is one of the commonly used structural design patterns. It provides a solution for indirect access to objects and can access objects For control, the design mode is suitable for people with experience or good foundation to see

Related recommendations:

PHP proxy mode sample code sharing

16php proxy mode

StarCraft php proxy mode

The above is the detailed content of Detailed explanation of the proxy pattern of PHP design patterns. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template