Thursday, 19 September 2024

Apply SOLID principles and design patterns Strategy Pattern

<?php

declare(strict_types=1);

namespace WolfShop;

final class WolfService
{
    /**
     * @param Item[] $items
     */
    public function __construct(
        private array $items
    ) { }

    public function updateQuality(): void
    {
        foreach ($this->items as $item) {
            if ($item->name != 'Apple AirPods' and $item->name != 'Apple iPad Air') {
                if ($item->quality > 0) {
                    if ($item->name != 'Samsung Galaxy S23') {
                        $item->quality = $item->quality - 1;
                    }
                }
            } else {
                if ($item->quality < 50) {
                    $item->quality = $item->quality + 1;
                    if ($item->name == 'Apple iPad Air') {
                        if ($item->sellIn < 11) {
                            if ($item->quality < 50) {
                                $item->quality = $item->quality + 1;
                            }
                        }
                        if ($item->sellIn < 6) {
                            if ($item->quality < 50) {
                                $item->quality = $item->quality + 1;
                            }
                        }
                    }
                }
            }

            if ($item->name != 'Samsung Galaxy S23') {
                $item->sellIn = $item->sellIn - 1;
            }

            if ($item->sellIn < 0) {
                if ($item->name != 'Apple AirPods') {
                    if ($item->name != 'Apple iPad Air') {
                        if ($item->quality > 0) {
                            if ($item->name != 'Samsung Galaxy S23') {
                                $item->quality = $item->quality - 1;
                            }
                        }
                    } else {
                        $item->quality = $item->quality - $item->quality;
                    }
                } else {
                    if ($item->quality < 50) {
                        $item->quality = $item->quality + 1;
                    }
                }
            }
        }
    }
}
 

##############################################

To refactor the provided WolfService class using SOLID principles and the Strategy Pattern, we'll create an ItemUpdateStrategy interface and implement different strategies for each item type. This approach adheres to the Single Responsibility Principle by separating the update logic into individual classes and the Open/Closed Principle by allowing new strategies to be added without modifying existing code.

Below is the refactored code:

ItemUpdateStrategy Interface:

<?php namespace WolfShop; interface ItemUpdateStrategy { public function update(Item $item): void; }

DefaultItemUpdateStrategy Class:

<?php namespace WolfShop; class DefaultItemUpdateStrategy implements ItemUpdateStrategy { public function update(Item $item): void { if ($item->quality > 0) { $item->quality--; } $item->sellIn--; if ($item->sellIn < 0 && $item->quality > 0) { $item->quality--; } } }

AppleAirPodsUpdateStrategy Class:

<?php namespace WolfShop; class AppleAirPodsUpdateStrategy implements ItemUpdateStrategy { public function update(Item $item): void { if ($item->quality < 50) { $item->quality++; } $item->sellIn--; if ($item->sellIn < 0 && $item->quality < 50) { $item->quality++; } } }

AppleIPadAirUpdateStrategy Class:

<?php namespace WolfShop; class AppleIPadAirUpdateStrategy implements ItemUpdateStrategy { public function update(Item $item): void { if ($item->quality < 50) { $item->quality++; if ($item->sellIn < 11 && $item->quality < 50) { $item->quality++; } if ($item->sellIn < 6 && $item->quality < 50) { $item->quality++; } } $item->sellIn--; if ($item->sellIn < 0) { $item->quality = 0; } } }

SamsungGalaxyS23UpdateStrategy Class:

<?php namespace WolfShop; class SamsungGalaxyS23UpdateStrategy implements ItemUpdateStrategy { public function update(Item $item): void { // Samsung Galaxy S23 does not change in quality or sellIn } }

ItemUpdateStrategyFactory Class:

<?php namespace WolfShop; class ItemUpdateStrategyFactory { public static function getStrategy(Item $item): ItemUpdateStrategy { return match ($item->name) { 'Apple AirPods' => new AppleAirPodsUpdateStrategy(), 'Apple iPad Air' => new AppleIPadAirUpdateStrategy(), 'Samsung Galaxy S23' => new SamsungGalaxyS23UpdateStrategy(), default => new DefaultItemUpdateStrategy(), }; } }

Refactored WolfService Class:

<?php declare(strict_types=1); namespace WolfShop; final class WolfService { /** * @param Item[] $items */ public function __construct( private array $items ) { } public function updateQuality(): void { foreach ($this->items as $item) { $strategy = ItemUpdateStrategyFactory::getStrategy($item); $strategy->update($item); } } }

This refactored code uses the Strategy Pattern to encapsulate the varying behavior of different items. Each strategy class implements the ItemUpdateStrategy interface, allowing WolfService to remain simple and focused on iterating over items and applying the appropriate strategy. This design adheres to the SOLID principles, making the code more maintainable and extensible.

Thank you

No comments:

Post a Comment

Golang Advanced Interview Q&A