{"id":2042,"date":"2019-06-01T13:05:16","date_gmt":"2019-06-01T10:05:16","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2042"},"modified":"2024-12-16T22:32:37","modified_gmt":"2024-12-16T19:32:37","slug":"bridge-pattern","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/2019\/06\/01\/bridge-pattern\/","title":{"rendered":"Pattern Bridge"},"content":{"rendered":"<p>The Bridge pattern is a structural design pattern. It allows you to abstract the implementation of class logic by moving the logic into a separate abstract class. Sounds simple, right?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2053\" src=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2019\/06\/bridge.jpg\" alt=\"\" width=\"380\" height=\"323\" \/><\/p>\n<p>Let&#8217;s say we are implementing a spam bot that should be able to send messages to different types of messengers.<br \/>We implement it using a common protocol:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>protocol User {\n    let token: String\n    let username: String\n}\n\nprotocol Messenger {\n    var authorize(login: String, password: String)\n    var send(message: String, to user: User)\n}\n\nclass iSeekUUser: User {\n    let token: String\n    let username: String\n}\n\nclass iSeekU: Messenger {\n\n    var authorizedUser: User?\n    var requestSender: RequestSender?\n    var requestFactory: RequestFactory?\n\n    func authorize(login: String, password: String) {\n        authorizedUser = requestSender?.perform(requestFactory.loginRequest(login: login, password: password))\n    }\n    \n    func send(message: String, to user: User) {\n        requestSender?.perform(requestFactory.messageRequest(message: message, to: user)\n    }\n}\n\nclass SpamBot {\n    func start(usersList: [User]) {\n        let iSeekUMessenger = iSeekU()\n        iSeekUMessenger.authorize(login: \"SpamBot\", password: \"SpamPassword\")\n        \n        for user in usersList {\n            iSeekUMessennger.send(message: \"Hey checkout demensdeum blog! http:\/\/demensdeum.com\", to: user)\n        }\n    }\n}\n<\/code><\/pre>\n<\/div>\n<p>Now let&#8217;s imagine a situation where a new, faster messaging protocol for the iSekU messenger is released. To add a new protocol, you will need to duplicate the iSekU bot implementation, changing only a small part of it. It is unclear why this should be done if only a small part of the class logic has changed. This approach violates the DRY principle; with further development of the product, the lack of flexibility will make itself known through errors and delays in the implementation of new features.<br \/>Let&#8217;s move the protocol logic into an abstract class, thus implementing the Bridge pattern:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>protocol User {\n    let token: String\n    let username: String\n}\n\nprotocol Messenger {\n    var authorize(login: String, password: String)\n    var send(message: String, to user: User)\n}\n\nprotocol MessagesSender {\n    func send(message: String, to user: User)\n}\n\nclass iSeekUUser: User {\n    let token: String\n    let username: String\n}\n\nclass iSeekUFastMessengerSender: MessagesSender {\n    func send(message: String, to user: User) {\n        requestSender?.perform(requestFactory.messageRequest(message: message, to: user)\n    }\n}\n\nclass iSeekU: Messenger {\n\n    var authorizedUser: User?\n    var requestSender: RequestSender?\n    var requestFactory: RequestFactory?\n    var messagesSender: MessengerMessagesSender?\n\n    func authorize(login: String, password: String) {\n        authorizedUser = requestSender?.perform(requestFactory.loginRequest(login: login, password: password))\n    }\n    \n    func send(message: String, to user: User) {\n        messagesSender?.send(message: message, to: user)\n    }\n}\n\nclass SpamBot {\n\n    var messagesSender: MessagesSender?\n\n    func start(usersList: [User]) {\n        let iSeekUMessenger = iSeekU()\n        iSeekUMessenger.authorize(login: \"SpamBot\", password: \"SpamPassword\")\n        \n        for user in usersList {\n            messagesSender.send(message: \"Hey checkout demensdeum blog! http:\/\/demensdeum.com\", to: user)\n        }\n    }\n}\n<\/code><\/pre>\n<\/div>\n<p>One of the advantages of this approach is undoubtedly the ability to expand the functionality of the application by writing plugins\/libraries that implement abstracted logic without changing the code of the main application.<br \/>What&#8217;s the difference with the Strategy pattern? Both patterns are very similar, but Strategy describes switching *algorithms*, while Bridge allows switching large parts of *any logic, no matter how complex*.<\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/refactoring.guru\/ru\/design-patterns\/bridge\" target=\"_blank\" rel=\"noopener\">https:\/\/refactoring.guru\/ru\/design-patterns\/bridge <\/a><\/p>\n<h3>Source code<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/patterns\/\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum\/patterns\/<\/a><\/a> p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Bridge pattern is a structural design pattern. It allows you to abstract the implementation of class logic by moving the logic into a separate abstract class. Sounds simple, right? Let&#8217;s say we are implementing a spam bot that should be able to send messages to different types of messengers.We implement it using a common<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/2019\/06\/01\/bridge-pattern\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Pattern Bridge&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[106,95],"class_list":["post-2042","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-bridge","tag-patterns","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"en","enabled_languages":["en","ru","zh","de","fr","ja","pt"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/comments?post=2042"}],"version-history":[{"count":12,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2042\/revisions"}],"predecessor-version":[{"id":3955,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/posts\/2042\/revisions\/3955"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/media?parent=2042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/categories?post=2042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/wp-json\/wp\/v2\/tags?post=2042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}