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";
Startup Enthusiastic || python, PHP, Node || Laravel, CodeIgniter, CakePHP, Symfony, Zend || AWS, GCP, Socket, unit-test || OOAD, Design Pattern, TDD, Agile
10 个月CFBR