The Factory pattern is a way to create objects without exposing the creation logic to the client. It uses a method to return instances, making object creation flexible and centralized.
Click to reveal answer
beginner
What is a companion object in Kotlin?
A companion object is a special object inside a class that allows you to define methods and properties tied to the class itself, similar to static members in other languages.
Click to reveal answer
intermediate
How does the Factory pattern use companion objects in Kotlin?
In Kotlin, the Factory pattern often uses companion objects to hold factory methods. This lets you create instances by calling methods on the class itself, keeping creation logic inside the class.
Click to reveal answer
intermediate
Example: What does this Kotlin code do?
<pre>class Car private constructor(val type: String) {
companion object {
fun createSedan() = Car("Sedan")
fun createSUV() = Car("SUV")
}
}</pre>
This code defines a Car class with a private constructor, so it can't be created directly. The companion object has factory methods createSedan() and createSUV() that create Car instances of specific types.
Click to reveal answer
intermediate
Why use a private constructor with a companion object factory in Kotlin?
Using a private constructor prevents direct creation of objects. The companion object factory methods control how objects are made, ensuring consistent and safe creation.
Click to reveal answer
What keyword makes a constructor private in Kotlin to support the Factory pattern?
Aprivate
Bprotected
Cinternal
Dpublic
✗ Incorrect
The 'private' keyword restricts constructor access, so objects can only be created through factory methods.
Where are factory methods typically placed in Kotlin when using the Factory pattern?
AIn interface declarations
BIn separate utility classes
CInside companion objects
DIn the main function
✗ Incorrect
Companion objects hold factory methods to create instances without exposing constructors.
What is the main benefit of using the Factory pattern with companion objects?
AMakes all properties mutable
BAutomatically generates getters and setters
CAllows multiple inheritance
DCentralizes object creation logic
✗ Incorrect
Factory pattern centralizes and controls how objects are created.
How do you call a factory method named createSedan in a companion object of class Car?
Anew Car.createSedan()
BCar.createSedan()
CcreateSedan.Car()
DCar().createSedan()
✗ Incorrect
Factory methods in companion objects are called on the class name directly.
What happens if the constructor is not private in a Factory pattern?
AObjects can be created directly, bypassing factory methods
BFactory methods won't work
CThe class becomes abstract
DThe compiler throws an error
✗ Incorrect
Without a private constructor, clients can create objects directly, which may break the pattern.
Explain how the Factory pattern works with companion objects in Kotlin.
Think about how you hide the constructor and provide special methods to create objects.
You got /4 concepts.
Describe the benefits of using a private constructor with companion object factory methods.
Why would you want to stop people from making objects any way they want?
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a companion object in Kotlin's factory pattern?
easy
A. To provide a place for factory methods that create instances
B. To allow multiple constructors in a class
C. To make the class inherit from another class
D. To store instance variables for each object
Solution
Step 1: Understand companion objects
Companion objects in Kotlin are used to hold methods and properties related to the class but not tied to an instance.
Step 2: Role in factory pattern
In the factory pattern, companion objects provide factory methods to create instances, especially when constructors are private.
Final Answer:
To provide a place for factory methods that create instances -> Option A
Quick Check:
Companion object = factory methods [OK]
Hint: Companion objects hold factory methods for instance creation [OK]
Common Mistakes:
Thinking companion objects store instance variables
Expecting compilation error due to private constructor
Thinking create() returns null
Confusing runtime exception with private constructor
4. Identify the error in this Kotlin factory pattern code:
class User private constructor(val id: Int) {
companion object {
fun create(id: Int): User {
User(id)
}
}
}
medium
A. Companion object cannot access private constructor
B. Constructor should not be private
C. Function create() should be outside companion object
D. Missing return statement in create() method
Solution
Step 1: Check create() method body
The create() method calls User(id) but does not return the created object.
Step 2: Understand Kotlin function return rules
Functions must explicitly return a value unless they are Unit (void). Here, create() should return the new User instance.
Final Answer:
Missing return statement in create() method -> Option D
Quick Check:
Factory method must return created object [OK]
Hint: Always return the new instance in factory methods [OK]
Common Mistakes:
Assuming constructor can't be private for companion access
Placing factory method outside companion object
Forgetting return keyword in factory method
5. Given a Kotlin class with a private constructor and a companion object factory, how can you modify the factory method to return a default instance when input is invalid (e.g., empty string)?
class Device private constructor(val model: String) {
companion object {
fun create(model: String): Device {
return Device(model)
}
}
}
hard
A. Throw an exception inside create() if model is empty
B. Make constructor public and handle validation outside
C. Add a condition in create() to return Device("Unknown") if model is empty
D. Remove companion object and use secondary constructor
Solution
Step 1: Understand the problem
The factory method should handle invalid input by returning a default instance instead of failing.
Step 2: Modify create() method
Add a check: if model is empty, return Device("Unknown"), else return Device(model).
Step 3: Why this works
This approach keeps constructor private, centralizes instance creation, and handles invalid input gracefully.
Final Answer:
Add a condition in create() to return Device("Unknown") if model is empty -> Option C
Quick Check:
Factory method handles invalid input with default instance [OK]
Hint: Use if-check in factory to return default instance on invalid input [OK]
Common Mistakes:
Making constructor public defeats factory pattern
Throwing exception instead of default instance
Removing companion object loses factory benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.