Design Patterns

Design Patterns

Explain Factory Pattern The Factory pattern is used to create objects without specifying the exact class of the object that will be created. This is done by creating a separate factory class which selects the appropriate class and creates its objects based on some specified parameters.

interface Vehicle {
    public function drive();
}

class Car implements Vehicle {
    public function drive() {
        echo "Driving a car.\\n";
    }
}

class Bike implements Vehicle {
    public function drive() {
        echo "Riding a bike.\\n";
    }
}

class VehicleFactory {
    public static function createVehicle($type) {
        if ($type === 'car') {
            return new Car();
        } elseif ($type === 'bike') {
            return new Bike();
        } else {
            throw new Exception("Vehicle type not supported.");
        }
    }
}

// Usage
$car = VehicleFactory::createVehicle('car');
$car->drive();

$bike = VehicleFactory::createVehicle('bike');
$bike->drive();
        

Describe Strategy Pattern:

The Strategy pattern is used to create a family of algorithms, encapsulate each one, and make them interchangeable. Strategy allows the algorithm to vary independently from clients that use it.

interface PaymentStrategy {
    public function pay($amount);
}

class PayPalPayment implements PaymentStrategy {
    public function pay($amount) {
        echo "Paying $amount using PayPal.\\n";
    }
}

class CreditCardPayment implements PaymentStrategy {
    public function pay($amount) {
        echo "Paying $amount using Credit Card.\\n";
    }
}

class PaymentContext {
    private $paymentStrategy;

    public function __construct(PaymentStrategy $paymentStrategy) {
        $this->paymentStrategy = $paymentStrategy;
    }

    public function executePayment($amount) {
        $this->paymentStrategy->pay($amount);
    }
}

// Usage
$context = new PaymentContext(new PayPalPayment());
$context->executePayment(100);

$context = new PaymentContext(new CreditCardPayment());
$context->executePayment(200);
        

Explain Decorator Pattern:

The Decorator pattern is used to add new functionality to an existing object, without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.

interface Coffee {
    public function getCost();
    public function getDescription();
}

class SimpleCoffee implements Coffee {
    public function getCost() {
        return 10;
    }

    public function getDescription() {
        return "Simple coffee";
    }
}

class MilkCoffeeDecorator implements Coffee {
    protected $coffee;

    public function __construct(Coffee $coffee) {
        $this->coffee = $coffee;
    }

    public function getCost() {
        return $this->coffee->getCost() + 2;
    }

    public function getDescription() {
        return $this->coffee->getDescription() . ", milk";
    }
}

class VanillaCoffeeDecorator implements Coffee {
    protected $coffee;

    public function __construct(Coffee $coffee) {
        $this->coffee = $coffee;
    }

    public function getCost() {
        return $this->coffee->getCost() + 3;
    }

    public function getDescription() {
        return $this->coffee->getDescription() . ", vanilla";
    }
}

// Usage
$someCoffee = new SimpleCoffee();
echo $someCoffee->getDescription() . " costs $" . $someCoffee->getCost() . "\\n";

$someCoffee = new MilkCoffeeDecorator($someCoffee);
echo $someCoffee->getDescription() . " costs $" . $someCoffee->getCost() . "\\n";

$someCoffee = new VanillaCoffeeDecorator($someCoffee);
echo $someCoffee->getDescription() . " costs $" . $someCoffee->getCost() . "\\n";

        

Describe Singleton Pattern:

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it.

Describe Decorator pattern:

The Decorator pattern is used to add new functionality to an existing object, without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.

interface Coffee {
    public function getCost();
    public function getDescription();
}

class SimpleCoffee implements Coffee {
    public function getCost() {
        return 5;  // base price for simple coffee
    }

    public function getDescription() {
        return 'Simple coffee';
    }
}

class MilkDecorator implements Coffee {
    protected $coffee;

    public function __construct(Coffee $coffee) {
        $this->coffee = $coffee;
    }

    public function getCost() {
        return $this->coffee->getCost() + 2;  // adding cost for milk
    }

    public function getDescription() {
        return $this->coffee->getDescription() . ', with milk';
    }
}

$someCoffee = new SimpleCoffee();
echo $someCoffee->getDescription() . " costs $" . $someCoffee->getCost() . "\\n";

$someCoffee = new MilkCoffeeDecorator($someCoffee);
echo $someCoffee->getDescription() . " costs $" . $someCoffee->getCost() . "\\n";
        
Some Nath Kundu

Startup Enthusiastic || python, PHP, Node || Laravel, CodeIgniter, CakePHP, Symfony, Zend || AWS, GCP, Socket, unit-test || OOAD, Design Pattern, TDD, Agile

10 个月

CFBR

回复

要查看或添加评论,请登录

Som K的更多文章

  • Laravel Middleware

    Laravel Middleware

    Laravel Middleware is a feature that provides a convenient mechanism for filtering HTTP requests entering your…

    1 条评论
  • Laravel-Job-Queue

    Laravel-Job-Queue

    Laravel's Job Queue is a powerful feature that allows you to defer the processing of time-consuming tasks, such as…

    1 条评论
  • Laravel-Notifications

    Laravel-Notifications

    Step 1: Step 2: Step 3 Trigger Notifications: You can trigger these notifications when a payment is successful for an…

  • laravel-Passport-OAuth

    laravel-Passport-OAuth

    OAuth (Open Authorization) is an open standard for access delegation, commonly used to enable secure access to…

    1 条评论
  • Service Provider & Container

    Service Provider & Container

    Laravel's Service Provider vs. Service Container: Service Container: The service container is a fundamental part of…

  • Laravel-Event-System

    Laravel-Event-System

    Laravel's event system allows for easy observation and handling of events throughout the application. Events can be…

  • Laravel-Task-Scheduler

    Laravel-Task-Scheduler

    Laravel's Task Scheduling feature allows you to schedule Artisan commands within your Laravel application. This is…

    1 条评论
  • PHP-Versions & features

    PHP-Versions & features

    PHP 8.2 New Features : Constructor Property Promotion in Traits: Allows properties to be initialized in traits…

    1 条评论

社区洞察

其他会员也浏览了