0
0
PHPprogramming~5 mins

Factory pattern in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the Factory pattern in PHP?
The Factory pattern is a design pattern that creates objects without exposing the creation logic to the client. It provides a method to create objects based on input or conditions, helping to keep code flexible and easy to maintain.
Click to reveal answer
beginner
Why use the Factory pattern instead of creating objects directly?
Using the Factory pattern helps separate object creation from usage. This makes code easier to change, test, and extend because the client code does not depend on specific classes but on interfaces or abstract classes.
Click to reveal answer
intermediate
In the Factory pattern, what role does the factory class play?
The factory class contains a method that decides which class to instantiate and returns the created object. It hides the details of object creation from the client.
Click to reveal answer
beginner
What is a simple example of a Factory pattern in PHP?
A simple example is a ShapeFactory class with a method createShape($type) that returns a Circle, Square, or Triangle object based on the $type parameter. The client calls createShape without knowing the details of each shape class.
Click to reveal answer
intermediate
How does the Factory pattern improve code maintenance?
It centralizes object creation in one place. When you add new types or change creation logic, you only update the factory, not all client code. This reduces errors and makes the system easier to extend.
Click to reveal answer
What does the Factory pattern mainly help with?
AHandling user input validation
BImproving database queries
CStyling HTML elements
DCreating objects without exposing creation logic
In PHP, where is the object creation logic placed in the Factory pattern?
AIn the database
BInside the factory class
CIn the client code
DIn the HTML template
Which of these is a benefit of using the Factory pattern?
AEasier to change object creation without affecting client code
BFaster execution of SQL queries
CBetter CSS styling
DSimplifies user authentication
What would a factory method typically return?
AA CSS stylesheet
BA string of HTML
CAn object instance
DA database connection
Which design principle does the Factory pattern support?
AEncapsulation of object creation
BDirect manipulation of global variables
CHardcoding class names in client code
DMixing UI and business logic
Explain the Factory pattern and why it is useful in PHP programming.
Think about how you can create objects without the client knowing the details.
You got /3 concepts.
    Describe a simple example of implementing the Factory pattern in PHP.
    Imagine a shape factory that creates different shapes based on input.
    You got /3 concepts.