search
HomeBackend DevelopmentPHP ProblemThe application of adapter mode and strategy mode in PHP
The application of adapter mode and strategy mode in PHPJun 23, 2021 pm 03:17 PM
strategy patternadapter

There are too many patterns in PHP that we need to understand and master. I wonder how many patterns you have mastered after studying for so long? How much do you know about the use of adapter mode and strategy mode in PHP? This article will teach you about the use of adapter mode and strategy mode in PHP. Let’s learn together

Related recommendations:What is the observer pattern in PHP? How to use it?

Adapter:

There are many adapters in life. The power adapter is the transformer inside 220v

can convert - a class The interface is converted into another interface that the customer wants, so that the originally incompatible interfaces can work together. The popular understanding is to adapt different interfaces into a unified interface

Similarly, we create a new file and define a class class. There is a method in the class class. We know such a class, but we only have one Method, and at this time we need to expose an interface to the outside world, so how do we implement the adapter pattern through a class? We continue to write a class, because the following class is a new class we added, so we can write any member method, And when we create an object, we need to save the object passed in; let’s take the code as an example:

<?php
interface PerfectMan
{
  function cook();
  function writePhp();
}
class Wife
{
  function cook( )
{
  echo &#39;最犀利的剑,只为最强大的手所挥动<br />&#39;;
}
}
class Man implements PerfectMan
{
  protected $wife;
  //在创建对象的时候保存传递进来的对象
  function __onstruct($wife)
  {
  $this->wife = $wife;
  }
  function cook( )
  {
    $this->wife->cook();
  }
  function writePhp()
  {
    echo &#39;我会写php代码<br />&#39;;
}
}
$li = new Wife();
$ming = new Man( ) ;
$ming->writePhp();
$ming->cook();

Strategy mode:

(1) Multiple classes only distinguish When expressing different behaviors, you can use Strategy mode to dynamically select the specific

behavior to be executed at runtime.

(2) Different strategies (algorithms) need to be used in different situations, or the strategy may be implemented in other ways

in the future.

(3) The implementation details of specific strategies (algorithms) are hidden from customers and are completely independent of each other.

(4) The client must know all the strategy classes and decide which one to use. The strategy mode is only applicable

when the client knows all the algorithms or behaviors. .

(5) The strategy model creates many strategy classes, and each specific strategy class will generate a new class.

Advantages:

1. Strategy mode provides a way to manage related algorithm families

2. The algorithm is enclosed in an independent Strategy class so that You can change it independently of its Context

3. Using strategy mode can avoid using multiple conditional transfer statements

The code is as follows:

<?php
interface Love
{
  function sajiao();
}
class KeAi implements Love
{
  function sajiao( )
{
  echo &#39;心怀不惧,才能翱翔于天际<br />&#39;;
}
}
class Tiger implements Love
{
  function sajiao( )
{
  echo &#39;智商太低会传染,离我远点<br />&#39; ;
}
}
class GirlFriend
{
  protected $xingge;
  function __construct($xingge)
{
  $this->xingge = $xingge ;
}
  function sajiao( )
{
  $this->xingge->sajiao();
}
}
$keai = new KeAi;
$li = new GirlFriend($keai);
$li ->sajiao();

Execution result As follows:

The application of adapter mode and strategy mode in PHP

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of The application of adapter mode and strategy mode in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
在 Windows 11 中如何启用/禁用局域网唤醒功能在 Windows 11 中如何启用/禁用局域网唤醒功能Sep 06, 2023 pm 02:49 PM

LAN唤醒是Windows11上的一项网络功能,有了它,您可以远程将电脑从休眠或睡眠模式唤醒。虽然临时用户不经常使用它,但此功能对于使用有线网络的网络管理员和高级用户非常有用,今天我们将向您展示如何设置它。如何知道我的计算机是否支持LAN唤醒?要使用此功能,您的计算机需要以下各项:PC需要连接到ATX电源,以便您可以远程将其从睡眠模式唤醒。需要创建访问控制列表并将其添加到网络中的所有路由器。网卡需要支持唤醒局域网功能。要使此功能正常工作,两台计算机需要位于同一网络上。虽然大多数以太网适配器通过以

USB转HDMI适配器在Windows 11/10上无法正常工作USB转HDMI适配器在Windows 11/10上无法正常工作Feb 19, 2024 pm 12:00 PM

如果您在Windows上遇到USB转HDMI适配器无法正常工作的问题,可能是因为显卡驱动程序过时或适配器本身存在问题。以下内容将帮助您解决这一困扰。修复USB转HDMI适配器在Windows11/10上无法工作使用以下解决方案修复USB转HDMI适配器在Windows10/11上无法正常工作:更新显示适配器驱动程序回滚显卡驱动程序拔下并插入USB转HDMI适配器已更新BIOS我们开始吧。1]更新显示适配器驱动程序问题可能是由于过时或损坏的显示适配器驱动程序引起的。您可以尝试从制造商的官方网站下载

