Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new instance of the Product class using the factory method.
PHP
<?php class Product { public function getName() { return "Product"; } } class Factory { public static function create() { return new [1](); } } $product = Factory::create(); echo $product->getName(); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the factory class name instead of the product class name.
Forgetting to use the 'new' keyword before the class name.
✗ Incorrect
The factory method returns a new instance of the Product class, so 'Product' is the correct class name to instantiate.
2fill in blank
mediumComplete the factory method to return a new instance of the correct class based on the type parameter.
PHP
<?php
interface Vehicle {
public function drive();
}
class Car implements Vehicle {
public function drive() {
return "Driving a car";
}
}
class Bike implements Vehicle {
public function drive() {
return "Riding a bike";
}
}
class VehicleFactory {
public static function createVehicle($type) {
if ($type === '[1]') {
return new Car();
} elseif ($type === 'bike') {
return new Bike();
}
return null;
}
}
$vehicle = VehicleFactory::createVehicle('car');
echo $vehicle->drive();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bike' instead of 'car' in the if condition.
Using the class name instead of the string type.
✗ Incorrect
The factory method checks if the type is 'car' to create a Car instance, so the string 'car' is the correct value.
3fill in blank
hardFix the error in the factory method to correctly instantiate the requested product.
PHP
<?php abstract class Button { abstract public function render(); } class WindowsButton extends Button { public function render() { return "Render Windows button"; } } class MacButton extends Button { public function render() { return "Render Mac button"; } } class ButtonFactory { public static function createButton($os) { switch ($os) { case 'windows': return new [1](); case 'mac': return new MacButton(); default: throw new Exception("Unsupported OS"); } } } $button = ButtonFactory::createButton('windows'); echo $button->render(); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Instantiating the abstract Button class.
Using the MacButton class for the windows case.
✗ Incorrect
To create a Windows button, the factory must instantiate the WindowsButton class.
4fill in blank
hardFill both blanks to complete the factory method that creates different notification objects.
PHP
<?php
interface Notification {
public function send();
}
class EmailNotification implements Notification {
public function send() {
return "Sending Email Notification";
}
}
class SMSNotification implements Notification {
public function send() {
return "Sending SMS Notification";
}
}
class NotificationFactory {
public static function createNotification($method) {
if ($method [1] 'email') {
return new EmailNotification();
} elseif ($method [2] 'sms') {
return new SMSNotification();
}
return null;
}
}
$notification = NotificationFactory::createNotification('email');
echo $notification->send();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same equality operator for both conditions.
Using inequality operators instead of equality.
✗ Incorrect
The factory method uses strict equality (===) to check for 'email' and loose equality (==) to check for 'sms'.
5fill in blank
hardFill all three blanks to complete the factory method that creates shape objects and calls their draw method.
PHP
<?php
interface Shape {
public function draw();
}
class Circle implements Shape {
public function draw() {
return "Drawing Circle";
}
}
class Square implements Shape {
public function draw() {
return "Drawing Square";
}
}
class ShapeFactory {
public static function createShape($type) {
return match($type) {
'circle' => new [1](),
'square' => new [2](),
default => throw new Exception("Unknown shape"),
};
}
}
$shape = ShapeFactory::createShape('[3]');
echo $shape->draw();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up class names and string types.
Using the wrong string for the shape type.
✗ Incorrect
The factory creates a Circle or Square object based on the type string 'circle' or 'square'.