{"id":20162485,"url":"https://github.com/willavelar/php-design-pattern-behavioral-strategy","last_synced_at":"2025-03-03T02:45:10.541Z","repository":{"id":191259251,"uuid":"684278539","full_name":"willavelar/php-design-pattern-behavioral-strategy","owner":"willavelar","description":"a simple php example about the behavioral pattern: Strategy","archived":false,"fork":false,"pushed_at":"2023-09-17T19:40:41.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-13T14:19:13.626Z","etag":null,"topics":["behavioral-patterns","design-patterns","php","strategy"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willavelar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-28T20:14:58.000Z","updated_at":"2023-10-31T22:10:08.000Z","dependencies_parsed_at":"2024-11-14T00:38:51.426Z","dependency_job_id":null,"html_url":"https://github.com/willavelar/php-design-pattern-behavioral-strategy","commit_stats":null,"previous_names":["willavelar/php-design-partner-behavioral-strategy","willavelar/php-design-pattern-behavioral-strategy"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-strategy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-strategy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-strategy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willavelar%2Fphp-design-pattern-behavioral-strategy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willavelar","download_url":"https://codeload.github.com/willavelar/php-design-pattern-behavioral-strategy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241600484,"owners_count":19988713,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["behavioral-patterns","design-patterns","php","strategy"],"created_at":"2024-11-14T00:25:20.385Z","updated_at":"2025-03-03T02:45:10.524Z","avatar_url":"https://github.com/willavelar.png","language":"PHP","readme":"## Strategy\r\n\r\nStrategy is a behavioral pattern, often used when we have rules for a particular activity that may contain a lot of logic, it organizes and separates the use of these logics, standardizing the usability of classes so that new implementations can be added in the future without much change in the use of a particular class of action.\r\n\r\n-----\r\n\r\nWe need to create a tax calculator. With this, we will have our budget and it will show the specific amount after calculating the taxes.\r\n\r\n### The problem\r\n\r\nIf we do it this way, we have two problems: we are dependent on a string parameter with the name of the tax. If it is passed incorrectly, an error will be returned. In addition, with each new tax, we will have to modify the file by adding another \"if\", which can generate additional bugs.\r\n\r\n```php\r\n\u003c?php\r\nclass Budget \r\n{\r\n    public float $value;\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass TaxCalculator\r\n{\r\n    public function calc(Budget $budget, string $taxName) : float\r\n    {\r\n        switch ($taxName) {\r\n            case \"ICMS\":\r\n                return $budget-\u003evalue * 0.1;\r\n            case \"ISS\":\r\n                return $budget-\u003evalue * 0.06;\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n### The solution\r\n\r\nNow, using the Strategy pattern, we create an interface that serves as a contract for all taxes, with a standardized calculation method. Each new tax just needs to create a new file that will be called when referring to it.\r\n\r\n```php\r\n\u003c?php\r\ninterface Tax\r\n{\r\n    public function calculateTax(Budget $budget) : float;\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass Icms implements Tax\r\n{\r\n    public function calculateTax(Budget $budget) : float\r\n    {\r\n        return $budget-\u003evalue * 0.1;\r\n    }\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass Iss implements Tax\r\n{\r\n    public function calculateTax(Budget $budget) : float\r\n    {\r\n        return $budget-\u003evalue * 0.06;\r\n    }\r\n}\r\n```\r\n```php\r\n\u003c?php\r\nclass TaxCalculator\r\n{\r\n    public function calc(Budget $budget, Tax $tax) : float\r\n    {\r\n        return $tax-\u003ecalculateTax($budget);\r\n    }\r\n}\r\n```\r\n-----\r\n\r\n### Installation for test\r\n\r\n![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)\r\n\r\n```bash\r\ncomposer install\r\n```\r\n\r\n```bash\r\nphp wrong/test.php\r\nphp right/test.php\r\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillavelar%2Fphp-design-pattern-behavioral-strategy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillavelar%2Fphp-design-pattern-behavioral-strategy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillavelar%2Fphp-design-pattern-behavioral-strategy/lists"}