{"id":9172,"date":"2020-10-19T11:00:17","date_gmt":"2020-10-19T02:00:17","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9172"},"modified":"2020-10-16T19:43:20","modified_gmt":"2020-10-16T10:43:20","slug":"bridge-design-pattern","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9172","title":{"rendered":"Bridge Design Pattern"},"content":{"rendered":"\n<p>Bridge is a structural design pattern that allows you to divide a large class or a set of closely related classes into two separate hierarchies (abstraction and implementation) that can be developed independently of each other.<\/p>\n\n\n\n<p>Bridge is a synonym for the expression &#8220;handle \/ body&#8221;. This is a design mechanism that encapsulates an implementation class within an interface class. The first is the body and the second is the handle. The user sees the identifier as the actual class, but the work is done in the body. &#8220;The idiom for the handle \/ body class can be used to decompose a complex abstraction into smaller, more manageable classes. The idiom can reflect the sharing of a single resource by multiple classes that control access to it (for example, the count of references)&#8221;.<\/p>\n\n\n\n<ul><li>Decouple an abstraction from your implementation so that the two can vary independently.<\/li><li>Publish the interface in an inheritance hierarchy and hide the implementation in its own inheritance hierarchy.<\/li><\/ul>\n\n\n\n<p>Lets take a look at the code samples I created as below.<\/p>\n\n\n\n<h3>PHP Code Sample<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/**\n * This is the original application interface \n * where we place the methods need to develop from its concrete sub classes\n *\/\ninterface ApplicationInterface\n{\n    public function setDbDriver(DbDriver $dbDriver);\n \n    public function query($query);\n}\n\n\/**\n * This abstract class will implements the interface as we have to reference the next hierarchy object here.\n * After that we can access the methods of their sub concrete classes in our sub classes. see below.\n *\/\nabstract class Application implements ApplicationInterface\n{\n   protected $dbDriver;\n \n   public function setDbDriver(DbDriver $dbDriver)\n   {\n       $this->dbDriver = $dbDriver;\n   }\n}\n\n\/**\n* Concrete sub classes of the original class working with the reference object's method.\n*\/\nclass android extends Application\n{\n    public function query($query)\n    {\n        $query .= \"\\n\\n running android app query\\n\";\n \n        return $this->dbDriver->handleQuery($query);\n    }\n}\n \nclass ios extends Application\n{\n    public function query($query)\n    {\n        $query .= \"\\n\\n running ios app query\\n\";\n \n        return $this->dbDriver->handleQuery($query);\n    }\n}\n\n\n\/**\n* This is the interface that need to be referenced by original class instead of inheritance.\n*\/\ninterface DbDriver\n{\n    public function handleQuery($query);\n}\n\n\/**\n* Concrete classes that will have the detail implementations of the interface.\n*\/\nclass MysqlDriver implements DbDriver\n{\n    public function handleQuery($query)\n    {\n        echo \"\\nUsing the mysql driver: \".$query;\n    }\n}\n \n \nclass OracleDriver implements DbDriver\n{\n    public function handleQuery($query)\n    {\n        echo \"\\nUsing the oracle driver: \".$query;\n    }\n}\n\n\n\/\/client code\n\/\/ client doesn't need to know any implementation details. \n\/\/ Just build the original concrete class and inject the concrete object that will be referenced.\n \n$android = new android();\n$android->setDbDriver(new MysqlDriver());\necho $android->query(\"select * from table\");\n \n$android->setDbDriver(new OracleDriver());\necho $android->query(\"select * from table\");\n \n\n$ios = new ios();\n$ios->setDbDriver(new MysqlDriver());\necho $ios->query(\"select * from table\");\n\n$ios->setDbDriver(new OracleDriver());\necho $ios->query(\"select * from table\");\n \n<\/code><\/pre>\n\n\n\n<h3>C# Code Sample<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\n\nnamespace HelloWorld\n{\n    \/**\n     * Two Original classes referencing the same DbDriver which is the next hierarchy object\n     * After that we can access the methods of their sub concrete classes in our sub classes. query method in this case\n     *\/\n    class ApplicationInterface\n    {\n        protected DbDriver  _dbdriver;\n\n        public ApplicationInterface(DbDriver  dbdriver)\n        {\n            this._dbdriver = dbdriver;\n        }\n\n        public virtual string setDbDriver()\n        {\n            return \"Base DB Driver:\" +\n                _dbdriver.query();\n        }\n    }\n\n    class RefinedAbstraction : ApplicationInterface\n    {\n        public RefinedAbstraction(DbDriver  dbdriver) : base(dbdriver)\n        {\n        }\n\n        public override string setDbDriver()\n        {\n            return \"Refined DB Driver:\" +\n                base._dbdriver.query();\n        }\n    }\n\n\n    \/**\n    * This is the interface that need to be referenced by original class instead of inheritance.\n    *\/\n    public interface DbDriver \n    {\n        string query();\n    }\n\n    \/**\n    * Concrete classes that will have the detail implementations of the interface.\n    *\/\n    class MysqlDriver : DbDriver \n    {\n        public string query()\n        {\n            return \"Using the mysql driver:\\n\";\n        }\n    }\n\n    class OracleDriver : DbDriver \n    {\n        public string query()\n        {\n            return \"Using the oracle driver:.\\n\";\n        }\n    }\n\n    \/\/ client doesn't need to know any implementation details. \n    \/\/ Just build the original class and inject the concrete object that will be referenced.\n    class Client\n    {\n        public void ClientCode(ApplicationInterface applicationInterface)\n        {\n            Console.Write(applicationInterface.setDbDriver());\n        }\n    }\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            Client client = new Client();\n\n            ApplicationInterface applicationInterface;\n\n            applicationInterface = new ApplicationInterface(new MysqlDriver());\n            client.ClientCode(applicationInterface);\n\n            Console.WriteLine();\n\n            applicationInterface = new RefinedAbstraction(new OracleDriver());\n            client.ClientCode(applicationInterface);\n        }\n    }\n\n}\n<\/code><\/pre>\n\n\n\n<p>By Yuuma.<a href=\"https:\/\/github.com\/HlaingTinHtun\/GOF-Design-Patterns\/blob\/master\/articles\/GOF_Patterns\/structural_patterns\/bridge.md#c-code-sample\"><\/a><\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9172\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9172\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Bridge Design Pattern\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9172\" data-text=\"Bridge Design Pattern\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9172\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9172\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Bridge is a structural design pattern that allows you to divide a large class or a set of closely related clas [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9172"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9172"}],"version-history":[{"count":3,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9172\/revisions"}],"predecessor-version":[{"id":10621,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9172\/revisions\/10621"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}