Design Patterns Starter Quiz (Factory, Singleton, Observer) Quiz

Challenge your understanding of essential design patterns, including Factory, Singleton, and Observer. Assess your ability to identify use cases, advantages, and characteristics of these patterns in software development scenarios.

  1. Identifying the Factory Pattern

    Which pattern is best illustrated when a method is used to create objects without specifying the exact class of object that will be created, for example when instantiating different types of shapes based on user input?

    1. Abstractor Pattern
    2. Singleton Patten
    3. Observer Patten
    4. Factory Pattern

    Explanation: The Factory Pattern provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created. The scenario with shape objects fits this behavior. Singleton Pattern is about single instance creation, not multiple varying objects. Observer Pattern deals with event notification, not object creation. 'Abstractor Pattern' is not a recognized design pattern.

  2. Understanding Singleton Characteristics

    Which of the following statements about the Singleton Pattern is correct, especially regarding resource management such as a global logger?

    1. It restricts objects from being created outside the main thread.
    2. It is mainly used for creating families of related objects.
    3. It allows multiple instances of a class for different resources.
    4. It ensures only one instance of a class is created throughout the application.

    Explanation: The Singleton Pattern ensures that only one instance of a particular class exists, which is ideal for resources like configuration managers and loggers. Allowing multiple instances contradicts the purpose of Singleton. Restricting creation to the main thread is not a Singleton feature. Creating families of related objects is more relevant to Abstract Factory, not Singleton.

  3. Observer Pattern in Action

    In a stock market application, when all registered display elements automatically update in response to stock price changes, which design pattern does this scenario showcase?

    1. Strategy Pattern
    2. Observer Pattern
    3. Factory Patturn
    4. Singleton Patturn

    Explanation: This scenario exemplifies the Observer Pattern, where multiple observers (displays) update automatically when the subject (stock data) changes. Factory Pattern is about object creation, not notification. Singleton deals with single instances, not notification mechanics. Strategy Pattern involves interchangeable algorithms, not notification of changes.

  4. Singleton Implementation Challenge

    Which potential issue must be handled when implementing a Singleton Pattern in a multithreaded application, such as a single configuration manager?

    1. Multiple product type handling
    2. Runtime algorithm switching
    3. Thread safety during instance creation
    4. Loose coupling between interface and implementation

    Explanation: Thread safety during instance creation is crucial in Singleton implementations to prevent multiple instances in multithreaded environments. Handling multiple product types is related to Factory patterns. Algorithm switching describes the Strategy Pattern. Loose coupling is desirable generally, but not a unique challenge for Singleton regarding instantiation.

  5. Identifying the Wrong Pattern Usage

    If a software component must notify many other components when its state changes, which approach would NOT best suit this need?

    1. Observer Pattern
    2. Singleton Pattern
    3. Event Listener mechanism
    4. Publish-Subscribe model

    Explanation: The Singleton Pattern does not facilitate event notification; it merely ensures a single class instance. Observer, Publish-Subscribe, and Event Listener mechanisms are all suitable for one-to-many notification scenarios. Singleton lacks the structure for updating multiple dependent objects about changes.