Home php教程 php手册 php设计模式 Visitor (访问者模式)

php设计模式 Visitor (访问者模式)

May 21, 2016 am 10:41 AM
Design Patterns visitor mode

25种php设计模式,你全都知道吗?下面用代码介绍访问者模式(Visitor模式)

<?php
/**
 * 访问者模式
 *
 * 表示一个作用于某对象结构中的各元素的操作,可以在不改变各元素的类的前提下定义作用于这些元素的新操作
 *
 */
abstractclassVisitor {
    abstractpublicfunction visitCroncreteElementA($element);
    abstractpublicfunction visitCroncreteElementB($element);
}
class ConcreteVisitor1 extends Visitor {
    public function visitCroncreteElementA($element) {
        echoget_class($element) . " visit 1A<br/>";
    }
    public function visitCroncreteElementB($element) {
        echoget_class($element) . " visit 1B<br/>";
    }
}
class ConcreteVisitor2 extends Visitor {
    public function visitCroncreteElementA($element) {
        echoget_class($element) . " visit 2A<br/>";
    }
    public function visitCroncreteElementB($element) {
        echoget_class($element) . " visit 2B<br/>";
    }
}
abstractclassElement {
    abstractpublicfunction accept($visitor);
}
class ConcreteElementA extends Element {
    public function accept($visitor) {
        $visitor->visitCroncreteElementA($this);
    }
}
class ConcreteElementB extends Element {
    public function accept($visitor) {
        $visitor->visitCroncreteElementB($this);
    }
}
class ObjectStructure {
    private $_elements = array();
    public function attach($element) {
        $this->_elements[] = $element;
    }
    public function detach($element) {
        if ($key = array_search($element, $this->_elements) !== false) unset($this->_elements[$key]);
    }
    public function accept($visitor) {
        foreach ($this->_elements as $element) {
            $element->accept($visitor);
        }
    }
}
//
$objOS = new ObjectStructure();
$objOS->attach(new ConcreteElementA());
$objOS->attach(new ConcreteElementB());
$objCV1 = new ConcreteVisitor1();
$objCV2 = new ConcreteVisitor2();
$objOS->accept($objCV1);
$objOS->accept($objCV2);
Copy after login


其他相关设计模式:

备忘录模式(Memento模式)
观察者模式(Observer模式)
模板方法模式(Template Method模式)
命令模式(command模式)
组合模式(composite模式)
享元模式(flyweight模式)
策略模式(strategy模式)
状态模式(state模式)
适配器模式(adapter模式)
工厂模式(factory模式)
原型模式(prototype模式)
外观模式(facade模式)
单例模式(singleton模式)
桥梁模式(bridge模式)
装饰模式(decorator模式)
抽象工厂模式(abstract factory模式)
建造者模式(Builder模式)
访问者模式(Visitor模式)
解释器模式(Interpreter模式)
中介者模式(Mediator模式)
职责链模式(Chain Of Responsibility模式)
代理模式(Proxy模式)
迭代器模式(Interator模式)
数据访问对象模式(DAO模式)
委托模式(Delegation模式)


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 Article

Hot Article

Hot Article Tags

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)

The difference between design patterns and architectural patterns in Java framework The difference between design patterns and architectural patterns in Java framework Jun 02, 2024 pm 12:59 PM

The difference between design patterns and architectural patterns in Java framework

The wonderful use of the adapter pattern in Java design patterns The wonderful use of the adapter pattern in Java design patterns May 09, 2024 pm 12:54 PM

The wonderful use of the adapter pattern in Java design patterns

Analysis of the Decorator Pattern in Java Design Patterns Analysis of the Decorator Pattern in Java Design Patterns May 09, 2024 pm 03:12 PM

Analysis of the Decorator Pattern in Java Design Patterns

PHP design pattern practical case analysis PHP design pattern practical case analysis May 08, 2024 am 08:09 AM

PHP design pattern practical case analysis

What are the advantages and disadvantages of using design patterns in java framework? What are the advantages and disadvantages of using design patterns in java framework? Jun 01, 2024 pm 02:13 PM

What are the advantages and disadvantages of using design patterns in java framework?

PHP Design Patterns: Test Driven Development in Practice PHP Design Patterns: Test Driven Development in Practice Jun 03, 2024 pm 02:14 PM

PHP Design Patterns: Test Driven Development in Practice

Application of design patterns in Guice framework Application of design patterns in Guice framework Jun 02, 2024 pm 10:49 PM

Application of design patterns in Guice framework

How design patterns deal with code maintenance challenges How design patterns deal with code maintenance challenges May 09, 2024 pm 12:45 PM

How design patterns deal with code maintenance challenges

See all articles