{"id":4107,"date":"2021-01-24T16:17:11","date_gmt":"2021-01-24T16:17:11","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4107"},"modified":"2021-01-25T08:27:43","modified_gmt":"2021-01-25T08:27:43","slug":"practical-uml-wisdom-pearls","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/practical-uml-wisdom-pearls\/","title":{"rendered":"Practical UML Wisdom Pearls"},"content":{"rendered":"<table>\n<thead>\n<th>Relationship<\/th>\n<th>Depiction<\/th>\n<th>Interpretation<\/th>\n<th>Example<\/th>\n<\/thead>\n<tbody>\n<tr>\n<td>Dependency<\/td>\n<td><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2021\/01\/dependency-1.png\" alt=\"\" \/><\/td>\n<td>A depends on B<br \/>\nThis is a very loose relationship and so I rarely use it, but it&#8217;s good to recognize and be able to read it.<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Association<\/td>\n<td><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2021\/01\/association.png\" alt=\"\" \/><\/td>\n<td>An A sends messages to a B<br \/>\nAssociations imply a direct communication path. In programming terms, it means instances of A can call methods of instances of B, for example, if a B is passed to a method of an A.\n<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Aggregation<\/td>\n<td><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2021\/01\/aggregation.png\" alt=\"\" \/><\/td>\n<td>An A is made up of B<br \/>\nThis is a part-to-whole relationship, where A is the whole and B is the part. In code, this essentially implies A has fields of type B.<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>Composition<\/td>\n<td><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2021\/01\/composition.png\" alt=\"\" \/><\/td>\n<td>An A is made up of B with lifetime dependency<br \/>\nThat is, A aggregates B, and if the A is destroyed, its B are destroyed as well.<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Are Dependency Injection and Depencency are Different?<\/strong><br \/>\nYes, They are. The Closest one to DI is aggregation. In Aggregation, we deal with direct objects. i.e. objects which maintain a state, life-cycle, etc.<br \/>\nBut in Dependency Injection, we focus on class level interactions. This is diverged with pure OOP practice. Actually, in DI <strong>we tend to inject stateless classes(worker classes) into some other classes<\/strong>. Though they look like objects they are actually just stateless classes that are being injected. And instances of that class can stand independently too.<\/p>\n<p>Assume a stateless worker class called StringUtils. It can be injected into classes called NounUtils, VerbUtils, etc. And also instances of StringUtils can also exist.<\/p>\n<p><strong>Which one is Close to Dependency(Not DI)<\/strong><br \/>\nAn <strong>association<\/strong> almost always implies that one object has the other object as a field\/property\/attribute (terminology differs).<\/p>\n<p>A <strong>dependency<\/strong> typically (but not always) implies that an object accepts another object as a method parameter, instantiates, or uses another object. A dependency is very much implied by an association.<\/p>\n<pre>\r\nAssociation --> A has-a C object (as a member variable)\r\nDependency --> A references B (as a method parameter or return type)\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class A {\r\n    private C c;\r\n    public void myMethod(B b) {\r\n        b.callMethod();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Simple Example of Dependency, Association, Aggregation and Composition<\/strong><br \/>\n<strong>Dependency (references)<\/strong><br \/>\nIt means there is no conceptual link between two objects. e.g. EnrollmentService object references Student &#038; Course objects (as method parameters or return types)<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class EnrollmentService {\r\n    public void enroll(Student s, Course c){}\r\n}\r\n<\/pre>\n<p><strong>Association (logical relationship)<\/strong> It means there is almost always a link between objects (they are associated). Order object has a Customer object<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Bank {\r\n}\r\n\r\npublic class Employee{\r\n}\r\n\r\nclass Association \r\n{\r\n    public static void main (String&#x5B;] args) \r\n    {\r\n        Bank bank = new Bank(&quot;Axis&quot;);\r\n        Employee emp = new Employee(&quot;Mani&quot;);\r\n           \r\n        System.out.println(emp.getEmployeeName() + &quot; is employee of &quot; + bank.getBankName());\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Aggregation (has-a relationship of weak degree)<\/strong><br \/>\nSpecial kind of association where there is whole-part relation between two objects. they might live without each other though.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class PlayList {\r\n    private List&lt;Song&gt; songs;\r\n}\r\n<\/pre>\n<p>OR<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Computer {\r\n    private Monitor monitor;\r\n}\r\n<\/pre>\n<p>Note: the trickiest part is to distinguish aggregation from the normal association. Honestly, I think this is open to different interpretations.<\/p>\n<p><strong>Composition (has-a relationship of strong degree+ ownership or Part-Of)<\/strong><br \/>\nSpecial kind of aggregation. An Apartment is composed of some Rooms. A Room cannot exist without an Apartment. when an apartment is deleted, all associated rooms are deleted as well.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Apartment{\r\n    private Room bedroom;\r\n    public Apartment() {\r\n       bedroom = new Room();\r\n    }\r\n} \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Relationship Depiction Interpretation Example Dependency A depends on B This is a very loose relationship and so I rarely use it, but it&#8217;s good to recognize and be able to read it. Association An A sends messages to a B Associations imply a direct communication path. In programming terms, it means instances of A can&hellip; <a href=\"https:\/\/codethataint.com\/blog\/practical-uml-wisdom-pearls\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[329],"tags":[],"class_list":["post-4107","post","type-post","status-publish","format-standard","hentry","category-uml"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4107"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4107\/revisions"}],"predecessor-version":[{"id":4127,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4107\/revisions\/4127"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}