PHP设计模式--适配器模式
声明:本系列博客参考资料《大话设计模式》,作者程杰。
适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的(适配器模式要解决的核心问题)。一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,做法是将类自己的接口包裹在一个已存在的类中。
类图:
待适配(ForeignPlayer)角色:此角色的接口规则内部的接口规则不一致,但内部需要调用该角色的方法功能。
内部接口(IPlayer)角色:这是一个抽象角色,此角色给出内部期待的接口规则。
适配器(Adapter)角色:通过在内部包装一个Adapter对象,把待适配接口转换成目标接口,此角色为适配器模式的核心角色,也是适配器模式所解决问题的关键。
代码:
<?php /** * Created by PhpStorm. * User: Jiang * Date: 2015/4/26 * Time: 12:23 *///-------------抽象接口---------------/**抽象运动员 * Interface IPlayer */interface IPlayer{ function Attack(); function Defense();}/**前锋 * Class Forward */class Forward implements IPlayer{ function Attack() { echo "前锋攻击<br/>"; } function Defense() { echo "前锋防御<br>"; }}/**中锋 * Class Center */class Center implements IPlayer{ function Attack() { echo "中锋攻击<br>"; } function Defense() { echo "中锋防御<br>"; }}//--------------待适配对象-----------/**姚明 外籍运动员 * Class Yaoming */class Yaoming{ function 进攻() { echo "姚明进攻<br>"; } function 防御() { echo "姚明防御<br>"; }}//------------适配器--------------/**适配器 * Class Adapter */class Adapter implements IPlayer{ private $_player; function __construct() { $this->_player=new Yaoming(); } function Attack() { $this->_player->进攻(); } function Defense() { $this->_player->防御(); }}
客户端测试代码:
header("Content-Type:text/html;charset=utf-8");//------------------------原型模式测试代码------------------require_once "./Adapter/Adapter.php";$player1=new Forward();echo "前锋上场:<br>";$player1->Attack();$player1->Defense();echo "<hr><br>";echo "姚明上场:<br>";$yaoming=new Adapter();$yaoming->Attack();$yaoming->Defense();
适用场景
1.接口中规定了所有要实现的方法
2.但要有一个实现此接口的具体类,只用到了其中的几个方法,而其它的方法都是没有用的。
注意事项
1.充当适配器角色的类就是实现已有接口的抽象类
2.为什么要用抽象类:
此类是不要被实例化的。而只充当适配器的角色,也就为其子类提供了一个共同的接口,但其子类又可以将精力只集中在其感兴趣的地方。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.
