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   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-06-14 (3 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 29 All time: 11,144 This week: 47Up
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.

Innovation Award
PHP Programming Innovation award nominee
June 2024
Number 3
The strategy pattern is a design pattern that developers can use to define multiple policies that applications can select at run time to apply to objects.

This package provides an example of the strategy pattern to contests with features implemented by Laravel model classes.

Manuel Lemos
Picture of András Zoltán-Gyárfás
Name: András Zoltán-Gyárfás is available for providing paid consulting. Contact András Zoltán-Gyárfás .
Classes: 7 packages by
Country: Romania Romania
Age: 44
All time rank: 184441 in Romania Romania
Week rank: 180 Up4 in Romania Romania Up
Innovation award
Innovation award
Nominee: 4x

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 (12)  
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 (12)  /  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 (12)  /  Strategies  /  beverage  
File Role Description
Files folder imageFeatures (2 files)

  Files folder image Files (12)  /  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 (12)  /  Strategies  /  nektaria  
File Role Description
Files folder imageFeatures (2 files)

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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:29
This week:0
All time:11,144
This week:47Up