{"id":19,"date":"2020-05-26T15:57:43","date_gmt":"2020-05-26T12:57:43","guid":{"rendered":"http:\/\/www.gmerden.com\/?p=19"},"modified":"2026-01-31T21:14:21","modified_gmt":"2026-01-31T21:14:21","slug":"strategy-pattern","status":"publish","type":"post","link":"https:\/\/www.gmerden.com\/index.php\/2020\/05\/26\/strategy-pattern\/","title":{"rendered":"Strategy Pattern"},"content":{"rendered":"\n<p>Strategy pattern, comes under behaviour patterns. Why it is using much and how effecting strategy of development. Main strategy is defining common operations and seperate them different classes. Thus we can maintain easly.<\/p>\n\n\n\n<p>In our example, we will create a repository class which connecting different db types but doing same things.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.gmerden.com\/wp-content\/uploads\/2020\/05\/Ekran-Resmi-2020-05-26-15.40.21-1024x692.png\" alt=\"\" class=\"wp-image-124\"\/><\/figure>\n\n\n\n<p>As we see, it is seperated different classes, so we can add different type of db such as mysql or mongo without changing our architecture.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace strategy_pattern {\n    public interface IdbOperations&lt;T&gt; {\n        void insert(T data);\n        void update(T data);\n        void delete(T data);\n        List&lt;T&gt; getlist();\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace strategy_pattern {\n    public class MssqlDbOperations : IdbOperations&lt;object&gt;\n    {\n        public void delete(object data)\n        {\n            throw new System.NotImplementedException();\n        }\n        public List&lt;object&gt; getlist()\n        {\n            throw new System.NotImplementedException();\n        }\n        public void insert(object data)\n        {\n            throw new System.NotImplementedException();\n        }\n        public void update(object data)\n        {\n            throw new System.NotImplementedException();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace strategy_pattern {\n    public class MysqlDbOperations : IdbOperations&lt;object&gt;\n    {\n        public void delete(object data)\n        {\n            throw new System.NotImplementedException();\n        }\n        public List&lt;object&gt; getlist()\n        {\n            throw new System.NotImplementedException();\n        }\n        public void insert(object data)\n        {\n            throw new System.NotImplementedException();\n        }\n        public void update(object data)\n        {\n            throw new System.NotImplementedException();\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace strategy_pattern\n{\n    class Program\n    {\n        static void Main(string&#91;] args)\n        {\n            MssqlDbOperations operationsMssql = new MssqlDbOperations();\n            operationsMssql.insert(new Object());\n            MysqlDbOperations operationsMysql = new MysqlDbOperations();\n            operationsMysql.insert(new Object());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Consequently, we can add new classes for new types of db thus we don&#8217;t need to change existing code. Also we stayed loyal to S of SOLID principles.<\/p>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>Strategy pattern, comes under behaviour patterns. Why it is using much and how effecting strategy of development. Main strategy is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[3],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-software-development"],"_links":{"self":[{"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":4,"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":448,"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/posts\/19\/revisions\/448"}],"wp:attachment":[{"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gmerden.com\/index.php\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}