Author - StudySection Post Views - 7 views

Bridge Pattern with an Example in Python

The Bridge pattern is a structural design pattern that separates an abstraction from its implementation and allows it to change independently. This pattern is especially useful when you need to switch implementations at runtime or have different implementations for different clients.

  • Abstraction(Shape): Defines an abstraction of a shape and contains a reference to a Renderer object that represents the implementation of how the shape is drawn.
  • Implementor(Renderer): Defines the interface for the implementation classes, declares methods such as render_circle and render_square to perform operations on shapes.
  • Concrete Implementors(VectorRenderer, RasterRenderer): Concrete implementation classes that extend the Renderer interface and provide a real implementation for rendering shapes using vector and raster graphics.
  • Refined abstractions (circle, square): Refined abstraction classes that extend the Shape interface. They use the Renderer object to delegate the rendering operation to the appropriate method in the Renderer object.
  • Client Code: Creates VectorRenderer and RasterRenderer instances to specify the rendering method for each shape. It then instantiates a Circle and a Square and passes the respective renderer to each shape. When the draw method is called on each shape, it defers the rendering operation to the specified renderer.The benefits of using the Bridge pattern include separating the abstraction from its implementation, allowing them to change independently, and making the code more flexible and easier to maintain.

Here is an example illustrating the Bridge pattern in Python:

# Abstraction
Class shape:
def __init__(self, renderer):
self.renderer = renderer
def draw(self):
fold

# Implementer
renderer class:
def render_circle(custom, x, y, radius):
fold

def render_square(self, x, y, side):
fold

# Concrete Implementer 1
class VectorRenderer(Renderer):
def render_circle(custom, x, y, radius):
print(f"Draw a circle at ({x}, {y}) with radius {radius} using vector graphics")

def render_square(self, x, y, side):
print(f"Draw a square at ({x}, {y}) with side {side} using vector graphics")

# Implementer of concrete 2
class RasterRenderer(Renderer):
def render_circle(custom, x, y, radius):
print(f"Draw a circle at ({x}, {y}) with radius {radius} using bitmap")

def render_square(self, x, y, side):
print(f"Draw a square at ({x}, {y}) with side {side} using bitmap")

# Refined abstraction 1
class Circle (Shape):
def __init__(custom, renderer, x, y, radius):
super().__init__(renderer)
self.x = x
self.y = y
self.radius = radius

def draw(self):
self.renderer.render_circle(self.x, self.y, self.radius)

# Refined abstraction 2
class Square (Shape):
def __init__(custom, renderer, x, y, page):
super().__init__(renderer)
self.x = x
self.y = y
self.page = page
def draw(self):
self.renderer.render_square(self.x, self.y, self.side)

if __name__ == "__main__":
vector_renderer = VectorRenderer()
raster_renderer = RasterRenderer()

circle = Circle(vector_renderer, 0, 0, 5)
square = square(raster_render, 1, 1, 5)

circle.draw()
square.draw()

In this example, the Shape class serves as the abstraction, while the Renderer class serves as the implementor. Specific implementations of shapes (Circle and Square) are created using specific renderers (vector or raster). Using the Bridge pattern, we can easily add new shapes or rendering methods without modifying the existing code.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri