{"id":772,"date":"2022-02-07T08:45:39","date_gmt":"2022-02-07T07:45:39","guid":{"rendered":"https:\/\/wojciechsiwek.pl\/?p=772"},"modified":"2025-02-02T11:13:08","modified_gmt":"2025-02-02T10:13:08","slug":"single-responsibility-principle","status":"publish","type":"post","link":"https:\/\/wojciechsiwek.pl\/en\/single-responsibility-principle\/","title":{"rendered":"Single Responsibility Principle"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_86 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Spis Tre\u015bci<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a8d0cf58621a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a8d0cf58621a\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/wojciechsiwek.pl\/en\/single-responsibility-principle\/#Troche_teorii\" >Some theory<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/wojciechsiwek.pl\/en\/single-responsibility-principle\/#Przykladowy_kod\" >Sample code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/wojciechsiwek.pl\/en\/single-responsibility-principle\/#Podsumowanie\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"troche-teorii\"><span class=\"ez-toc-section\" id=\"Troche_teorii\"><\/span>Some theory<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The first of the principles behind the letter S, abbreviated as SOLID, is the title Single Responsibility Principle. Its definition can be presented as follows:<\/p>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p>There shouldn't be more than one reason for a class or method to exist<\/p><\/blockquote><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"przykladowy-kod\"><span class=\"ez-toc-section\" id=\"Przykladowy_kod\"><\/span>Sample code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Since this definition is simple enough, I will now show how easy it is to break this rule, and how it should be avoided in a specific situation. An example is the Report class, which handles functions related to reporting some data from the system.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-java\" data-line=\"\">public class Report {\n    private String reportData1, reportData2;\n    private String completeReport;\n\n    public Report(String reportData1, String reportData2) {\n        this.reportData1 = reportData1;\n        this.reportData2 = reportData2;\n    }\n\n    public void generateReport(){\n        completeReport = reportData1 + reportData2;\n    }\n    public void printReport(){\n        System.out.println(completeReport);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that this class aims to combine two fields with data in order to create a report and print it. And here comes the crux of the problem, i.e. breaking the rule. In addition to generating the report, the class is also responsible for printing it. In order for the implementation to comply with the principle of one responsibility, the code should look like this:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-8f761849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-java\" data-line=\"\">public class Report {\n    private String reportData1, reportData2;\n    private String completeReport;\n\n    public Report(String reportData1, String reportData2) {\n        this.reportData1 = reportData1;\n        this.reportData2 = reportData2;\n    }\n\n    public String getCompleteReport() {\n        return completeReport;\n    }\n\n    public void generateReport(){\n        completeReport = reportData1 + reportData2;\n    }\n}\n<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-java\" data-line=\"\">public class Printer {\n    public String textToPrint;\n\n    public Printer(String textToPrint) {\n        this.textToPrint = textToPrint;\n    }\n\n    public void printReport() {\n        System.out.println(textToPrint);\n    }\n}\n<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">We have separated a new Printer class from the Report class, the principle of which is to print the report only. Remember that writing text to the console is just an example. The actual implementation of printing would certainly be more complicated.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"podsumowanie\"><span class=\"ez-toc-section\" id=\"Podsumowanie\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Applying the single responsibility principle seems quite simple and obvious, but in practice we often think that we will save time by combining different responsibilities into one class or method. However, after some time it may turn out that the class functionality should be used elsewhere. Then we will have to refactor the class and extract the selected functionality from it.<\/p>","protected":false},"excerpt":{"rendered":"<p>Briefly about the first of the SOLID principles, i.e. single responsibility principle, with an example of the code in Java.<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","_themeisle_gutenberg_block_has_review":false,"footnotes":""},"categories":[26],"tags":[25,23,77],"class_list":["post-772","post","type-post","status-publish","format-standard","hentry","category-programowanie","tag-java","tag-programowanie","tag-solid"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Single Responsibility Principle &#8211; Wojciech Siwek<\/title>\n<meta name=\"description\" content=\"Kr\u00f3tko o pierwszej z zasad SOLID czyli single responsibility principle wraz z przyk\u0142adem kodu w j\u0119zyku Java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wojciechsiwek.pl\/en\/single-responsibility-principle\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Single Responsibility Principle &#8211; Wojciech Siwek\" \/>\n<meta property=\"og:description\" content=\"Kr\u00f3tko o pierwszej z zasad SOLID czyli single responsibility principle wraz z przyk\u0142adem kodu w j\u0119zyku Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wojciechsiwek.pl\/en\/single-responsibility-principle\/\" \/>\n<meta property=\"og:site_name\" content=\"Wojciech Siwek\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-07T07:45:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-02T10:13:08+00:00\" \/>\n<meta name=\"author\" content=\"Wojciech Siwek\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Wojciech Siwek\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/single-responsibility-principle\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/single-responsibility-principle\\\/\"},\"author\":{\"name\":\"Wojciech Siwek\",\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#\\\/schema\\\/person\\\/eb4246ee83fb20ed02fc723e28258677\"},\"headline\":\"Single Responsibility Principle\",\"datePublished\":\"2022-02-07T07:45:39+00:00\",\"dateModified\":\"2025-02-02T10:13:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/single-responsibility-principle\\\/\"},\"wordCount\":257,\"publisher\":{\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#\\\/schema\\\/person\\\/eb4246ee83fb20ed02fc723e28258677\"},\"keywords\":[\"java\",\"programowanie\",\"SOLID\"],\"articleSection\":[\"Programowanie\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/single-responsibility-principle\\\/\",\"url\":\"https:\\\/\\\/wojciechsiwek.pl\\\/single-responsibility-principle\\\/\",\"name\":\"Single Responsibility Principle &#8211; Wojciech Siwek\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#website\"},\"datePublished\":\"2022-02-07T07:45:39+00:00\",\"dateModified\":\"2025-02-02T10:13:08+00:00\",\"description\":\"Kr\u00f3tko o pierwszej z zasad SOLID czyli single responsibility principle wraz z przyk\u0142adem kodu w j\u0119zyku Java.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/single-responsibility-principle\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wojciechsiwek.pl\\\/single-responsibility-principle\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/single-responsibility-principle\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Strona g\u0142\u00f3wna\",\"item\":\"https:\\\/\\\/wojciechsiwek.pl\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Single Responsibility Principle\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#website\",\"url\":\"http:\\\/\\\/wojciechsiwek.pl\\\/\",\"name\":\"Wojciech Siwek\",\"description\":\"Programista na pocz\u0105tku swojej \u015bcie\u017cki w IT\",\"publisher\":{\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#\\\/schema\\\/person\\\/eb4246ee83fb20ed02fc723e28258677\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\\\/\\\/wojciechsiwek.pl\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\\\/\\\/wojciechsiwek.pl\\\/#\\\/schema\\\/person\\\/eb4246ee83fb20ed02fc723e28258677\",\"name\":\"Wojciech Siwek\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/cropped-logo.png\",\"url\":\"https:\\\/\\\/wojciechsiwek.pl\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/cropped-logo.png\",\"contentUrl\":\"https:\\\/\\\/wojciechsiwek.pl\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/cropped-logo.png\",\"width\":313,\"height\":306,\"caption\":\"Wojciech Siwek\"},\"logo\":{\"@id\":\"https:\\\/\\\/wojciechsiwek.pl\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/cropped-logo.png\"},\"sameAs\":[\"https:\\\/\\\/wojciechsiwek.pl\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Single Responsibility Principle &#8211; Wojciech Siwek","description":"Briefly about the first of the SOLID principles, i.e. single responsibility principle, with an example of the code in Java.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wojciechsiwek.pl\/en\/single-responsibility-principle\/","og_locale":"en_GB","og_type":"article","og_title":"Single Responsibility Principle &#8211; Wojciech Siwek","og_description":"Kr\u00f3tko o pierwszej z zasad SOLID czyli single responsibility principle wraz z przyk\u0142adem kodu w j\u0119zyku Java.","og_url":"https:\/\/wojciechsiwek.pl\/en\/single-responsibility-principle\/","og_site_name":"Wojciech Siwek","article_published_time":"2022-02-07T07:45:39+00:00","article_modified_time":"2025-02-02T10:13:08+00:00","author":"Wojciech Siwek","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Wojciech Siwek","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wojciechsiwek.pl\/single-responsibility-principle\/#article","isPartOf":{"@id":"https:\/\/wojciechsiwek.pl\/single-responsibility-principle\/"},"author":{"name":"Wojciech Siwek","@id":"http:\/\/wojciechsiwek.pl\/#\/schema\/person\/eb4246ee83fb20ed02fc723e28258677"},"headline":"Single Responsibility Principle","datePublished":"2022-02-07T07:45:39+00:00","dateModified":"2025-02-02T10:13:08+00:00","mainEntityOfPage":{"@id":"https:\/\/wojciechsiwek.pl\/single-responsibility-principle\/"},"wordCount":257,"publisher":{"@id":"http:\/\/wojciechsiwek.pl\/#\/schema\/person\/eb4246ee83fb20ed02fc723e28258677"},"keywords":["java","programowanie","SOLID"],"articleSection":["Programowanie"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/wojciechsiwek.pl\/single-responsibility-principle\/","url":"https:\/\/wojciechsiwek.pl\/single-responsibility-principle\/","name":"Single Responsibility Principle &#8211; Wojciech Siwek","isPartOf":{"@id":"http:\/\/wojciechsiwek.pl\/#website"},"datePublished":"2022-02-07T07:45:39+00:00","dateModified":"2025-02-02T10:13:08+00:00","description":"Briefly about the first of the SOLID principles, i.e. single responsibility principle, with an example of the code in Java.","breadcrumb":{"@id":"https:\/\/wojciechsiwek.pl\/single-responsibility-principle\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wojciechsiwek.pl\/single-responsibility-principle\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/wojciechsiwek.pl\/single-responsibility-principle\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Strona g\u0142\u00f3wna","item":"https:\/\/wojciechsiwek.pl\/"},{"@type":"ListItem","position":2,"name":"Single Responsibility Principle"}]},{"@type":"WebSite","@id":"http:\/\/wojciechsiwek.pl\/#website","url":"http:\/\/wojciechsiwek.pl\/","name":"Wojciech Siwek","description":"Software developer at the beginning of his career in IT","publisher":{"@id":"http:\/\/wojciechsiwek.pl\/#\/schema\/person\/eb4246ee83fb20ed02fc723e28258677"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/wojciechsiwek.pl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":["Person","Organization"],"@id":"http:\/\/wojciechsiwek.pl\/#\/schema\/person\/eb4246ee83fb20ed02fc723e28258677","name":"Wojciech Siwek","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/wojciechsiwek.pl\/wp-content\/uploads\/2021\/03\/cropped-logo.png","url":"https:\/\/wojciechsiwek.pl\/wp-content\/uploads\/2021\/03\/cropped-logo.png","contentUrl":"https:\/\/wojciechsiwek.pl\/wp-content\/uploads\/2021\/03\/cropped-logo.png","width":313,"height":306,"caption":"Wojciech Siwek"},"logo":{"@id":"https:\/\/wojciechsiwek.pl\/wp-content\/uploads\/2021\/03\/cropped-logo.png"},"sameAs":["https:\/\/wojciechsiwek.pl"]}]}},"_links":{"self":[{"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/posts\/772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/comments?post=772"}],"version-history":[{"count":1,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/posts\/772\/revisions"}],"predecessor-version":[{"id":773,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/posts\/772\/revisions\/773"}],"wp:attachment":[{"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/media?parent=772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/categories?post=772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wojciechsiwek.pl\/en\/wp-json\/wp\/v2\/tags?post=772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}