Design Patterns: Bridge Pattern
20 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the main issue with the Shape class hierarchy when trying to extend it to incorporate colors?

  • Lack of abstraction in the Shape class
  • Having too many subclasses
  • Not using object composition
  • Trying to extend the shape classes in two independent dimensions (correct)
  • What does the Bridge pattern attempt to solve?

  • The lack of object composition
  • The problem of too many subclasses
  • The need for more abstraction
  • The issue with class inheritance (correct)
  • What is the result of adding new shape types and colors to the hierarchy?

  • The hierarchy grows exponentially (correct)
  • The hierarchy remains the same size
  • The hierarchy grows linearly
  • The hierarchy shrinks
  • What is the purpose of the reference field in the Shape class?

    <p>To point to one of the color objects</p> Signup and view all the answers

    What is the benefit of using the Bridge pattern?

    <p>It allows adding new colors without changing the shape hierarchy</p> Signup and view all the answers

    What is the term used to describe the extraction of one of the dimensions into a separate class hierarchy?

    <p>Object composition</p> Signup and view all the answers

    What is the result of introducing a new shape type, such as a triangle, to the hierarchy?

    <p>Two new subclasses are introduced</p> Signup and view all the answers

    What is the relationship between the Shape class and the Color class?

    <p>The Shape class has a reference field pointing to one of the color objects</p> Signup and view all the answers

    What is the purpose of the Bridge pattern in terms of abstraction and implementation?

    <p>To separate abstraction and implementation into two separate hierarchies</p> Signup and view all the answers

    What is the term used to describe the link between the Shape class and the Color class?

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

    What is the primary purpose of the abstraction layer in the Bridge pattern?

    <p>To delegate the work to the implementation layer</p> Signup and view all the answers

    What is the result of not using the Bridge pattern in a scenario with multiple GUIs and APIs?

    <p>A giant spaghetti bowl with hundreds of conditionals</p> Signup and view all the answers

    What is the benefit of dividing the classes into two hierarchies in the Bridge pattern?

    <p>To enable the same GUI to work under different operating systems</p> Signup and view all the answers

    What is the role of the graphical user interface (GUI) in the Bridge pattern?

    <p>To call the underlying operating system code (API) in response to user interactions</p> Signup and view all the answers

    What is the advantage of using the Bridge pattern when adding support for a new operating system?

    <p>It requires creating a new subclass in the implementation hierarchy</p> Signup and view all the answers

    What is the meaning of the term 'abstraction' in the context of the Bridge pattern?

    <p>A high-level control layer that delegates the work to the implementation layer</p> Signup and view all the answers

    What is the result of extracting the code related to specific interface-platform combinations into separate classes?

    <p>An exponential growth of the class hierarchy</p> Signup and view all the answers

    What is the key characteristic of the implementation layer in the Bridge pattern?

    <p>It is interchangeable with different implementations as long as they follow a common interface</p> Signup and view all the answers

    What is the benefit of using the Bridge pattern in terms of GUI and API independence?

    <p>It decouples the GUI and API layers, allowing for independent changes</p> Signup and view all the answers

    What is the primary advantage of the Bridge pattern in terms of code structure?

    <p>It divides the classes into two separate hierarchies</p> Signup and view all the answers

    Study Notes

    Bridge Pattern

    • The Bridge pattern allows you to split a large class or a set of closely related classes into two separate hierarchies: abstraction and implementation.
    • This enables the two hierarchies to be developed independently of each other.

    Problem with Class Inheritance

    • Adding new shape types and colors to a hierarchy can lead to exponential growth, making it difficult to manage.
    • For example, adding a triangle shape requires introducing two subclasses for each color, and adding a new color requires creating three subclasses for each shape type.

    Solution

    • The Bridge pattern solves this problem by switching from inheritance to object composition.
    • It extracts one of the dimensions (e.g., color) into a separate class hierarchy, allowing the original classes to reference an object of the new hierarchy.
    • This enables the original classes to delegate work to the linked object, rather than having all the state and behaviors within one class.

    Abstraction and Implementation

    • Abstraction (interface) is a high-level control layer that delegates work to the implementation layer (platform).
    • Abstraction is not supposed to do any real work on its own.
    • The implementation layer performs the actual work.

    Real-World Example

    • In a GUI application, the abstraction can be represented by the graphical user interface (GUI), and the implementation can be the underlying operating system code (API).
    • The GUI layer calls the API in response to user interactions.

    Benefits of the Bridge Pattern

    • Enables extending the application in two independent directions.
    • Allows for changing the GUI classes without touching the API-related classes.
    • Enables adding support for another operating system by creating a subclass in the implementation hierarchy.

    Studying That Suits You

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

    Quiz Team

    Description

    Learn about the Bridge pattern, a design pattern that separates abstraction and implementation, allowing for independent development of the two hierarchies.

    More Like This

    Bridge Pattern UML Diagram Concepts
    12 questions
    Adapter and Bridge Patterns Overview
    40 questions
    Design Patterns: Adapter e Bridge
    17 questions
    Structural Design Patterns Quiz
    21 questions

    Structural Design Patterns Quiz

    SweepingArcticTundra565 avatar
    SweepingArcticTundra565
    Use Quizgecko on...
    Browser
    Browser