{"id":8271,"date":"2025-07-18T04:59:25","date_gmt":"2025-07-18T04:59:25","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=8271"},"modified":"2025-07-18T05:33:40","modified_gmt":"2025-07-18T05:33:40","slug":"bridge-pattern-with-an-example-in-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/","title":{"rendered":"Bridge Pattern with an Example in Python"},"content":{"rendered":"<p>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.<\/p>\n<ul>\n<li><strong>Abstraction(Shape):<\/strong> Defines an abstraction of a shape and contains a reference to a Renderer object that represents the implementation of how the shape is drawn.<\/li>\n<li><strong>Implementor(Renderer):<\/strong> Defines the interface for the implementation classes, declares methods such as render_circle and render_square to perform operations on shapes.<\/li>\n<li><strong>Concrete Implementors(VectorRenderer, RasterRenderer):<\/strong> Concrete implementation classes that extend the Renderer interface and provide a real implementation for rendering shapes using vector and raster graphics.<\/li>\n<li><strong>Refined abstractions (circle, square):<\/strong> 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.<\/li>\n<li><strong>Client Code:<\/strong> 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.<\/li>\n<\/ul>\n<p>Here is an example illustrating the Bridge pattern in <a href=\"https:\/\/studysection.com\/blog\/adapter-pattern-in-python-with-an-example\/\">Python<\/a>:<\/p>\n<p><code><strong># Abstraction<\/strong><br \/>\n<strong>Class shape:<\/strong><br \/>\ndef __init__(self, renderer):<br \/>\nself.renderer = renderer<br \/>\ndef draw(self):<br \/>\nfold<\/code><\/p>\n<p><code><strong># Implementer<\/strong><br \/>\nrenderer class:<br \/>\ndef render_circle(custom, x, y, radius):<br \/>\nfold<\/code><\/p>\n<p><code>def render_square(self, x, y, side):<br \/>\nfold<\/code><\/p>\n<p><code><strong># Concrete Implementer 1<\/strong><br \/>\nclass VectorRenderer(Renderer):<br \/>\ndef render_circle(custom, x, y, radius):<br \/>\nprint(f\"Draw a circle at ({x}, {y}) with radius {radius} using vector graphics\")<\/code><\/p>\n<p><code>def render_square(self, x, y, side):<br \/>\nprint(f\"Draw a square at ({x}, {y}) with side {side} using vector graphics\")<\/code><\/p>\n<p><code><strong># Implementer of concrete 2<\/strong><br \/>\nclass RasterRenderer(Renderer):<br \/>\ndef render_circle(custom, x, y, radius):<br \/>\nprint(f\"Draw a circle at ({x}, {y}) with radius {radius} using bitmap\")<\/code><\/p>\n<p><code>def render_square(self, x, y, side):<br \/>\nprint(f\"Draw a square at ({x}, {y}) with side {side} using bitmap\")<\/code><\/p>\n<p><code><strong># Refined abstraction 1<\/strong><br \/>\nclass Circle (Shape):<br \/>\ndef __init__(custom, renderer, x, y, radius):<br \/>\nsuper().__init__(renderer)<br \/>\nself.x = x<br \/>\nself.y = y<br \/>\nself.radius = radius<\/code><\/p>\n<p><code>def draw(self):<br \/>\nself.renderer.render_circle(self.x, self.y, self.radius)<\/code><\/p>\n<p><code><strong># Refined abstraction 2<\/strong><br \/>\nclass Square (Shape):<br \/>\ndef __init__(custom, renderer, x, y, page):<br \/>\nsuper().__init__(renderer)<br \/>\nself.x = x<br \/>\nself.y = y<br \/>\nself.page = page<br \/>\ndef draw(self):<br \/>\nself.renderer.render_square(self.x, self.y, self.side)<\/code><\/p>\n<p><code>if __name__ == \"__main__\":<br \/>\nvector_renderer = VectorRenderer()<br \/>\nraster_renderer = RasterRenderer()<\/code><\/p>\n<p><code>circle = Circle(vector_renderer, 0, 0, 5)<br \/>\nsquare = square(raster_render, 1, 1, 5)<\/code><\/p>\n<p><code>circle.draw()<br \/>\nsquare.draw()<\/code><\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Bridge pattern is a structural design pattern that separates an abstraction from its implementation and allows it to change<\/p>\n","protected":false},"author":1,"featured_media":8273,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Bridge Pattern with an Example in Python<\/title>\n<meta name=\"description\" content=\"The Bridge pattern is a structural design pattern that separates an abstraction from its implementation and allows it to change independently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bridge Pattern with an Example in Python\" \/>\n<meta property=\"og:description\" content=\"The Bridge pattern is a structural design pattern that separates an abstraction from its implementation and allows it to change independently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-18T04:59:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-18T05:33:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/07\/Orange-and-Beige-Simple-Icon-Beauty-and-Fashion-Logo-3.png\" \/>\n\t<meta property=\"og:image:width\" content=\"940\" \/>\n\t<meta property=\"og:image:height\" content=\"788\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Bridge Pattern with an Example in Python\",\"datePublished\":\"2025-07-18T04:59:25+00:00\",\"dateModified\":\"2025-07-18T05:33:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/\"},\"wordCount\":291,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/\",\"name\":\"Bridge Pattern with an Example in Python\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2025-07-18T04:59:25+00:00\",\"dateModified\":\"2025-07-18T05:33:40+00:00\",\"description\":\"The Bridge pattern is a structural design pattern that separates an abstraction from its implementation and allows it to change independently.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bridge Pattern with an Example in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bridge Pattern with an Example in Python","description":"The Bridge pattern is a structural design pattern that separates an abstraction from its implementation and allows it to change independently.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Bridge Pattern with an Example in Python","og_description":"The Bridge pattern is a structural design pattern that separates an abstraction from its implementation and allows it to change independently.","og_url":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2025-07-18T04:59:25+00:00","article_modified_time":"2025-07-18T05:33:40+00:00","og_image":[{"width":940,"height":788,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/07\/Orange-and-Beige-Simple-Icon-Beauty-and-Fashion-Logo-3.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Bridge Pattern with an Example in Python","datePublished":"2025-07-18T04:59:25+00:00","dateModified":"2025-07-18T05:33:40+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/"},"wordCount":291,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/","url":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/","name":"Bridge Pattern with an Example in Python","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2025-07-18T04:59:25+00:00","dateModified":"2025-07-18T05:33:40+00:00","description":"The Bridge pattern is a structural design pattern that separates an abstraction from its implementation and allows it to change independently.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/bridge-pattern-with-an-example-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Bridge Pattern with an Example in Python"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":85,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8271"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=8271"}],"version-history":[{"count":4,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8271\/revisions"}],"predecessor-version":[{"id":8276,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8271\/revisions\/8276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/8273"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=8271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=8271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=8271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}