thrashzone13/visitor
最新稳定版本:V1.0.1
Composer 安装命令:
composer require thrashzone13/visitor
包简介
Simple implementation of visitor design pattern
关键字:
README 文档
README
This package provides a visitor pattern implementation.
Visitor Pattern
Visitor is a behavioral design pattern that lets you separate algorithms from the objects on which they operate.
Install
Via Composer
$ composer require thrashzone13/visitor
Usage
Consider having an array of different kinds of shapes
$shapes = [ new Circle(radius: 10), new Rectangle(width: 15, height: 20), new Rectangle(width: 10, height: 14), new Square(side: 16) ];
Let's say the intention is to calculate their area and sum them up. There can be a visitor which does the calculation regarding the type of the received instance
$visitor = (new Visitor) ->add(fn(Circle $circle) => pi() * $circle->getRadius() * $circle->getRadius()) ->add(fn(Square $square) => $square->getSide() * $square->getSide()) ->add(fn(Rectangle $rectangle) => $rectangle->getWidth() * $rectangle->getHeight());
Now it's ready to use!
$totalArea = 0; foreach ($shapes as $shape) { $totalArea += $visitor->visit($shape); }
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 36
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-24