Free Basic Design Pattern Question and Answers — Questions and Answers
Question 1: Give a subsystem's collection of interfaces a unified interface. It defines a higher level interface that facilitates use of the subsystem.
- Mediator
- Adapter
- Strategy
- Facade (Correct answer)
Correct answer: Facade
The Facade pattern provides a unified, higher-level interface to a set of interfaces in a subsystem. It defines a simpler interface that makes the subsystem easier to use, reducing complexity and promoting loose coupling between the client and the subsystem. This hides the intricate details of the subsystem's components.
Question 2: Define an operation that contains the basic structure of an algorithm while leaving some phases to subclasses. It enables subclasses to modify specific algorithmic stages without altering the algorithm's structure.
- Template method (Correct answer)
- Interpretor
- Prototype
- Chain of responsibility
Correct answer: Template method
The Template Method pattern defines the basic structure of an algorithm in an operation, deferring some specific steps to subclasses. This allows subclasses to modify or implement certain stages of the algorithm without altering its fundamental structure. It ensures that the overall flow of the algorithm remains consistent while providing customization points.
Question 3: Enables sequential access to the components of an aggregate object without disclosing its underlying representation.
- Composite
- Command
- Iterator (Correct answer)
- Visitor
Correct answer: Iterator
The Iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. It decouples the traversal logic from the aggregate object itself, allowing different traversal algorithms to be used and simplifying the aggregate's interface. This ensures controlled and consistent access to collections.
Question 4: Which statement regarding the Singleton design pattern is true.
- A singleton class provides a method of accessing its single object directly without the requirement to initialize the class's object.
- Creational patterns include this kind of design pattern.
- In this pattern, a single class is in charge of creating an object while ensuring that only one object is produced.
- All of the above (Correct answer)
Correct answer: All of the above
All the statements accurately describe the Singleton design pattern. It is a creational pattern that ensures a class has only one instance and provides a global point of access to it, typically through a static method, without requiring explicit object initialization by the client.
Question 5: Dynamically assign extra responsibilities to an object. It offers a versatile substitute for subclassing when it comes to extending functionality.
- Decorator (Correct answer)
- Chain of responsibility
- Composite
- Adapter
Correct answer: Decorator
The Decorator pattern dynamically attaches additional responsibilities to an object. It provides a flexible alternative to subclassing for extending functionality, allowing new behaviors to be added or removed at runtime. This 'wraps' the original object with new capabilities without altering its core class.
Question 6: Encapsulate a request as an object to provide undoable operations, parametrize clients with various requests, and queue or log requests.
- Decorator
- Adapter
- Composite
- Command (Correct answer)
Correct answer: Command
The Command pattern encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. It decouples the sender of a request from its receiver, allowing for greater flexibility in handling and managing operations.
Question 7: Establishes an interface for object creation but leaves it up to the subclasses to choose the class to instantiate. It let subclasses' instantiations to vary.
- Abstract Factory
- Prototype
- Factory Method (Correct answer)
- Builder
Correct answer: Factory Method
The Factory Method pattern defines an interface for creating an object but lets subclasses decide which class to instantiate. It defers instantiation to subclasses, promoting loose coupling between the client code and the concrete classes being created. This allows the system to be extended with new product types without modifying existing client code.
Give a subsystem's collection of interfaces a unified interface.
It defines a higher level interface that facilitates use of the subsystem.