We are building a vacation management system where different types of vacations (Paid Vacation, Sick Leave, and Work From Home) need to be created dynamically based on user input.
Below is an incomplete PHP code snippet. Your task is to complete the VacationFactory class so that it properly creates instances of different vacation types using the Factory Pattern.
Use any online compiler to write code;
- https://www.programiz.com/php/online-compiler/
- https://nextleap.app/online-compiler/php-programming
- https://www.codechef.com/php-online-compiler
<?php
// Step 1: Define an abstract Vacation class
abstract class Vacation {
protected $type;
protected $duration;
public function __construct($duration) {
$this->duration = $duration;
}
public function getType() {
return $this->type;
}
public function getDuration() {
return $this->duration;
}
abstract public function getDescription();
}
// Step 2: Implement concrete vacation classes
class PaidVacation extends Vacation {
public function __construct($duration) {
parent::__construct($duration);
$this->type = "Paid Vacation";
}
public function getDescription() {
return "You have booked {$this->duration} days of Paid Vacation.";
}
}
class SickLeaveVacation extends Vacation {
public function __construct($duration) {
parent::__construct($duration);
$this->type = "Sick Leave Vacation";
}
public function getDescription() {
return "You have taken {$this->duration} days of Sick Leave Vacation.";
}
}
class WorkFromHomeVacation extends Vacation {
public function __construct($duration) {
parent::__construct($duration);
$this->type = "Work from Home";
}
public function getDescription() {
return "You will be working from home for {$this->duration} days.";
}
}
// Step 3: Implement the VacationFactory class (Complete this!)
class VacationFactory {
public static function createVacation($type, $duration) {
// TODO: Implement the Factory Pattern to return the correct Vacation object
}
}
// Step 4: Client code (This should work once the factory is implemented correctly)
$paidVacation = VacationFactory::createVacation("Paid", 5);
$sickLeaveVacation = VacationFactory::createVacation("SickLeave", 3);
$workFromHomeVacation = VacationFactory::createVacation("WorkFromHome", 7);
echo $paidVacation->getDescription() . PHP_EOL;
echo $sickLeaveVacation->getDescription() . PHP_EOL;
echo $workFromHomeVacation->getDescription() . PHP_EOL;