Chapter 18 of TypeScript delves into design patterns such as Dependency Injection, Singleton, Factory, and Builder patterns. Understanding these patterns enhances your ability to write scalable, maintainable, and testable code in TypeScript.
Multiple Choice Questions (MCQs)
Section 1: Dependency Injection
What is the primary purpose of Dependency Injection (DI)? a) To reduce the complexity of code b) To control the flow of data c) To provide components with their dependencies externally d) To enhance the performance of applications
Which of the following is NOT a benefit of using Dependency Injection? a) Improved testability b) Reduced code duplication c) Tight coupling of components d) Easier maintenance
What is the core idea behind Dependency Injection in TypeScript? a) Injecting data into a class directly b) Passing dependencies into classes at runtime c) Creating classes manually without relying on external dependencies d) Injecting classes as a function return value
In TypeScript, what is an example of a constructor-based Dependency Injection? a) class MyClass { constructor(private dependency: Service) {} b) class MyClass { dependency = new Service()} c) class MyClass { constructor() {} d) class MyClass { dependency: Service; }
Which of the following is a typical characteristic of Dependency Injection in TypeScript? a) Dependencies are passed into a class constructor or methods b) Dependencies are hard-coded within the class c) Dependencies are fetched using require() d) Dependencies are only used in static methods
What does Dependency Injection help to achieve in TypeScript applications? a) Easier debugging b) Increased memory usage c) Loose coupling between components d) Decreased code readability
What is an example of a TypeScript Dependency Injection container? a) InversifyJS b) React c) Angular d) Lodash
How can Dependency Injection improve testability in TypeScript? a) By allowing mocking and stubbing of dependencies in unit tests b) By making dependencies public c) By using a global state for the entire application d) By reducing the number of required tests
Which of the following is a common disadvantage of Dependency Injection? a) It can result in more complex setup and configuration b) It reduces the flexibility of the application c) It makes the codebase smaller d) It makes applications faster
What kind of testing is Dependency Injection commonly used for? a) Performance testing b) Unit testing c) Integration testing d) UI testing
Section 2: Singleton and Factory Patterns with TypeScript
What is the main purpose of the Singleton design pattern? a) To create multiple instances of a class b) To allow only one instance of a class throughout the application c) To create a class with dynamic properties d) To manage component lifecycles
Which of the following best describes the Singleton pattern? a) It is a pattern where a class can have multiple instances b) It restricts a class to a single instance c) It is used to extend classes d) It is used for creating interfaces
How does the Singleton pattern ensure only one instance of a class? a) By using a static method to return the same instance b) By creating a new instance every time c) By limiting the class constructor to one parameter d) By implementing an event listener pattern
In TypeScript, which method ensures only one instance of a class in Singleton pattern? a) A private constructor and a static method returning the single instance b) A public constructor and a private static instance variable c) A factory function that creates instances d) A class that doesn’t allow inheritance
Which of the following best defines the Factory design pattern? a) It defines how to create an object but lets subclasses alter the type of objects that will be created b) It is used to create one instance of a class c) It is used to define an interface for creating families of related or dependent objects d) It is a pattern where an object’s behavior can be defined dynamically
How does the Factory pattern differ from the Singleton pattern? a) Factory creates multiple instances; Singleton ensures one instance b) Singleton creates multiple instances; Factory ensures one instance c) Factory pattern is only for static classes d) Singleton can create multiple objects with shared data
How do you implement the Singleton pattern in TypeScript? a) By creating a class with a static method returning the instance b) By using a constructor with a return type of void c) By using a factory function to create instances d) By creating a static instance variable and instantiating it in a constructor
In which situation is the Singleton pattern most useful? a) When you need to control access to a shared resource b) When you need to create several instances of a class c) When you want to decouple code d) When creating independent objects
Which is the main benefit of the Factory pattern? a) It isolates the client from the creation process of objects b) It guarantees only one instance of a class c) It uses fewer resources d) It makes all objects mutable
How does TypeScript support the Factory pattern? a) By using constructors with optional arguments b) By allowing factory functions to return instances of different types c) By using class inheritance d) By allowing abstract classes
Section 3: Builder Pattern in TypeScript
What is the purpose of the Builder design pattern? a) To create an object by constructing it step by step b) To allow objects to be shared among multiple classes c) To create different objects based on a shared interface d) To store objects in a collection
Which scenario is ideal for using the Builder pattern? a) When you want to create objects with a complex structure and multiple configurations b) When you want to restrict object creation to one instance c) When you need a shared pool of objects d) When you need to define an interface for creating objects
What does the Director do in the Builder pattern? a) It manages the construction process by calling methods on the builder b) It creates the objects based on provided configurations c) It defines the interfaces for the Builder d) It keeps track of created objects
In TypeScript, how is the Builder pattern typically implemented? a) By using classes and methods to construct parts of an object b) By using a static factory method to create objects c) By using singletons to control object creation d) By extending a base class and overriding methods
Which of the following is a key benefit of using the Builder pattern in TypeScript? a) It allows complex object creation to be broken down into simpler, reusable methods b) It reduces the number of classes needed c) It guarantees only one instance of an object d) It creates lightweight objects
How can you customize the product created by the Builder pattern in TypeScript? a) By passing configuration options to the builder methods b) By using a single method to create all objects c) By subclassing the builder class d) By changing the object’s type dynamically
How does the Builder pattern help in managing complex object creation? a) By breaking down the construction process into smaller, more manageable steps b) By avoiding the use of constructors entirely c) By creating singletons for complex objects d) By allowing direct object manipulation without methods
In TypeScript, which of the following classes can be used to build a product in the Builder pattern? a) The Builder class itself b) The Product class c) The Director class d) The Client class
Which of the following is a drawback of the Builder pattern? a) It can result in code duplication if not used properly b) It requires significant overhead to implement c) It does not allow flexible object configurations d) It is difficult to use in a multi-threaded environment
How does TypeScript’s type system support the Builder pattern? a) By enforcing strict interfaces for builders and products b) By providing a way to define object properties dynamically c) By allowing inheritance in builder classes d) By using generics to define product types
Answer Key
Qno
Answer
1
c) To provide components with their dependencies externally
2
c) Tight coupling of components
3
b) Passing dependencies into classes at runtime
4
a) class MyClass { constructor(private dependency: Service) {}
5
a) Dependencies are passed into a class constructor or methods
6
c) Loose coupling between components
7
a) InversifyJS
8
a) By allowing mocking and stubbing of dependencies in unit tests
9
a) It can result in more complex setup and configuration
10
b) Unit testing
11
b) To allow only one instance of a class throughout the application
12
b) It restricts a class to a single instance
13
a) By using a static method to return the same instance
14
a) A private constructor and a static method returning the single instance
15
a) It defines how to create an object but lets subclasses alter the type of objects that will be created
16
a) Factory creates multiple instances; Singleton ensures one instance
17
a) By creating a class with a static method returning the instance
18
a) When you need to control access to a shared resource
19
a) It isolates the client from the creation process of objects
20
b) By allowing factory functions to return instances of different types
21
a) To create an object by constructing it step by step
22
a) When you want to create objects with a complex structure and multiple configurations
23
a) It manages the construction process by calling methods on the builder
24
a) By using classes and methods to construct parts of an object
25
a) It allows complex object creation to be broken down into smaller, reusable methods
26
a) By passing configuration options to the builder methods
27
a) By breaking down the construction process into smaller, more manageable steps
28
b) The Product class
29
b) It requires significant overhead to implement
30
a) By enforcing strict interfaces for builders and products