php装饰模式
/** * 抽象构件角色 */ interface Component { /** * 示例方法 */ public function operation(); } /** * 装饰角色 */ abstract class Decorator implements Component{ protected $_component; public function __construct(Component $component) { $this->_component = $component; } public function operation() { //通过对象委派操作 $this->_component->operation(); } } /** * 具体装饰类A */ class ConcreteDecoratorA extends Decorator { public function __construct(Component $component) { parent::__construct($component); } public function operation() { parent::operation(); // 调用装饰类的操作 $this->addedOperationA(); // 新增加的操作 } /** * 新增加的操作A,即装饰上的功能 */ public function addedOperationA() { echo 'Add Operation A '; } } /** * 具体装饰类B */ class ConcreteDecoratorB extends Decorator { public function __construct(Component $component) { parent::__construct($component); } public function operation() { parent::operation();//父类根据对象进行委派 $this->addedOperationB(); } /** * 新增加的操作B,即装饰上的功能 */ public function addedOperationB() { echo 'Add Operation B '; } } /** * 具体构件 */ class ConcreteComponent implements Component{ public function operation() { echo 'Concrete Component operation '; } } /** * 客户端 */ class Client { /** * Main program. */ public static function main() { $component = new ConcreteComponent(); $decoratorA = new ConcreteDecoratorA($component); $decoratorB = new ConcreteDecoratorB($decoratorA); $decoratorA->operation(); $decoratorB->operation(); } } Client::main(); 链接地址:http://www.phppan.com/2010/06/php-design-pattern-4-decorator/
Copy after login
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
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
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
1 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Where to find the Crane Control Keycard in Atomfall
1 weeks ago
By DDD

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
CakePHP Tutorial
1359
52