Microsoft Wi-Fi 直连虚拟适配器:它有什么作用?Microsoft Wi-Fi 直连虚拟适配器:它有什么作用?Jun 29, 2023 pm 12:33 PM

您可能想知道MicrosoftWi-Fi直接虚拟适配器在您的PC上的作用。最好放心,此网络适配器Microsoft且完全安全。但是,如果适配器弄乱了您的活动,并且您想知道它是否值得保留,本指南将为您提供您需要了解的所有信息。MicrosoftWi-FiDirect虚拟适配器有什么作用?顾名思义,MicrosoftWi-Fi直接虚拟适配器有助于使您的PC成为无线热点。这样,其他计算机可以连接到您的PC以访问互联网。它通过虚拟化无线网络适配器来实现此目的。这样,您的单个物理无线适配器将转换为两个虚拟

如何在 Windows 11 上检查网络连接详细信息和状态如何在 Windows 11 上检查网络连接详细信息和状态Sep 11, 2023 pm 02:17 PM

为了确保您的网络连接正常工作或解决问题,有时您需要检查Windows11上的网络连接详细信息。通过这样做,您可以查看各种信息,包括您的IP地址、MAC地址、链接速度、驱动程序版本等,在本指南中,我们将向您展示如何做到这一点。如何在Windows11上查找网络连接详细信息?1.使用“设置”应用按+键打开Windows设置。WindowsI接下来,导航到左窗格中的网络和互联网,然后选择您的网络类型。在我们的例子中,这就是以太网。如果您使用的是无线网络,请改为选择Wi-Fi网络。在屏幕底部,您应该会看

Win7无线网络适配器不见了Win7无线网络适配器不见了Dec 23, 2023 pm 05:57 PM

很多用户都反馈说win7无线网络适配器消失了,遇到这种情况很可能是网卡或者网卡驱动出现了问题,我们只需要修复一下网卡驱动或者更换一个好的网卡就可以解决。win7无线网络适配器消失了1、首先右键开始按键,选择“设备管理器”2、接着我们找到“网络适配器”,右键可以选择先更新一下驱动,或者是先禁用设备再启用,也可以在这里点击“属性”3、点击属性之后,再点击上方的“驱动程序”,在这里点击“更新驱动程序”4、点击后这里再选择”自动搜索更新驱动文件“5、完成之后也可以把电脑的网络重置一下。首先还是按下win

iOS 17 更新后,您的 iPhone 无法充电?这是你可以做的iOS 17 更新后,您的 iPhone 无法充电?这是你可以做的Sep 21, 2023 pm 11:41 PM

iOS17充电问题的原因是什么?更新到iOS17后,您的iPhone可能无法充电有几个可能的原因。一种可能性是软件更新中存在错误。Apple通常会快速修复iOS更新中的错误,因此,如果您遇到充电问题,则值得检查是否有新的更新。另一种可能性是充电电缆或适配器有问题。如果您使用的是第三方充电连接线或适配器,请确保它已通过Apple认证。如何修复iPhone不充电问题以下是有关如何修复iOS17更新后iPhone无法充电问题的一些提示:重启你的苹果手机这通常可以解决可能是iOS17充电问题根本原因的小

修复:系统驱动程序被锁定到制造商规格修复:系统驱动程序被锁定到制造商规格Sep 23, 2023 pm 07:25 PM

图形驱动程序是PC上最重要的驱动程序之一,直接影响性能和游戏体验。但是,当通过安装程序更新驱动程序时,许多人遇到“您的系统具有锁定到制造商规格的驱动程序”错误。出现此问题的原因是制造商限制更新,除非从其末端专门推送更新。这确保了稳定性,但对许多人来说可能是一个问题。因此,让我们找出如何立即解决问题!如何修复您的系统具有锁定在Windows11上的制造商规范的驱动程序?在我们转向稍微复杂的解决方案之前,请先尝试以下快速解决方案:确保你的电脑和操作系统满足驱动程序的系统要求。将电脑启动到安全模式,然

java框架中策略模式的实际应用案例有哪些?java框架中策略模式的实际应用案例有哪些?Jun 05, 2024 pm 08:44 PM

Java框架中策略模式用于动态更改类行为,具体应用包括:Spring框架:数据验证和缓存管理JakartaEE框架:事务管理和依赖注入JSF框架:转换器和验证器、响应生命周期管理

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),