Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/willavelar/php-design-pattern-structural-proxy

About a simple php example about the structural pattern: Proxy
https://github.com/willavelar/php-design-pattern-structural-proxy

design-patterns php proxy structural-patterns

Last synced: 6 days ago
JSON representation

About a simple php example about the structural pattern: Proxy

Awesome Lists containing this project

README

        

## Proxy

Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.

-----

We need to create a discount calculator, where the calculation makes a request to an external API

### The problem

If we do it this way, we are held hostage by the API if it takes too long

```php
value * 0.1;

(new RedisLog())->save($discountCalc);

return $discountCalc;
}
}
```
```php
discountCalculator = $discountCalculator;
}

public function calc(Budget $budget): float
{
if ($this->calcProxy == 0) {
$this->calcProxy = $this->discountCalculator->calc($budget);
}

return $this->calcProxy;
}
}
```

-----

### Installation for test

![PHP Version Support](https://img.shields.io/badge/php-7.4%2B-brightgreen.svg?style=flat-square) ![Composer Version Support](https://img.shields.io/badge/composer-2.2.9%2B-brightgreen.svg?style=flat-square)

```bash
composer install
```

```bash
php wrong/test.php
php right/test.php
```