thrashzone13/visitor 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thrashzone13/visitor

最新稳定版本:V1.0.1

Composer 安装命令:

composer require thrashzone13/visitor

包简介

Simple implementation of visitor design pattern

README 文档

README

Latest Version on Packagist Software License Total Downloads

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

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-24