PHP Classes

Learn How to Implement a PHP Strategy Pattern for Contests with the Package Strategy Pattern Example: Add a competition strategy to model classes

Recommend this page to a friend!
  Info   View files Documentation   View files View files (12)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-06-14 (15 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
strategy-pattern-exa 1.0MIT/X Consortium ...7Libraries, Design Patterns, PHP 7
Description 

Author

This package can add a competition strategy to model classes.

It provides abstract classes to implement strategy and contest strategy classes that can execute strategy-specific features on Laravel model classes, such as the validation strategy feature and view section strategy feature.

The package also provides an abstract strategy feature class and two examples of strategy classes.

Picture of András Zoltán-Gyárfás
Name: András Zoltán-Gyárfás <contact>
Classes: 5 packages by
Country: Romania Romania
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Documentation

This code contains a startegy pattern implemented for a contest management system.

It is the implementation of the backend of a competition management system, where the administrators can create different predefined competitions, and the backend logic and also the UI will differ from strategy to strategy.

The App\Models\Contest has a strategy property stored in the database, and set by the Administrators on contest creation, which holds the key of the applied strategy. This adds the functionality and UI logic to the contest.

The relevant part of the Contest model is as follows:

<?php

namespace App\Models;

use App\Extensions\Contests\ContestStrategy;
use App\Extensions\Contests\ContestStrategyMap;
use Illuminate\Database\Eloquent\Model;

class Contest extends Model
{
    public function getStrategy(): ContestStrategy
    {
        $map = ContestStrategyMap::create();
        return $map->getStrategyForKey($this->strategy, $this);
    }
}

The details of the functionalities of each of the strategy are stored in the App/Extensions/Contests/Strategies/*/Features folders in various classes like ValidationRules, ViewSection, etc., and loaded through the code as follows:

$validationRules = $contest->getStrategy()->executeFeature('validation-rules');

// or

$contestViewSection = $contest->getStrategy()->executeFeature('view-section');
$listElement = $contestViewSection?->getSectionByKey('list-element', compact($product));

Also, the NoFeatureImplementedForStrategyException it is handled in app/Exceptions/Handler.php.


  Files folder image Files  
File Role Description
Files folder imageStrategies (3 files, 2 directories)
Plain text file AbstractContestStrategy.php Class Class source
Plain text file ContestStrategyMap.php Class Class source
Plain text file HasContestInterface.php Class Class source
Accessible without login Plain text file readme.md Doc. Documentation
Plain text file StrategyFeatureInterface.php Class Class source

  Files folder image Files  /  Strategies  
File Role Description
Files folder imagebeverage (1 directory)
Files folder imagenektaria (1 directory)
  Plain text file AbstractStrategyFeature.php Class Class source
  Plain text file Beverage.php Class Class source
  Plain text file Nektaria.php Class Class source

  Files folder image Files  /  Strategies  /  beverage  
File Role Description
Files folder imageFeatures (2 files)

  Files folder image Files  /  Strategies  /  beverage  /  Features  
File Role Description
  Plain text file ValidationRules.php Class Class source
  Plain text file ViewSection.php Class Class source

  Files folder image Files  /  Strategies  /  nektaria  
File Role Description
Files folder imageFeatures (2 files)

  Files folder image Files  /  Strategies  /  nektaria  /  Features  
File Role Description
  Plain text file ValidationRules.php Class Class source
  Plain text file ViewSection.php Class Class source

 Version Control Unique User Downloads  
 100%
Total:0
This week:0