{"id":886,"date":"2017-02-04T22:41:23","date_gmt":"2017-02-04T13:41:23","guid":{"rendered":"http:\/\/okamurax.com\/?p=886"},"modified":"2021-11-21T21:47:47","modified_gmt":"2021-11-21T12:47:47","slug":"c-adapter","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=886","title":{"rendered":"C# Adapter"},"content":{"rendered":"<pre class=\"lang:c# decode:true \">using System.Windows.Forms;\r\n\r\nnamespace WindowsFormsApp2\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            ITarget target = new Adapter(new Adaptee());\r\n            target.Request();\r\n\r\n            \/\/ Adaptee\u306f\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u6e21\u3055\u305a\u3001\u5185\u90e8\u3067\u751f\u6210\u3057\u3066\u3044\u308b\u5834\u5408\u3082\u3042\u308b\u3002\r\n            ITarget target2 = new Adapter2(\"Hello World\");\r\n            target2.Request();\r\n        }\r\n    }\r\n\r\n    \/\/ Wrapper\u30d1\u30bf\u30fc\u30f3\u3068\u3082\u3044\u3046\u3002(Adapter\u304c\u30e9\u30c3\u30d1\u30fc)\r\n    \/\/ Adaptee\u304c\u5b9f\u7e3e\u306e\u3042\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u518d\u5229\u7528\u3057\u305f\u3044\u5834\u5408\u306a\u3069\r\n\r\n    \/\/ \u57fa\u672c\u306f\u3001Client\u304b\u3089\u306f(Adapter\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6301\u3063\u305f)Target\u3092\u5229\u7528\u3059\u308b\u3002\r\n    \/\/ Adapter\u306e\u4e2d\u3067\u306fTarget\u306e\u30e1\u30bd\u30c3\u30c9\u304cAdaptee\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3093\u3067\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u3002\r\n\r\n    public interface ITarget\r\n    {\r\n        void Request(); \/\/ \u5fc5\u8981\u306a\u6a5f\u80fd\r\n    }\r\n\r\n    public class Adapter : ITarget\r\n    {\r\n        private Adaptee adaptee;\r\n        public Adapter(Adaptee target)\r\n        {\r\n            this.adaptee = target;\r\n        }\r\n\r\n        public void Request()\r\n        {\r\n            adaptee.AdapteeMethod();\r\n        }\r\n    }\r\n    public class Adaptee\r\n    {\r\n        public void AdapteeMethod()\r\n        {\r\n            \/\/ \u5b9f\u969b\u306b\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\u6a5f\u80fd\r\n        }\r\n    }\r\n\r\n    \/\/ --------------------------------------------------------\r\n\r\n    public class Adapter2 : ITarget\r\n    {\r\n        private Adaptee2 adaptee2;\r\n        private string extra;\r\n\r\n        public Adapter2(string extra)\r\n        {\r\n            this.extra = extra;\r\n            this.adaptee2 = new Adaptee2();\r\n        }\r\n\r\n        public void Request()\r\n        {\r\n            MessageBox.Show(this.extra);\r\n            adaptee2.Adaptee2Method();\r\n        }\r\n\r\n    }\r\n    public class Adaptee2\r\n    {\r\n        public void Adaptee2Method()\r\n        {\r\n            \/\/ \u5b9f\u969b\u306b\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\u6a5f\u80fd\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using System.Windows.Forms; namespace WindowsFormsApp2 { public partial class Form1 : Form { public Form1() {  &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=886\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Adapter&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9,144],"class_list":["post-886","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net","tag-144"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=886"}],"version-history":[{"count":4,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/886\/revisions"}],"predecessor-version":[{"id":4643,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/886\/revisions\/4643"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}