{"id":1148,"date":"2019-05-10T20:58:19","date_gmt":"2019-05-10T20:58:19","guid":{"rendered":"https:\/\/yazilimperver.com\/?p=1148"},"modified":"2019-10-12T12:53:30","modified_gmt":"2019-10-12T12:53:30","slug":"solid-1-tek-sorumluluk-prensibi","status":"publish","type":"post","link":"https:\/\/yazilimperver.com\/index.php\/2019\/05\/10\/solid-1-tek-sorumluluk-prensibi\/","title":{"rendered":"[:tr]SOLID 1 &#8211; Tek Sorumluluk Prensibi[:en]SOLID 1 &#8211; Single-Responsiblity Principle (SRP)[:]"},"content":{"rendered":"<p>[:tr]Evet arkada\u015flar, uzunca bir s\u00fcredir, modern C++ ile ilgili yaz\u0131lar payla\u015f\u0131yordum sizler ile ve yaz\u0131l\u0131m m\u00fchendisli\u011fine ili\u015fkin konulara biraz uzak kald\u0131k. Bu yaz\u0131 ile birlikte, biraz daha bu kavramlara e\u011filmeye ba\u015flayaca\u011f\u0131m. Daha \u00f6nce baz\u0131 kavramlara ili\u015fkin yaz\u0131lar\u0131m olmu\u015ftu, in\u015fallah \u00f6n\u00fcm\u00fczdeki d\u00f6nemde bunlar\u0131n s\u0131kl\u0131\u011f\u0131n\u0131 artt\u0131rmay\u0131 planl\u0131yorum. Gelelim bu yaz\u0131m\u0131n (daha do\u011frusu art\u0131k yaz\u0131 dizisi) konusuna.<br \/>\nYaz\u0131m\u0131 tek bir kelime ile \u00f6zetleyecek olursam: SOLID. Pek anlaml\u0131 gelmemi\u015f olabilir. S.O.L.I.D. \u015feklinde yazarsam baz\u0131 kelimelerin k\u0131saltmalar\u0131 oldu\u011funu hemen anlayacaks\u0131n\u0131z.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1000\/1*qfk6AFv4OF1GRd1WZJc25A.jpeg\" \/><\/p>\n<p>Sizleri daha fazla merakta b\u0131rakmadan, S.O.L.I.D.&#8217;in neyin k\u0131saltmas\u0131 oldu\u011funu sizler ile payla\u015fay\u0131m.<\/p>\n<p style=\"text-align: left;\"><strong><span style=\"color: #000080;\">S &#8211; Single-responsiblity principle (SRP)<\/span><\/strong><br \/>\n<strong><span style=\"color: #000080;\">O &#8211; Open-closed principle (OCP)<\/span><\/strong><br \/>\n<strong><span style=\"color: #000080;\">L &#8211; Liskov substitution principle (LSP)<\/span><\/strong><br \/>\n<strong><span style=\"color: #000080;\">I &#8211; Interface segregation principle (ISP)<\/span><\/strong><br \/>\n<strong><span style=\"color: #000080;\">D &#8211; Dependency Inversion Principle (DIP)<\/span><\/strong><\/p>\n<p>Bunlar\u0131n her birisi, zaman\u0131nda Robert C. Martin, kendisi Bob amca olarak da bilinir, taraf\u0131ndan Nesne Y\u00f6nelimli Yaz\u0131l\u0131m Geli\u015ftirme i\u00e7in uyulmas\u0131 gereken asgari prensipler olarak tan\u0131mlanm\u0131\u015ft\u0131r. Daha fazla detay i\u00e7in <span style=\"color: #339966;\"><strong><a style=\"color: #339966;\" href=\"https:\/\/fi.ort.edu.uy\/innovaportal\/file\/2032\/1\/design_principles.pdf\">\u015fu dok\u00fcmana<\/a><\/strong><\/span> da g\u00f6z atabilirsiniz.\u00a0Nesne y\u00f6nelimli programlama yakla\u015f\u0131m\u0131, <span style=\"color: #339966;\"><strong><a style=\"color: #339966;\" href=\"https:\/\/en.wikipedia.org\/wiki\/Structured_programming\">yap\u0131sal programlama<\/a> <\/strong><\/span>yakla\u015f\u0131m\u0131na g\u00f6re tekrar kullan\u0131labilirli\u011fi\/esnekli\u011fi artt\u0131r\u0131p, karma\u015f\u0131kl\u0131\u011f\u0131 kotarabilse de, yanl\u0131\u015f kullan\u0131m, bunun tam tersine de sebep olabilir. Bu prensiplerin temel amac\u0131 da asl\u0131nda bu tarz s\u0131k\u0131nt\u0131lar\u0131 gidererek, daha <strong>\u00e7evik<\/strong>, <strong>tekrar kullan\u0131labilir<\/strong> ve <strong>idame edilebilir<\/strong> yaz\u0131l\u0131mlar elde etmek.<\/p>\n<p>Bu ba\u011flamda, nesne y\u00f6nelimli yaz\u0131l\u0131m geli\u015ftiren hatta, \u00e7o\u011fu yaz\u0131l\u0131mc\u0131n\u0131n bu kavramlara vak\u0131f olmas\u0131, bana kal\u0131rsa olduk\u00e7a elzem. O sebeple daha fazla vakit kaybetmeden bunlar\u0131n her birisine g\u00f6z atal\u0131m.<\/p>\n<p>B\u00fct\u00fcn bu prensipleri tek bir yaz\u0131da anlatmaktansa, ayr\u0131 ayr\u0131 yaz\u0131larda anlatmaya karar verdim. Hem daha anla\u015f\u0131l\u0131r hem de daha kolay okunabilir olaca\u011f\u0131n\u0131 d\u00fc\u015f\u00fcn\u00fcyorum. Bu yaz\u0131mda Tek Sorumluluk &#8220;Single-Responsibility&#8221; prensibine g\u00f6z ataca\u011f\u0131z.<\/p>\n<p><span style=\"font-weight: 400;\">Di\u011fer SOLID yaz\u0131lar\u0131na a\u015fa\u011f\u0131daki adreslerden ula\u015fabilirsiniz. <\/span><\/p>\n<ol>\n<li><strong><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/yazilimperver.com\/index.php\/2019\/05\/10\/solid-1-tek-sorumluluk-prensibi\/\">SOLID 1 &#8211; Tek Sorumluluk Prensibi<\/a><\/span><\/strong><\/li>\n<li><strong><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/yazilimperver.com\/index.php\/2019\/08\/06\/solid-2-acik-kapali-prensibi\/\">SOLID 2 &#8211; A\u00e7\u0131k\/Kapal\u0131 Prensibi<\/a><\/span><\/strong><\/li>\n<li><a href=\"https:\/\/yazilimperver.com\/index.php\/2019\/09\/08\/solid-3-liskovun-yerine-gecme-prensibi\/\"><strong><span style=\"color: #008000;\">SOLID 3 &#8211; &#8220;Liskov Substitution&#8221; Prensibi<\/span><\/strong><\/a><\/li>\n<li><a href=\"https:\/\/yazilimperver.com\/?p=1430&amp;preview=true\"><span style=\"color: #008000;\"><strong>SOLID 4 &#8211; Aray\u00fcz Ayr\u0131\u015ft\u0131rma Prensibi<\/strong><\/span><\/a><\/li>\n<\/ol>\n<h2><span style=\"color: #0000ff;\"><strong>Tek Sorumluluk Prensibi (Single-Responsiblity Principle):<\/strong><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/waog.files.wordpress.com\/2014\/08\/army-2185_640.jpg\" alt=\"Image result for single responsibility image\" width=\"367\" height=\"244\" \/><\/p>\n<p>Prensibin isminden de anla\u015f\u0131laca\u011f\u0131 gibi, bu kavram\u0131n en \u00f6nemli amac\u0131: ilgili kodun, nesne y\u00f6nelimli yaz\u0131l\u0131mlar i\u00e7in, s\u0131n\u0131f\u0131n, sadece tek bir sorumlulu\u011fu olmas\u0131d\u0131r. Bu biraz da asl\u0131nda, yaz\u0131l\u0131m tasar\u0131m\u0131 i\u00e7in \u00f6nemli olan tutarl\u0131l\u0131k\/kohezyon (&#8220;<em>cohesion<\/em>&#8220;) kriteri ile de yak\u0131ndan ilintilidir. Bu kavram, verilen bile\u015fenin metot ve verilerinin birbirleri ve ger\u00e7ekle\u015ftirilecek olan yegane fonksiyonalite ile ne kadar ilintili oldu\u011funu ifade eder. Y\u00fcksek d\u00fczeyde kohezyon, iyi bir yaz\u0131l\u0131m tasar\u0131m\u0131 elde etmede b\u00fcy\u00fck katk\u0131 sa\u011flar.<\/p>\n<p>Bob amca, bu prensibe dair ayr\u0131ca &#8220;s\u0131n\u0131f\u0131 de\u011fi\u015ftirmemiz i\u00e7in sadece bir sebebimiz olmal\u0131&#8221; gibi olduk\u00e7a \u00fcnl\u00fc bir tan\u0131m da yapm\u0131\u015ft\u0131r (bunun detaylar\u0131 i\u00e7in verdi\u011fim <span style=\"color: #339966;\"><strong><a style=\"color: #339966;\" href=\"https:\/\/blog.cleancoder.com\/uncle-bob\/2014\/05\/08\/SingleReponsibilityPrinciple.html\">\u015fu adrese<\/a> <\/strong><\/span>bir g\u00f6z atabilirsiniz). Bir di\u011fer ifade ile s\u0131n\u0131f i\u00e7erisindeki elemanlar\u0131n de\u011fi\u015fimi ayn\u0131 sebepten olmal\u0131 ve de\u011fi\u015fiklik i\u00e7in farkl\u0131 sebeplere ihtiya\u00e7 duyulan \u015feyler ayr\u0131lmal\u0131d\u0131r. Peki sebepten kas\u0131t nedir? Burada k\u0131saca bir \u00f6rnek \u00fczerinden bunu anlatmaya \u00e7al\u0131\u015fay\u0131m.<\/p>\n<p>\u00d6rne\u011fin elimizde, uygulamam\u0131z\u0131n d\u0131\u015f d\u00fcnya ile haberle\u015fmesinden sorumlu bir s\u0131n\u0131f oldu\u011funu d\u00fc\u015f\u00fcnelim ve \u00e7ok temel olarak a\u015fa\u011f\u0131daki kabiliyetleri i\u00e7erdi\u011fini farz edelim:<\/p>\n<pre class=\"lang:c++ decode:true\">class ExternalCommunication\r\n{\r\npublic:\r\n    \/\/ Soket mesaji gonder\r\n    bool sendTCPMessage(const char* rawData);\r\n\r\n    \/\/ En son alinan mesajlari kaydet\r\n    void saveMessageHistoryAsXML();\r\n};<\/pre>\n<p>Yukar\u0131da verdi\u011fim s\u0131n\u0131fa bakt\u0131\u011f\u0131m\u0131zda, temel olarak iki fonksiyon g\u00f6r\u00fclmekte: TCP\/IP soketi \u00fczerinden haberle\u015fme ve haberle\u015fme ge\u00e7mi\u015finin kaydedilmesi. Mesaj g\u00f6nderilmesi ve mesaj ge\u00e7mi\u015fin kaydedilmesinin her birini ayr\u0131 bir kabiliyet olarak d\u00fc\u015f\u00fcnebiliriz. Bu ba\u011flamda olabilecek de\u011fi\u015fiklikleri \u00f6ng\u00f6rd\u00fc\u011f\u00fcm\u00fczde, burada kay\u0131t i\u00e7in XML format\u0131 kullan\u0131l\u0131rken, JSON&#8217;\u0131 destekleme durumu olabilir. Ayn\u0131 zamanda TCP yerine seri kanal ya da UDP kullanma durumu olabilir. G\u00f6rd\u00fc\u011f\u00fcn\u00fcz \u00fczere, bu s\u0131n\u0131f de\u011fi\u015fmesi i\u00e7in birden fazla sebep ortaya \u00e7\u0131kt\u0131! SRP&#8217;ye g\u00f6re bunlardan sadece birinin de\u011fi\u015fikli\u011fe sebebiyet vermesini bekliyoruz. \u015eimdi bu kod par\u00e7as\u0131n\u0131 SRP&#8217;ye uygun hale getirmeye \u00e7al\u0131\u015fal\u0131m.<\/p>\n<p>\u00d6ncelikli olarak mesaj ge\u00e7mi\u015fini yazan i\u015flevi ayr\u0131 bir s\u0131n\u0131fa alal\u0131m. Bunu da yaparken birden fazla kay\u0131t format\u0131 olabilece\u011fini d\u00fc\u015f\u00fcnelim. Bu durumu kotarmak i\u00e7in a\u015fa\u011f\u0131daki gibi s\u0131n\u0131flar\u0131 olu\u015fturabiliriz:<\/p>\n<pre class=\"lang:c++ decode:true\">class IMessageHistoryRecorder\r\n{\r\npublic:\r\n\tvirtual void saveMessageHistory(const std::vector&lt;MessageData&gt;&amp; hisory) = 0;\r\n};\r\n\r\nclass XMLMessageHistoryRecorder\r\n: public IMessageHistoryRecorder\r\n{\r\npublic:\r\n\tvirtual void saveMessageHistory(const std::vector&lt;MessageData&gt;&amp; hisory)\r\n\t{\r\n\t     std::cout &lt;&lt;\"Message history is recorded as XML file!\" &lt;&lt; '\\n';\r\n\t}\r\n};\r\n\r\nclass JSONMessageHistoryRecorder\r\n: public IMessageHistoryRecorder\r\n{\r\npublic:\r\n\tvirtual void saveMessageHistory(const std::vector&lt;MessageData&gt;&amp; hisory)\r\n\t{\r\n\t     std::cout &lt;&lt;\"Message history is recorded as JSON file!\" &lt;&lt; '\\n';\r\n\t}\r\n};<\/pre>\n<p>Art\u0131k yeni bir mesaj kay\u0131t format\u0131 kullanmam\u0131z gerekti\u011finde, <em>ExternalCommunication<\/em> s\u0131n\u0131f\u0131n\u0131 de\u011fi\u015ftirmemize gerek kalmayacak. Benzer \u015fekilde haberle\u015fme s\u0131n\u0131f\u0131 i\u00e7in de a\u015fa\u011f\u0131daki s\u0131n\u0131flar\u0131 tan\u0131mlay\u0131p kullanabiliriz.<\/p>\n<pre class=\"minimize:true lang:c++ decode:true\">class ICommunicationItem\r\n{\r\npublic:\r\n\tvirtual bool initialize() = 0;\r\n\tvirtual bool sendMessage(const char* rawData) = 0;\r\n};\r\n\r\nclass TCPSocketCommunication\r\n\t: public ICommunicationItem\r\n{\r\npublic:\r\n\tvirtual bool initialize()\r\n\t{\r\n\t\t\/\/ Soket haberle\u015fmesine ili\u015fkin ilklendirmeler\r\n\t}\r\n\tvirtual bool sendMessage(const char* rawData)  override\r\n\t{\r\n\t\t\/\/ Soket uzerinden mesaj gonderme kodu\r\n\t}\r\n\r\n\t\/\/ Sokete iliskin ozellesmis metotlar\r\n\tvoid setSocketParameters(const string ipAddress, int portNo)\r\n\t{\r\n\t\t\/\/ Soket parametreleri\r\n\t}\r\n};\r\n\r\nclass SerialCommunication\r\n\t: public ICommunicationItem\r\n{\r\npublic:\r\n\tvirtual bool initialize()\r\n\t{\r\n\t\t\/\/ Seri kanal uzerinden haberle\u015fmeye ili\u015fkin ilklendirmeler\r\n\t}\r\n\r\n\tvirtual bool sendMessage(const char* rawData)  override\r\n\t{\t\t\r\n\t\t\/\/ Seri kanal uzerinden mesaj gonderme\r\n\t}\r\n\r\n\t\/\/ Seri kanala iliskin ozellesmis metotlar\r\n\tvoid setSerialPortParameters(eBaudRate baudRate, eDataBit dataBits, eParity parity,)\r\n\t{\r\n\t\t\/\/ Seri kanal parametreleri\r\n\t}\r\n};<\/pre>\n<p>Nihai olarak s\u0131n\u0131f\u0131m\u0131z a\u015fa\u011f\u0131daki gibi olacak ve SRP&#8217;i da sa\u011flam\u0131\u015f ve olduk\u00e7a geni\u015fleyebilir bir s\u0131n\u0131f elde etmi\u015f olduk \ud83d\ude42<\/p>\n<pre class=\"minimize:true lang:c++ decode:true\">class ExternalCommunication\r\n{\r\npublic:    \r\n    \/\/ mesaji gonder\r\n    bool sendMessage(const char* rawData)\r\n    {\r\n    \t... \r\n\r\n    \tif(nullptr != mCommunicationMedium)\r\n    \t\tmCommunicationMedium-&gt;sendMessage(rawData);\r\n\r\n    \t... \r\n    }\r\n\r\n    \/\/ En son alinan mesajlari kaydet\r\n    void saveMessageHistory()\r\n    {\r\n    \t... \r\n\r\n    \tif(nullptr != mMessageRecorder)\r\n    \t{\r\n    \t\tmMessageRecorder-&gt;saveMessageHistory(mRecordedMessageData);\r\n    \t\tmRecordedMessageData.clear();\r\n    \t}\r\n\r\n    \t...     \t\r\n    }\r\n\r\n\r\n    \/\/ Mesajlari kaydetmek i\u00e7in kullanaca\u011f\u0131m\u0131z s\u0131n\u0131f atayal\u0131m\r\n    void setMessageRecoder(IMessageHistoryRecorder* recorder)\r\n    {\r\n    \tmMessageRecorder = recorder;\r\n    }\r\n\r\n    \/\/ Haberle\u015fme alt yap\u0131s\u0131na ili\u015fkin s\u0131n\u0131flar\u0131 gecirelim\r\n    void setCommunicationItem(ICommunicationItem* commItem)\r\n    {\r\n    \tmCommunicationMedium = commItem;\r\n    }\r\nprotected:\r\n     IMessageHistoryRecorder* mMessageRecorder{nullptr};\r\n     ICommunicationItem* mCommunicationMedium{nullptr};\r\n\r\n     std::vector&lt;MessageData&gt; mRecordedMessageData;\r\n};<\/pre>\n<p>Yukar\u0131daki \u00f6rnek yan\u0131nda, Bob amcan\u0131n kulland\u0131\u011f\u0131 \u00f6rne\u011fi de sizler ile payla\u015fmak istiyorum. A\u015fa\u011f\u0131da, \u00f6rne\u011fe ili\u015fkin s\u0131n\u0131f diyagram\u0131n\u0131 g\u00f6rebilirsiniz.<\/p>\n<p id=\"xjswrjS\"><img decoding=\"async\" src=\"http:\/\/rcardin.github.io\/assets\/2017-12-26\/srp_wrong_design.png\" alt=\"Violation of SRP\" \/><\/p>\n<p>Burada g\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere <em>Rectangle<\/em> s\u0131n\u0131f\u0131m\u0131z\u0131n en az iki adet sorumlulu\u011fu var. Bunlar: <em>GraphicalApplication<\/em> taraf\u0131ndan, grafiksel kullan\u0131c\u0131 aray\u00fcz\u00fc (<em>GUI<\/em>) arac\u0131l\u0131\u011f\u0131 ile bir dikd\u00f6rtgen \u00e7izdirilmesi\u00a0 (<em>draw<\/em>() API&#8217;si) ve <em>ComputationalGeometryApp<\/em> taraf\u0131ndan dikd\u00f6rtgenin alan\u0131n\u0131n hesaplan\u0131larak kullan\u0131lmas\u0131 (<em>area<\/em>() API&#8217;si). \u0130lk bak\u0131\u015fta herhangi bir problem g\u00f6r\u00fclmeyebilir, hadi biraz daha yak\u0131ndan bakal\u0131m.<\/p>\n<p>Buradaki g\u00f6rece\u011fimiz ilk s\u0131k\u0131nt\u0131, sadece dikd\u00f6rtgen alan hesaplamas\u0131na ihtiya\u00e7 duyan <em>ComputationalGeometryApp<\/em> s\u0131n\u0131f\u0131n\u0131n, dolayl\u0131 yoldan <em>GUI&#8217;ye<\/em> de ba\u011f\u0131ml\u0131l\u0131k i\u00e7ermesidir. Benzer \u015fekilde <em>GUI<\/em> ile ilgili bir de\u011fi\u015fiklikten \u00f6t\u00fcr\u00fc\u00a0<em>ComputationalGeometryApp<\/em> uygulamas\u0131na da bu de\u011fi\u015fiklik yans\u0131yabilir. Bu da asl\u0131nda de\u011fi\u015fim i\u00e7in sadece bir sebep olmal\u0131 kural\u0131n\u0131 ihlal ediyor.<\/p>\n<p>Peki bu s\u0131k\u0131nt\u0131lar\u0131 nas\u0131l giderebiliriz? Hemen bunu gideren tasar\u0131ma g\u00f6z atal\u0131m:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/rcardin.github.io\/assets\/2017-12-26\/srp_design.png\" alt=\"Design SRP-proof\" \/><\/p>\n<p>G\u00f6r\u00fclece\u011fi \u00fczere ba\u011f\u0131ml\u0131l\u0131klar\u0131n yukar\u0131da g\u00f6sterildi\u011fi \u015fekilde ayarlanmas\u0131 ile iki uygulama aras\u0131ndaki ba\u011f\u0131ml\u0131l\u0131k da ortadan kalkm\u0131\u015f oldu ve tasar\u0131m\u0131m\u0131z da SRP ile uyumlu hale geldi.<\/p>\n<p>Yukar\u0131daki \u00f6rneklerden sonra akla \u015fu tarz bir soru gelebilir? Peki ger\u00e7ekten ilgili s\u0131n\u0131f\u0131m\u0131z\u0131n birden fazla sorumlulu\u011fu olup olmad\u0131\u011f\u0131n\u0131 nas\u0131l anlayabiliriz? Bu noktada imdad\u0131m\u0131za, yaz\u0131m\u0131n ba\u015flar\u0131nda bahsetti\u011fim kohezyon kavram\u0131 yeti\u015fiyor. Buna \u00f6rnek te\u015fkil etmesi a\u00e7\u0131s\u0131ndan\u00a0<strong><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"http:\/\/www.yegor256.com\/2017\/12\/19\/srp-is-hoax.html\">SRP is a Hoax<\/a><\/span><\/strong>\u00a0sayfas\u0131nda verilen Java \u00f6rne\u011fine g\u00f6z ataca\u011f\u0131z:<\/p>\n<pre class=\"lang:c++ decode:true \">class AwsOcket {\r\n    boolean exists() { \/* ... *\/ }\r\n    void read(final OutputStream output) { \/* ... *\/ }\r\n    void write(final InputStream input) { \/* ... *\/ }\r\n}<\/pre>\n<p>\u015eimdi bu s\u0131n\u0131f \u00f6zelinde SRP&#8217;yi k\u00f6r\u00fc k\u00f6r\u00fcne uygulamaya kalkt\u0131\u011f\u0131m\u0131zda, muhtemelen elimizde <em>ExistanceChecker<\/em>, <em>ContentReader<\/em> ve <em>ContentWriter<\/em> gibi s\u0131n\u0131flar olacakt\u0131r. Basit bir \u015fekilde Aws s\u0131n\u0131f \u00fczerinde bir veri okuyup, g\u00f6r\u00fcnt\u00fclemek i\u00e7in a\u015fa\u011f\u0131daki gibi bir kodu kullanmam\u0131z gerekecektir:<\/p>\n<pre class=\"lang:c++ decode:true \">if (new ExistenceChecker(ocket.aws()).exists()) {\r\n  new ContentReader(ocket.aws()).read(System.out);\r\n}<\/pre>\n<p>G\u00f6r\u00fclece\u011fi \u00fczere, \u00e7o\u011fu zaman bu s\u0131n\u0131flar birlikte kullan\u0131lacaklar. Ayr\u0131ca bu s\u0131n\u0131flar, birbirleri ile y\u00fcksek kenetlenme (<em>coupling<\/em>) ve d\u00fc\u015f\u00fck seviyede kohezyon sergiliyorlar. O sebeple, bu s\u0131n\u0131f\u0131 asl\u0131nda bu \u015fekilde ay\u0131rmak do\u011fru de\u011fil.<\/p>\n<p>Normal \u015fartlarda da bu prensibi kullan\u0131rken, s\u0131n\u0131flar\u0131n\u0131z\u0131 \u00f6zellikle kohezyon seviyelerini iyi de\u011ferlendirmeniz gerekiyor.<\/p>\n<p>Bu soru ile birlikte ilk prensibimizi tamamlam\u0131\u015f oluyoruz. Haftaya ikinci prensipte g\u00f6r\u00fc\u015fmek \u00fczere, kendinize iyi bak\u0131n.<\/p>\n<h2><strong><span style=\"color: #0000ff;\">Kaynaklar:<\/span><\/strong><\/h2>\n<ul>\n<li><a href=\"https:\/\/blog.cleancoder.com\/uncle-bob\/2014\/05\/08\/SingleReponsibilityPrinciple.html\"><strong><span style=\"color: #008000;\">https:\/\/blog.cleancoder.com\/uncle-bob\/2014\/05\/08\/SingleReponsibilityPrinciple.html<\/span><\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/Designing-Object-Oriented-Applications-Method\/dp\/0132038374\"><strong><span style=\"color: #008000;\">Designing Object Oriented Applications using UML, 2d. ed., Robert C. Martin, Prentice Hall, 1999.<\/span><\/strong><\/a><\/li>\n<li><a href=\"http:\/\/www.yegor256.com\/2017\/12\/19\/srp-is-hoax.html\"><strong><span style=\"color: #008000;\">http:\/\/www.yegor256.com\/2017\/12\/19\/srp-is-hoax.html<\/span><\/strong><\/a><\/li>\n<li><a href=\"https:\/\/fi.ort.edu.uy\/innovaportal\/file\/2032\/1\/design_principles.pdf\"><strong><span style=\"color: #008000;\">https:\/\/fi.ort.edu.uy\/innovaportal\/file\/2032\/1\/design_principles.pdf<\/span><\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.wikiwand.com\/en\/Object-oriented_programming\"><strong><span style=\"color: #008000;\">https:\/\/www.wikiwand.com\/en\/Object-oriented_programming<\/span><\/strong><\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Structured_programming\"><strong><span style=\"color: #008000;\">https:\/\/en.wikipedia.org\/wiki\/Structured_programming<\/span><\/strong><\/a><\/li>\n<\/ul>\n<p>[:en]Yes, friends, I have been sharing posts about C++ and have not written many posts about software engineering stuff. This will change from this post and I will try to increase the frequency of such topics. Let us, talk about the topic of this post (in fact post series now on).<br \/>\nSo if I would summarize my post with one word, it would SOLID. It may not sound familiar, but if I say S.O.L.I.D. then you may immediately undestand that it is an acronym.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1000\/1*qfk6AFv4OF1GRd1WZJc25A.jpeg\" \/><\/p>\n<p>Let me share with you what S.O.L.I.D. stands for.<\/p>\n<p style=\"text-align: left;\"><strong><span style=\"color: #000080;\">S &#8211; Single-responsiblity principle (SRP)<\/span><\/strong><br \/>\n<strong><span style=\"color: #000080;\">O &#8211; Open-closed principle (OCP)<\/span><\/strong><br \/>\n<strong><span style=\"color: #000080;\">L &#8211; Liskov substitution principle (LSP)<\/span><\/strong><br \/>\n<strong><span style=\"color: #000080;\">I &#8211; Interface segregation principle (ISP)<\/span><\/strong><br \/>\n<strong><span style=\"color: #000080;\">D &#8211; Dependency Inversion Principle (DIP)<\/span><\/strong><\/p>\n<p>Each of these has been defined as the minimum principles to be followed for Object Oriented Software Development by Robert C. Martin who is also known as Uncle Bob. You can also refer to <span style=\"color: #008000;\"><strong><a style=\"color: #008000;\" href=\"https:\/\/fi.ort.edu.uy\/innovaportal\/file\/2032\/1\/design_principles.pdf\">this document<\/a><\/strong><\/span> for more details. Although the object-oriented programming approach can increase the reusability \/ flexibility compared to the <strong><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/en.wikipedia.org\/wiki\/Structured_programming\">structured programming<\/a> <\/span><\/strong>approach and handle the complexity, misuse can cause the contrary. The main purpose of these principles is to eliminate such problems and to obtain more <strong>agile, reusable <\/strong>and <strong>maintainable<\/strong> software.<\/p>\n<p>In this context, it is essential to me that most developers, especially those who develop object oriented software, should be familiar with these principles. So let&#8217;s take a look at each of these without wasting any more time.<\/p>\n<p>Instead of explaining all these principles in a single article, I decided to explain them in separate articles. I think, it will be more understandable and easier to read. In this article, we will look at the principle of Single Responsibility.<\/p>\n<p><span style=\"font-weight: 400;\">For other SOLID principles, you can check out following links:<\/span><\/p>\n<ol>\n<li><strong><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/yazilimperver.com\/index.php\/2019\/05\/10\/solid-1-tek-sorumluluk-prensibi\/\">SOLID 1 &#8211; Single-responsibility Principle<\/a><\/span><\/strong><\/li>\n<li><strong><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/yazilimperver.com\/index.php\/2019\/08\/06\/solid-2-acik-kapali-prensibi\/\">SOLID 2 &#8211; Open-closed Principle<\/a><\/span><\/strong><\/li>\n<li><a href=\"https:\/\/yazilimperver.com\/index.php\/2019\/09\/08\/solid-3-liskovun-yerine-gecme-prensibi\/\"><strong><span style=\"color: #008000;\">SOLID 3 &#8211; &#8220;Liskov Substitution&#8221; Principle<\/span><\/strong><\/a><\/li>\n<\/ol>\n<h2><span style=\"color: #0000ff;\"><strong>Single-Responsiblity Principle:<\/strong><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/waog.files.wordpress.com\/2014\/08\/army-2185_640.jpg\" alt=\"Image result for single responsibility image\" width=\"367\" height=\"244\" \/><\/p>\n<p>As its name implies, the most important purpose of this principle is: for object oriented software, each class should only have one responsibility.<\/p>\n<p>This is, in fact, also closely related to the cohesion criterion, which is important for software design. This concept expresses how much the method and data of the given component relate to each other to perform the designated class functionality. The high level of cohesion makes a great contribution to achieving good software design.<\/p>\n<p>Uncle Bob has also made a very famous definition of this principle: &#8220;We only have one reason to change the class&#8221; (you can take a look at <strong><span style=\"color: #008000;\"><a style=\"color: #008000;\" href=\"https:\/\/blog.cleancoder.com\/uncle-bob\/2014\/05\/08\/SingleReponsibilityPrinciple.html\">this address<\/a><\/span><\/strong> for details). In other words, the change of the elements in the class must be for the same reason. If different reasons are required for different changes, then those should be separated. So what do we mean by reason? Let me try to give a brief example here.<\/p>\n<p>For instance, let us assume that we have a class which is responsible for external communication and have following capabilities:<\/p>\n<pre class=\"lang:c++ decode:true\">class ExternalCommunication\r\n{\r\npublic:\r\n    \/\/ Send socket message\r\n    bool sendTCPMessage(const char* rawData);\r\n\r\n    \/\/ Save last received messages\r\n    void saveMessageHistoryAsXML();\r\n};<\/pre>\n<p>When we look at the class that I gave above, there are basically two functions: communication via TCP \/ IP socket and recording of communication history. We can think of sending each message and recording the message history as a separate capability. The possible changes that may happen, there may be a need for supporting JSON instead of XML format for recording. It may also be the case of using serial channels or UDP instead of TCP. As you can see, there are multiple reasons for this class for change! According to SRP, we expect only one of them to cause change. Let&#8217;s try to make this piece of code suitable for SRP.<\/p>\n<p>First of all, let us move history recording capability to a separate class. As we design this class, let us also consider multiple formats. We can develop following classes for this purpose:<\/p>\n<pre class=\"lang:c++ decode:true\">class IMessageHistoryRecorder\r\n{\r\npublic:\r\n\tvirtual void saveMessageHistory(const std::vector&lt;MessageData&gt;&amp; hisory) = 0;\r\n};\r\n\r\nclass XMLMessageHistoryRecorder\r\n: public IMessageHistoryRecorder\r\n{\r\npublic:\r\n\tvirtual void saveMessageHistory(const std::vector&lt;MessageData&gt;&amp; hisory)\r\n\t{\r\n\t     std::cout &lt;&lt;\"Message history is recorded as XML file!\" &lt;&lt; '\\n';\r\n\t}\r\n};\r\n\r\nclass JSONMessageHistoryRecorder\r\n: public IMessageHistoryRecorder\r\n{\r\npublic:\r\n\tvirtual void saveMessageHistory(const std::vector&lt;MessageData&gt;&amp; hisory)\r\n\t{\r\n\t     std::cout &lt;&lt;\"Message history is recorded as JSON file!\" &lt;&lt; '\\n';\r\n\t}\r\n};<\/pre>\n<p>When we need to use a new message recording format, we no longer need to change the <em>ExternalCommunication<\/em> class. Similarly, we can define and use the following classes for the communication purpose.<\/p>\n<pre class=\"minimize:true lang:c++ decode:true\">class ICommunicationItem\r\n{\r\npublic:\r\n  virtual bool initialize() = 0;\r\n  virtual bool sendMessage(const char* rawData) = 0;\r\n};\r\n\r\nclass TCPSocketCommunication\r\n  : public ICommunicationItem\r\n{\r\npublic:\r\n  virtual bool initialize()\r\n  {\r\n    \/\/ Initializations associated with socket communication\r\n  }\r\n  virtual bool sendMessage(const char* rawData)  override\r\n  {\r\n    \/\/ Code for sending message via socket\r\n  }\r\n\r\n  \/\/ Socket communication specific methods\r\n  void setSocketParameters(const string ipAddress, int portNo)\r\n  {\r\n    \/\/ Soket parameters\r\n  }\r\n};\r\n\r\nclass SerialCommunication\r\n  : public ICommunicationItem\r\n{\r\npublic:\r\n  virtual bool initialize()\r\n  {\r\n    \/\/ Initializations associated with serial communication\r\n  }\r\n\r\n  virtual bool sendMessage(const char* rawData)  override\r\n  {   \r\n    \/\/ Code for sending message via serial channel\r\n  }\r\n\r\n  \/\/ Serial channel communication specific methods\r\n  void setSerialPortParameters(eBaudRate baudRate, eDataBit dataBits, eParity parity,)\r\n  {\r\n    \/\/ Serial channel parametreleri\r\n  }\r\n};<\/pre>\n<p>Our final class will be as follow which also supports SRP. We have achieved a class that is quite expandable \ud83d\ude42<\/p>\n<pre class=\"minimize:true lang:c++ decode:true\">class ExternalCommunication\r\n{\r\npublic:    \r\n    \/\/ Send socket message\r\n    bool sendMessage(const char* rawData)\r\n    {\r\n      ... \r\n\r\n      if(nullptr != mCommunicationMedium)\r\n        mCommunicationMedium-&gt;sendMessage(rawData);\r\n\r\n      ... \r\n    }\r\n\r\n    \/\/ Save last received messages\r\n    void saveMessageHistory()\r\n    {\r\n      ... \r\n\r\n      if(nullptr != mMessageRecorder)\r\n      {\r\n        mMessageRecorder-&gt;saveMessageHistory(mRecordedMessageData);\r\n        mRecordedMessageData.clear();\r\n      }\r\n\r\n      ...       \r\n    }\r\n\r\n\r\n    \/\/ Assign instance that will be used for message recorder\r\n    void setMessageRecoder(IMessageHistoryRecorder* recorder)\r\n    {\r\n      mMessageRecorder = recorder;\r\n    }\r\n\r\n    \/\/ Assign instance that will be used for communication\r\n    void setCommunicationItem(ICommunicationItem* commItem)\r\n    {\r\n      mCommunicationMedium = commItem;\r\n    }\r\nprotected:\r\n     IMessageHistoryRecorder* mMessageRecorder{nullptr};\r\n     ICommunicationItem* mCommunicationMedium{nullptr};\r\n\r\n     std::vector&lt;MessageData&gt; mRecordedMessageData;\r\n};<\/pre>\n<p>In addition to this code example, I also want to share the example that is given by Uncle Bob. You can find class diagram related with that example below.<\/p>\n<p id=\"xjswrjS\"><img decoding=\"async\" src=\"http:\/\/rcardin.github.io\/assets\/2017-12-26\/srp_wrong_design.png\" alt=\"Violation of SRP\" \/><\/p>\n<div>It can be observed that <em>Rectangle<\/em> class has at least two responsibilities. These are: drawing a rectangle by <em>GraphicalApplication<\/em> via <em>GUI<\/em> (<em>draw<\/em>() API) and calculating rectangle area by <em>ComputationalGeometryApp<\/em> (<em>area<\/em>() API). At first glance, you may not see any problem, let&#8217;s take a closer look.<\/div>\n<p>The first problem we will see here is that the <em>ComputationalGeometryApp<\/em> class, which only needs a rectangular area calculation, also includes indirect dependency on the <em>GUI<\/em>. Similarly, due to a change in <em>GUI<\/em>, this change may cause another change in <em>ComputationalGeometryApp<\/em>. This, in fact, violates the rule that there must be only one reason for change.<\/p>\n<p>So how can we handle these issues? Let us look the resultant class diagram that resolve these issues:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/rcardin.github.io\/assets\/2017-12-26\/srp_design.png\" alt=\"Design SRP-proof\" \/><\/p>\n<p>As it can be seen, by adjusting the dependencies as shown above, the dependency between the two classes has also disappeared and our design has become compatible with SRP.<\/p>\n<p>After the above examples, one might ask: So how can we really tell if our class has more than one responsibility? At this point, the concept of cohesion that I mentioned at the beginning of my writing comes to our rescue. As an example, we will take a look at the Java example given at &#8220;<span style=\"color: #008000;\"><strong><a style=\"color: #008000;\" href=\"http:\/\/www.yegor256.com\/2017\/12\/19\/srp-is-hoax.html\">SRP is a Hoax<\/a><\/strong><\/span>&#8221; page:<\/p>\n<pre class=\"lang:c++ decode:true\">class AwsOcket {\r\n    boolean exists() { \/* ... *\/ }\r\n    void read(final OutputStream output) { \/* ... *\/ }\r\n    void write(final InputStream input) { \/* ... *\/ }\r\n}<\/pre>\n<p>Now, when we try to blindly implement SRP in this class, we probably have classes like <em>ExistanceChecker<\/em>, <em>ContentReader<\/em> and <em>ContentWriter<\/em>. To read and display data on the Aws class in a simple way, we will need to use code like the following:<\/p>\n<pre class=\"lang:c++ decode:true\">if (new ExistenceChecker(ocket.aws()).exists()) {\r\n  new ContentReader(ocket.aws()).read(System.out);\r\n}<\/pre>\n<p>As you can see, these classes will often be used together. In addition, these classes exhibit high coupling and low cohesion. Therefore, it is not right and feasible to separate these classes in this way.<\/p>\n<p>When using this principle under normal circumstances, you need to assess the cohesion levels of your classes carefully.<\/p>\n<p>With this question, we have completed our first principle. See you next week on principle two, take care of yourself.<\/p>\n<h2><strong><span style=\"color: #0000ff;\">References:<\/span><\/strong><\/h2>\n<ul>\n<li><a href=\"https:\/\/blog.cleancoder.com\/uncle-bob\/2014\/05\/08\/SingleReponsibilityPrinciple.html\"><strong><span style=\"color: #008000;\">https:\/\/blog.cleancoder.com\/uncle-bob\/2014\/05\/08\/SingleReponsibilityPrinciple.html<\/span><\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/Designing-Object-Oriented-Applications-Method\/dp\/0132038374\"><strong><span style=\"color: #008000;\">Designing Object Oriented Applications using UML, 2d. ed., Robert C. Martin, Prentice Hall, 1999.<\/span><\/strong><\/a><\/li>\n<li><a href=\"http:\/\/www.yegor256.com\/2017\/12\/19\/srp-is-hoax.html\"><strong><span style=\"color: #008000;\">http:\/\/www.yegor256.com\/2017\/12\/19\/srp-is-hoax.html<\/span><\/strong><\/a><\/li>\n<li><a href=\"https:\/\/fi.ort.edu.uy\/innovaportal\/file\/2032\/1\/design_principles.pdf\"><strong><span style=\"color: #008000;\">https:\/\/fi.ort.edu.uy\/innovaportal\/file\/2032\/1\/design_principles.pdf<\/span><\/strong><\/a><\/li>\n<li><a href=\"https:\/\/www.wikiwand.com\/en\/Object-oriented_programming\"><strong><span style=\"color: #008000;\">https:\/\/www.wikiwand.com\/en\/Object-oriented_programming<\/span><\/strong><\/a><\/li>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Structured_programming\"><strong><span style=\"color: #008000;\">https:\/\/en.wikipedia.org\/wiki\/Structured_programming<\/span><\/strong><\/a><\/li>\n<\/ul>\n<p>[:]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[:tr]Evet arkada\u015flar, uzunca bir s\u00fcredir, modern C++ ile ilgili yaz\u0131lar payla\u015f\u0131yordum sizler ile ve yaz\u0131l\u0131m m\u00fchendisli\u011fine ili\u015fkin konulara biraz uzak kald\u0131k. Bu yaz\u0131 ile birlikte, biraz daha bu kavramlara e\u011filmeye ba\u015flayaca\u011f\u0131m. Daha \u00f6nce baz\u0131 kavramlara ili\u015fkin yaz\u0131lar\u0131m olmu\u015ftu, in\u015fallah \u00f6n\u00fcm\u00fczdeki d\u00f6nemde bunlar\u0131n s\u0131kl\u0131\u011f\u0131n\u0131 artt\u0131rmay\u0131 planl\u0131yorum. Gelelim bu yaz\u0131m\u0131n (daha do\u011frusu art\u0131k yaz\u0131 dizisi) konusuna. Yaz\u0131m\u0131&#8230; <a class=\"more-link\" href=\"https:\/\/yazilimperver.com\/index.php\/2019\/05\/10\/solid-1-tek-sorumluluk-prensibi\/\">Continue reading <span class=\"meta-nav\">&#8594;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":1162,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","_seopress_robots_follow":"","_seopress_robots_imageindex":"","_seopress_robots_snippet":"","_seopress_robots_primary_cat":"0","_seopress_robots_breadcrumbs":"","_seopress_robots_freeze_modified_date":"","_seopress_robots_custom_modified_date":"","_seopress_robots_canonical":"","_seopress_social_fb_title":"","_seopress_social_fb_desc":"","_seopress_social_fb_img":"","_seopress_social_fb_img_attachment_id":0,"_seopress_social_fb_img_width":0,"_seopress_social_fb_img_height":0,"_seopress_social_twitter_title":"","_seopress_social_twitter_desc":"","_seopress_social_twitter_img":"","_seopress_social_twitter_img_attachment_id":0,"_seopress_social_twitter_img_width":0,"_seopress_social_twitter_img_height":0,"_seopress_redirections_value":"","_seopress_redirections_enabled":"","_seopress_redirections_enabled_regex":"","_seopress_redirections_logged_status":"","_seopress_redirections_param":"","_seopress_redirections_type":0,"_seopress_analysis_target_kw":"","dsgo_overlay_header":false,"dsgo_overlay_header_text_color":"","dsgo_overlay_skip_top_bar":false,"_designsetgo_exclude_llms":false,"footnotes":""},"categories":[188],"tags":[445,446,444,442,441,448,443,447,440,449],"class_list":["post-1148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-yazilim-muhendisligi","tag-cohesion","tag-coupling","tag-object-oriented-programming","tag-ood","tag-oop","tag-robert-c-martin","tag-single-responsiblity-principle","tag-software-design","tag-solid","tag-srp"],"_links":{"self":[{"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/posts\/1148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/comments?post=1148"}],"version-history":[{"count":0,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/posts\/1148\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/media\/1162"}],"wp:attachment":[{"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/media?parent=1148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/categories?post=1148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/tags?post=1148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}