The Bridge pattern separates an abstraction from its implementation so that both can vary independently. It helps to avoid a permanent binding between an abstraction and its implementation.
Click to reveal answer
beginner
In the Bridge pattern, what are the two main components?
The two main components are: 1. Abstraction - defines the high-level control layer. 2. Implementor - defines the interface for implementation classes.
Click to reveal answer
intermediate
How does the Bridge pattern improve system scalability?
By decoupling abstraction and implementation, the Bridge pattern allows you to change or extend either side independently without affecting the other, making the system easier to scale and maintain.
Click to reveal answer
beginner
Give a real-life analogy for the Bridge pattern.
Think of a TV remote (abstraction) and the TV device (implementation). The remote controls the TV but can work with different TV brands. The remote and TV are separate, so you can change one without changing the other.
Click to reveal answer
intermediate
What problem does the Bridge pattern solve compared to inheritance?
Inheritance binds abstraction and implementation tightly, making changes difficult. The Bridge pattern uses composition to separate them, allowing independent changes and reducing code duplication.
Click to reveal answer
What does the Bridge pattern primarily separate?
AData and logic
BAbstraction and implementation
CClient and server
DUI and backend
✗ Incorrect
The Bridge pattern separates abstraction from implementation to allow them to vary independently.
Which design principle does the Bridge pattern follow?
AFavor composition over inheritance
BUse inheritance over composition
CAvoid encapsulation
DTight coupling
✗ Incorrect
The Bridge pattern favors composition to separate abstraction and implementation.
In the Bridge pattern, the 'Implementor' is responsible for:
AHandling database connections
BProviding the user interface
CManaging client requests
DDefining the interface for implementation classes
✗ Incorrect
The Implementor defines the interface for concrete implementation classes.
Which scenario best fits the use of the Bridge pattern?
AWhen you want to vary both abstraction and implementation independently
BWhen you have a simple one-level class hierarchy
CWhen you want to tightly couple classes
DWhen you want to avoid interfaces
✗ Incorrect
The Bridge pattern is used when both abstraction and implementation need to vary independently.
What is a key benefit of using the Bridge pattern?
ASimpler inheritance hierarchy
BIncreased code duplication
CImproved flexibility and extensibility
DTighter coupling between classes
✗ Incorrect
The Bridge pattern improves flexibility and extensibility by decoupling abstraction and implementation.
Explain the Bridge pattern and how it helps in designing flexible systems.
Think about how you can change parts of a system without affecting others.
You got /4 concepts.
Describe a real-life example that illustrates the Bridge pattern concept.
Consider everyday devices that work together but can be changed separately.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Bridge pattern in system design?
easy
A. To create a single class that handles all functionalities
B. To separate an abstraction from its implementation so they can vary independently
C. To enforce a strict inheritance hierarchy without flexibility
D. To combine multiple unrelated classes into one
Solution
Step 1: Understand the Bridge pattern concept
The Bridge pattern is designed to separate an abstraction from its implementation, allowing both to change independently.
Step 2: Compare options with the concept
To separate an abstraction from its implementation so they can vary independently correctly states this separation. Other options describe different or incorrect design approaches.
Final Answer:
To separate an abstraction from its implementation so they can vary independently -> Option B
Quick Check:
Bridge pattern = separation of abstraction and implementation [OK]
Hint: Bridge = separate interface and implementation [OK]
Common Mistakes:
Confusing Bridge with Adapter or Decorator patterns
Thinking Bridge merges classes instead of separating
Assuming Bridge enforces rigid inheritance
2. Which of the following is the correct way to define the Bridge pattern structure in code?
easy
A. class Implementor extends Abstraction {}
B. class Abstraction extends Implementor {}
C. class Abstraction { Implementor impl; }
D. class Abstraction implements Implementor {}
Solution
Step 1: Recall Bridge pattern structure
Bridge pattern uses composition: the abstraction holds a reference to the implementor interface.
Step 2: Analyze each option
class Abstraction { Implementor impl; } correctly shows the required composition. The other options use inheritance (extends) or interface implementation (implements), which defeats the purpose of decoupling via composition.
Final Answer:
class Abstraction { Implementor impl; } -> Option C
Quick Check:
Bridge uses composition, not inheritance [OK]
Hint: Bridge uses composition, not inheritance [OK]
Common Mistakes:
Using inheritance instead of composition
Confusing implements keyword with composition
Ignoring the implementor interface role
3. Given the following code snippet using the Bridge pattern, what will be the output?
class DrawingAPI {
drawCircle(x, y, radius) { console.log(`API drawing circle at (${x},${y}) with radius ${radius}`); }
}
class Shape {
constructor(drawingAPI) { this.drawingAPI = drawingAPI; }
draw() {}
}
class Circle extends Shape {
constructor(x, y, radius, drawingAPI) {
super(drawingAPI);
this.x = x; this.y = y; this.radius = radius;
}
draw() {
this.drawingAPI.drawCircle(this.x, this.y, this.radius);
}
}
const api = new DrawingAPI();
const circle = new Circle(5, 10, 7, api);
circle.draw();
medium
A. API drawing circle at (5,10) with radius 7
B. Circle drawn at (5,10) with radius 7
C. Error: drawCircle is not a function
D. No output
Solution
Step 1: Trace the draw() method call
The Circle's draw method calls drawingAPI.drawCircle with x=5, y=10, radius=7.
Ignoring tight coupling caused by direct instantiation
Thinking concrete class Implementor is always wrong
Expecting operation to return value unnecessarily
5. You are designing a graphics system that supports multiple shapes (Circle, Square) and multiple rendering APIs (OpenGL, DirectX). How does the Bridge pattern help in this design?
hard
A. By using inheritance to create a subclass for each shape-API combination
B. By forcing all shapes to use a single rendering API implementation
C. By merging shape and rendering API classes into one large class
D. By allowing shapes and rendering APIs to vary independently without subclass explosion
Solution
Step 1: Understand the problem of subclass explosion
If each shape-API pair requires a subclass, the number grows quickly (e.g., CircleOpenGL, CircleDirectX).
Step 2: Apply Bridge pattern advantage
Bridge separates shape abstraction from rendering implementation, so shapes and APIs can be combined dynamically without many subclasses.
Final Answer:
By allowing shapes and rendering APIs to vary independently without subclass explosion -> Option D
Quick Check:
Bridge reduces subclass explosion by separating abstraction and implementation [OK]
Hint: Bridge avoids subclass explosion by separating concerns [OK]
Common Mistakes:
Thinking Bridge merges classes instead of separating
Assuming inheritance must handle all combinations
Ignoring dynamic combination of abstraction and implementation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.