Creational Patterns Quiz
17 Questions
0 Views

Creational Patterns Quiz

Created by
@StylizedTheme

Questions and Answers

What does the factory method pattern use for object creation?

Inheritance

Which statement about the Anti-lock Braking System (ABS) is correct?

  • The ABS system does not require any specific types of sensors.
  • The sensor modules for cars are the same as for trucks.
  • There is only one type of sensor for all vehicles.
  • The ABS control module can use different sensor modules based on the vehicle type. (correct)
  • What is the purpose of the factory method in the ABS Controller class?

    To create an ABS sensor instance.

    The prototype design pattern ensures type safety by specifying which objects to create at compile-time.

    <p>False</p> Signup and view all the answers

    The __________ design pattern avoids the need for subclasses of an object creator in the client application.

    <p>prototype</p> Signup and view all the answers

    What is a negative aspect of the Singleton design pattern?

    <p>It introduces global state into an application.</p> Signup and view all the answers

    How is a singleton instance typically accessed?

    <p>Through a public static method.</p> Signup and view all the answers

    The constructor of a Singleton class is typically __________ to restrict instantiation.

    <p>private</p> Signup and view all the answers

    Which of the following is a type of creational pattern?

    <p>Abstract Factory</p> Signup and view all the answers

    What is the main purpose of the Abstract Factory pattern?

    <p>To provide an interface for creating families of related or dependent objects without specifying their concrete classes.</p> Signup and view all the answers

    The Builder pattern is used to create complex objects step by step?

    <p>True</p> Signup and view all the answers

    The Factory Method pattern allows a class to defer instantiation to its _____?

    <p>subclasses</p> Signup and view all the answers

    Which pattern allows for the creation of only one instance of a class?

    <p>Singleton</p> Signup and view all the answers

    What is the benefit of using the Factory Method pattern?

    <p>It allows for creating objects without specifying the exact class of the object that will be created.</p> Signup and view all the answers

    What is an example of a transformation applied by an image kernel?

    <p>Blurring, sharpening, outlining, or embossing.</p> Signup and view all the answers

    The Builder pattern can help encapsulate the construction process of an object?

    <p>True</p> Signup and view all the answers

    Match the following creational patterns with their definitions:

    <p>Abstract Factory = Creates families of related objects without specifying their concrete classes. Builder = Separates the construction of a complex object from its representation. Factory Method = Creates an object instance of a class by deferring instantiation to subclasses. Prototype = Creates new objects by copying an existing object. Singleton = Ensures a class has only one instance.</p> Signup and view all the answers

    Study Notes

    Creational Patterns

    • Creational design patterns deal with object creation mechanisms, aiming to create objects in a manner suitable for the situation.
    • Five main types of creational patterns include:
      • Abstract Factory: Provides an interface for creating families of related or dependent objects.
      • Builder: Separates the construction of a complex object from its representation.
      • Factory Method: Defines an interface for creating an object, allowing subclasses to decide the class to instantiate.
      • Prototype: Creates objects based on a prototypical instance, enhancing performance and memory efficiency.
      • Singleton: Ensures a class has only one instance, providing a global access point.

    Abstract Factory

    • Problem Identification:

      • Applications need to remain independent of object creation details.
      • Classes require independence in the creation of objects they use.
      • Families of related objects need to be created efficiently and uniformly.
    • Solution Strategy:

      • Encapsulate object creation within a factory object, using an interface for defined object creation.
      • Allow classes to delegate object creation to the factory, enhancing flexibility.
    • Advantages:

      • Promotes decoupling by avoiding direct instantiation within classes.
      • Facilitates the interchangeability of concrete implementations without altering dependent code.
      • Supports runtime changes in object creation.

    Builder

    • Problem Identification:

      • Need to separate complex object construction from its representation to simplify class management.
    • Solution Strategy:

      • Utilize a builder object to manage the creation of complex object parts, thus varying internal representations.
    • Advantages and Disadvantages:

      • Advantages:
        • Allows for flexible internal representation of objects.
        • Encapsulation of the construction and representation logic.
      • Disadvantages:
        • Requires distinct builder classes for each object type.
        • Builder classes must support mutability, which could complicate implementations.

    Factory Method

    • Problem Identification:

      • Need to create objects dynamically at runtime while keeping the object creation process abstract.
    • Solution Strategy:

      • Implement a factory method within a class that subclasses can override to customize object instantiation.
    • Benefits:

      • Reduces code duplication by centralizing the object creation process.
      • Allows complex object creation processes to be cleanly abstracted away from the composing object’s duties.

    Overall Importance of Creational Patterns

    • Creational patterns provide a more flexible and reusable way to manage object creation. This can lead to improved code organization, easier maintenance, and enhanced testing capabilities through techniques like dependency injection and mock object usage.### Factory Method Pattern
    • CarABSController and TruckABSController are subclasses of ABSController that implement the factory method makeABSSensor().
    • Each subclass creates a specific type of ABS sensor: CarABSSensor for cars and TruckABSSensor for trucks.
    • The subclass decision on sensor type occurs at runtime, allowing flexibility and avoiding hardcoded dependencies.
    • In the BenzABSController test code, instances of both CarABSController and TruckABSController are created and utilized.

    Prototype Pattern

    • The Prototype pattern addresses the need to instantiate classes at runtime rather than compile-time, allowing more dynamic object creation.
    • A Prototype object is defined to return a copy of itself, enabling new objects to be created by cloning.
    • This pattern avoids the need for subclassing to create object instances, unlike the factory method pattern.
    • Cloning is generally cheaper than instantiating new objects, improving efficiency.
    • Prototype objects can be configured and modified at runtime, providing increased flexibility.

    Prototype Pattern Use Case

    • Example scenario: Camera system implementation for the Mars Rover, where different types of images (black & white for obstacle detection, color for scientific purposes) are required.

    Prototype Example Code

    • ImageBuffer is an abstract prototype that utilizes cloning.
    • ImageBW and ImageRGB are concrete implementations of ImageBuffer, each overriding the clone() method.
    • BufferMaker class holds single instances of ImageBW and ImageRGB and provides a method to create new image buffers by cloning the prototypes.
    • In the MarsRover test code, multiple image buffers are created from the prototypes using the BufferMaker.

    Singleton Pattern

    • The Singleton pattern ensures that only a single instance of a class is created, providing a global point of access.
    • The constructor of the class is made private to restrict instantiation from outside the class.
    • A public static method (getInstance()) returns the sole instance, which is stored in a private static variable.
    • The pattern can introduce global state but is preferred over uncontrolled global variables.

    Singleton Example Code

    • Logger class employs the Singleton pattern to provide logging capabilities.
    • It has a private static final instance and a private constructor to prevent external instantiation.
    • The getInstance() method provides access to the logger instance.
    • The logging count can be incremented and retrieved through instance methods.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge on creational design patterns that focus on object creation mechanisms. This quiz covers five main types: Abstract Factory, Builder, Factory Method, Prototype, and Singleton, along with their uses and solutions. Assess your understanding of how these patterns enhance application flexibility and efficiency.

    More Quizzes Like This

    생성 패턴 퀴즈
    13 questions

    생성 패턴 퀴즈

    WorldFamousOnyx6547 avatar
    WorldFamousOnyx6547
    Factory Design Patterns Quiz
    5 questions
    Software Design Patterns Overview
    12 questions
    Use Quizgecko on...
    Browser
    Browser