Guest User

Bridge Design Pattern

a guest
Dec 11th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. The Bridge Design Pattern is a structural design pattern that separates abstraction from implementation, allowing them to vary independently. It involves creating a bridge interface that acts as a bridge between the abstraction (client code) and the implementation (implementation-specific classes). This pattern is particularly useful when you want to avoid a permanent binding between an abstraction and its implementation.
  2.  
  3. Here's a simple example in Java to illustrate the Bridge Pattern:
  4.  
  5. java
  6. Copy code
  7. // Abstraction
  8. interface Shape {
  9. void draw();
  10. }
  11.  
  12. // Implementor
  13. interface DrawingAPI {
  14. void drawCircle(int x, int y, int radius);
  15. }
  16.  
  17. // Concrete Implementor 1
  18. class DrawingAPI1 implements DrawingAPI {
  19. @Override
  20. public void drawCircle(int x, int y, int radius) {
  21. System.out.printf("API1 - Drawing circle at (%d, %d) with radius %d%n", x, y, radius);
  22. }
  23. }
  24.  
  25. // Concrete Implementor 2
  26. class DrawingAPI2 implements DrawingAPI {
  27. @Override
  28. public void drawCircle(int x, int y, int radius) {
  29. System.out.printf("API2 - Drawing circle at (%d, %d) with radius %d%n", x, y, radius);
  30. }
  31. }
  32.  
  33. // Refined Abstraction
  34. class Circle implements Shape {
  35. private int x, y, radius;
  36. private DrawingAPI drawingAPI;
  37.  
  38. public Circle(int x, int y, int radius, DrawingAPI drawingAPI) {
  39. this.x = x;
  40. this.y = y;
  41. this.radius = radius;
  42. this.drawingAPI = drawingAPI;
  43. }
  44.  
  45. @Override
  46. public void draw() {
  47. drawingAPI.drawCircle(x, y, radius);
  48. }
  49. }
  50.  
  51. // Client code
  52. public class Client {
  53. public static void main(String[] args) {
  54. DrawingAPI api1 = new DrawingAPI1();
  55. DrawingAPI api2 = new DrawingAPI2();
  56.  
  57. Shape circle1 = new Circle(1, 2, 3, api1);
  58. Shape circle2 = new Circle(5, 7, 11, api2);
  59.  
  60. circle1.draw(); // Output: API1 - Drawing circle at (1, 2) with radius 3
  61. circle2.draw(); // Output: API2 - Drawing circle at (5, 7) with radius 11
  62. }
  63. }
  64. In this example:
  65.  
  66. Shape is the abstraction interface.
  67. DrawingAPI is the implementor interface.
  68. DrawingAPI1 and DrawingAPI2 are concrete implementors.
  69. Circle is the refined abstraction that uses the implementor interface.
  70. The client code (Client) creates circles with different drawing APIs, and the circles delegate the drawing operation to their respective drawing APIs.
  71. The Bridge Pattern allows the abstraction (Circle) and the implementation (DrawingAPI) to evolve independently. If you want to add more shapes or more drawing APIs, you can do so without modifying existing classes.
  72.  
  73.  
  74.  
Advertisement
Add Comment
Please, Sign In to add comment