quiz image

Design Patterns: Bridge Pattern

SelfSufficientRadon avatar
SelfSufficientRadon
·
·
Download

Start Quiz

Study Flashcards

20 Questions

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

Trying to extend the shape classes in two independent dimensions

What does the Bridge pattern attempt to solve?

The issue with class inheritance

What is the result of adding new shape types and colors to the hierarchy?

The hierarchy grows exponentially

What is the purpose of the reference field in the Shape class?

To point to one of the color objects

What is the benefit of using the Bridge pattern?

It allows adding new colors without changing the shape hierarchy

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

Object composition

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

Two new subclasses are introduced

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

The Shape class has a reference field pointing to one of the color objects

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

To separate abstraction and implementation into two separate hierarchies

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

Bridge

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

To delegate the work to the implementation layer

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

A giant spaghetti bowl with hundreds of conditionals

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

To enable the same GUI to work under different operating systems

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

To call the underlying operating system code (API) in response to user interactions

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

It requires creating a new subclass in the implementation hierarchy

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

A high-level control layer that delegates the work to the implementation layer

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

An exponential growth of the class hierarchy

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

It is interchangeable with different implementations as long as they follow a common interface

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

It decouples the GUI and API layers, allowing for independent changes

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

It divides the classes into two separate hierarchies

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.

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

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free
Use Quizgecko on...
Browser
Browser