{"id":921,"date":"2015-07-06T09:13:47","date_gmt":"2015-07-06T09:13:47","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=921"},"modified":"2015-11-16T16:25:30","modified_gmt":"2015-11-16T16:25:30","slug":"object-orientation-search-screen","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/object-orientation-search-screen\/","title":{"rendered":"Object Orientation Search Screen"},"content":{"rendered":"<p>The Software development can be grouped in to three phase.<br \/>\n1.Meeting Customer Requirement<br \/>\n2.Applying OOAD Principles<br \/>\n3.Design Patterns<\/p>\n<p>We need to create an app which does search of cars in garage.The search is going to take specification of cars as input and display the matching cars.<\/p>\n<p><strong>Phase 1<\/strong>:<\/p>\n<p><em>Car.java<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n public class Car {\r\n\r\n\tprivate String serialNumber;\r\n\tprivate String model;\r\n\tprivate String builder;\r\n\tprivate String price;\r\n\tprivate String type;\t\r\n\r\n\tCar(String pserialNumber, String pmodel, String pbuilder, String pprice, String ptype)\r\n\t{\r\n\t\tserialNumber=pserialNumber;\r\n\t\tmodel=pmodel;\r\n\t\tbuilder=pbuilder;\r\n\t\tprice=pprice;\r\n\t\ttype=ptype;\r\n\t}\r\n\r\n\tpublic String getSerialNumber() {\r\n\t\treturn serialNumber;\r\n\t}\r\n\tpublic void setSerialNumber(String serialNumber) {\r\n\t\tthis.serialNumber = serialNumber;\r\n\t}\r\n\tpublic String getModel() {\r\n\t\treturn model;\r\n\t}\r\n\tpublic void setModel(String model) {\r\n\t\tthis.model = model;\r\n\t}\r\n\tpublic String getBuilder() {\r\n\t\treturn builder;\r\n\t}\r\n\tpublic void setBuilder(String builder) {\r\n\t\tthis.builder = builder;\r\n\t}\r\n\tpublic String getPrice() {\r\n\t\treturn price;\r\n\t}\r\n\tpublic void setPrice(String price) {\r\n\t\tthis.price = price;\r\n\t}\r\n\tpublic String getType() {\r\n\t\treturn type;\r\n\t}\r\n\tpublic void setType(String type) {\r\n\t\tthis.type = type;\r\n\t}\r\n\r\n\tpublic enum Type {\r\n\t\tPETROL, DIESEL;\r\n\r\n\t\tpublic String toString() {\r\n\r\n\t\t\tswitch (this) {\r\n\t\t\tcase PETROL:\r\n\t\t\t\treturn &quot;petrol&quot;;\r\n\r\n\t\t\tcase DIESEL:\r\n\t\t\t\treturn &quot;diesel&quot;;\r\n\r\n\t\t\t}\r\n\t\t\treturn null;\r\n\t\t}\r\n\t}\r\n\r\n\tpublic enum Builder {\r\n\t\tFORD, HONDA, TOYOTA;\r\n\r\n\t\tpublic String toString() {\r\n\t\t\tswitch (this) {\r\n\t\t\tcase FORD:\r\n\t\t\t\treturn &quot;ford&quot;;\r\n\r\n\t\t\tcase HONDA:\r\n\t\t\t\treturn &quot;honda&quot;;\r\n\r\n\t\t\tcase TOYOTA:\r\n\t\t\t\treturn &quot;toyota&quot;;\r\n\r\n\t\t\t}\r\n\r\n\t\t\treturn null;\r\n\t\t}\r\n\r\n\t}\r\n}\r\n<\/pre>\n<p><em>Garage.java<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.ArrayList;\r\nimport java.util.Iterator;\r\nimport java.util.List;\r\n\r\npublic class Garage {\r\n\tprivate List&lt;Car&gt; carList = new ArrayList&lt;Car&gt;();\r\n\r\n\tpublic void addCar(String pserialNumber, String pmodel, String pbuilder, String pprice, String ptype)\r\n\t{\r\n\t\tCar objCar = new Car(pserialNumber, pmodel, pbuilder, pprice, ptype);\r\n\t\tcarList.add(objCar);\r\n\t}\r\n\r\n\t\/\/The guitar will be returned only when all Search Criteria Match\r\n\tpublic List&lt;Car&gt; searchCar(Car searchCar)\r\n\t{\r\n\t\tList&lt;Car&gt; resultCarList = new ArrayList&lt;Car&gt;(); \r\n\r\n\t\tfor (Iterator iterator = carList.iterator(); iterator.hasNext();) {\r\n\t\t\tCar Car = (Car) iterator.next();\r\n\r\n\t\t\tif(!searchCar.getType().equals(Car.getType()))\r\n\t\t\t\tcontinue;\r\n\r\n\t\t\tif(!searchCar.getBuilder().equals(Car.getBuilder()))\r\n\t\t\t\tcontinue;\r\n\r\n\t\t\tresultCarList.add(Car);\r\n\t\t}\r\n\r\n\t\treturn resultCarList;\r\n\t}\r\n\r\n\tpublic Car getCar(String SerialNo)\r\n\t{\r\n\t\treturn null;\r\n\t}\r\n}\r\n<\/pre>\n<p><em>SearchCar.java<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.IOException;\r\nimport java.util.Iterator;\r\nimport java.util.List;\r\n\r\npublic class SearchCar {\r\n\tpublic static void main(String&#x5B;] args) throws IOException {\r\n\t\tGarage objGarage = new Garage();\r\n\t\tinitializeCar(objGarage);\r\n\r\n\t\tCar searchCar = new Car(&quot;&quot;, &quot;A1&quot;, Car.Builder.FORD.toString(), &quot;&quot;,\r\n\t\t\t\tCar.Type.PETROL.toString());\r\n        List&lt;Car&gt; searchResult  =  objGarage.searchCar(searchCar);\r\n\r\n        if(searchResult != null)\r\n        {\r\n        \tSystem.out.println(&quot;Search Result&quot;);\r\n\r\n        \tSystem.out.println(&quot;-------------------------------&quot;);\r\n\r\n\t\t\tfor (Iterator iterator = searchResult.iterator(); iterator.hasNext();) {\r\n\r\n\t\t\t\tCar Car = (Car) iterator.next();\r\n\r\n\t        \tSystem.out.println(&quot;Model : &quot;+ Car.getModel());\r\n\t        \tSystem.out.println(&quot;Builder : &quot;+ Car.getBuilder());\r\n\t        \tSystem.out.println(&quot;Type : &quot;+ Car.getType());\r\n\t        \tSystem.out.println(&quot;-------------------------------&quot;);\r\n\t\t\t}\r\n        }else\r\n        {\r\n        \tSystem.out.println(&quot;Sorry! We are unable to Find Car...&quot;);\r\n        }\r\n\t}\r\n\r\n\tpublic static void initializeCar(Garage pobjGarage) {\r\n\t\t\/\/pserialNumber, pmodel, pbuilder, pprice, ptype\r\n\t\tpobjGarage.addCar(&quot;&quot;, &quot;Mustang&quot;, Car.Builder.FORD.toString(), &quot;&quot;, Car.Type.PETROL.toString());\r\n\t\tpobjGarage.addCar(&quot;&quot;, &quot;Corolla&quot;, Car.Builder.TOYOTA.toString(), &quot;&quot;, Car.Type.DIESEL.toString());\r\n\t\tpobjGarage.addCar(&quot;&quot;, &quot;Endevadour&quot;, Car.Builder.FORD.toString(), &quot;&quot;, Car.Type.PETROL.toString());\r\n\t\tpobjGarage.addCar(&quot;&quot;, &quot;Civic&quot;, Car.Builder.HONDA.toString(), &quot;&quot;, Car.Type.PETROL.toString());\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>Points to Note<\/strong>:<\/p>\n<ol>\n<li>When specifications contains less number of search criteria like Fuel Type which can be either petrol or diesel use ENUM.By doing this we are avoiding String comparison and other overheads like converting to lowercase, uppercase before comparison<\/li>\n<li>The searchCar method in Garage will return Cars only when all specs matches the car in the garage.<\/li>\n<\/ol>\n<p><strong>Phase 2<\/strong><br \/>\n<strong>Object Orientation<\/strong><\/p>\n<ol>\n<li>Objects should do what their name Indicate<\/li>\n<li>Each Object should represent a Single Concept<\/li>\n<li>Unused properties are dead give away<\/li>\n<\/ol>\n<p>In the above code the Search criteria used can be split separately.<br \/>\nThis includes Type, Model and Builder.<br \/>\nThe Price and Serial No are not moved to new class since they are unique.<\/p>\n<p><em>CarSpec.java<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class CarSpec {\r\n\r\n\tprivate String model;\r\n\tprivate String builder;\r\n\tprivate String type;\r\n\r\n\tpublic CarSpec(String pbuilder, String pmodel,String ptype){\r\n\t\tmodel=pmodel;\r\n\t\tbuilder=pbuilder;\r\n\t\ttype=ptype;\r\n\t}\r\n\r\n\tpublic String getModel() {\r\n\t\treturn model;\r\n\t}\r\n\tpublic void setModel(String model) {\r\n\t\tthis.model = model;\r\n\t}\r\n\tpublic String getBuilder() {\r\n\t\treturn builder;\r\n\t}\r\n\tpublic void setBuilder(String builder) {\r\n\t\tthis.builder = builder;\r\n\t}\r\n\r\n\tpublic String getType() {\r\n\t\treturn type;\r\n\t}\r\n\tpublic void setType(String type) {\r\n\t\tthis.type = type;\r\n\t}\r\n}\r\n<\/pre>\n<p>Now the car class is going to be replaced with CarSpec variable as property.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\npublic class Car {\r\n\r\n\tprivate String serialNumber;\r\n\tprivate String price;\r\n\tprivate CarSpec carSpecification;\r\n\r\n\tCar(String pserialNumber, String pprice, CarSpec pcarSpec)\r\n\t{\r\n\t\tserialNumber=pserialNumber;\r\n\t\tprice=pprice;\r\n\t\tcarSpecification = pcarSpec;\r\n\t}\r\n\r\n\tpublic String getSerialNumber() {\r\n\t\treturn serialNumber;\r\n\t}\r\n\tpublic void setSerialNumber(String serialNumber) {\r\n\t\tthis.serialNumber = serialNumber;\r\n\t}\r\n\r\n\tpublic String getPrice() {\r\n\t\treturn price;\r\n\t}\r\n\tpublic void setPrice(String price) {\r\n\t\tthis.price = price;\r\n\t}\r\n\r\n\tpublic CarSpec getCarSpecification() {\r\n\t\treturn carSpecification;\r\n\t}\r\n\r\n\tpublic void setCarSpecification(CarSpec carSpecification) {\r\n\t\tthis.carSpecification = carSpecification;\r\n\t}\r\n\r\n\tpublic CarSpec getCarSpec(){\r\n\t\treturn carSpecification;\r\n\t}\r\n}\r\n<\/pre>\n<p>Since the Specification of the car are moved separately they can be used for both searching and storing car details.<\/p>\n<p>Now the searchCar() method in Garage.java should be allowed to take car Specification as argument instead of whole car object which contains redundant Price and Serial No which are unique.<\/p>\n<p><em>Garage.java<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n import java.util.ArrayList;\r\nimport java.util.Iterator;\r\nimport java.util.List;\r\n\r\npublic class Garage {\r\n\tprivate List&lt;Car&gt; carList = new ArrayList&lt;Car&gt;();\r\n\r\n\tpublic void addCar(String pserialNumber, String pprice, CarSpec pcarSpec)\r\n\t{\r\n\t\tCar objCar = new Car(pserialNumber, pprice, pcarSpec);\r\n\t\tcarList.add(objCar);\r\n\t}\r\n\r\n\t\/\/The guitar will be returned only when all Search Criteria Match\r\n\tpublic List&lt;Car&gt; searchCar(CarSpec searchCarSpec)\r\n\t{\r\n\t\tList&lt;Car&gt; resultCarList = new ArrayList&lt;Car&gt;(); \r\n\r\n\t\tfor (Iterator iterator = carList.iterator(); iterator.hasNext();) {\r\n\t\t\tCar objCar = (Car) iterator.next();\r\n\r\n\t\t\tCarSpec objCarSpec = objCar.getCarSpec();\r\n\r\n\t\t\tif(!objCarSpec.getBuilder().equals(searchCarSpec.getBuilder()))\r\n\t\t\t continue;\r\n\r\n\t\t\tif(!objCarSpec.getType().equals(searchCarSpec.getType()))\r\n\t\t\t  continue;\r\n\r\n\t\t\tresultCarList.add(objCar);\r\n\t\t}\r\n\r\n\t\treturn resultCarList;\r\n\t}\r\n}\r\n<\/pre>\n<p><em>Searchcar.java<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class SearchCar {\r\n\tpublic static void main(String&#x5B;] args) throws IOException {\r\n\t\tGarage objGarage = new Garage();\r\n\t\tinitializeCar(objGarage);\r\n\r\n\t\tCarSpec searchCar = new CarSpec(Builder.FORD.toString(), &quot;&quot;, Type.PETROL.toString());\r\n        List&lt;Car&gt; searchResult  =  objGarage.searchCar(searchCar);\r\n\r\n        if(searchResult != null)\r\n        {\r\n        \tSystem.out.println(&quot;Search Result&quot;);\r\n\r\n        \tSystem.out.println(&quot;-------------------------------&quot;);\r\n\r\n\t\t\tfor (Iterator iterator = searchResult.iterator(); iterator.hasNext();) {\r\n\r\n\t\t\t\tCar objCar = (Car) iterator.next();\r\n\t\t\t\tCarSpec objSpec = objCar.getCarSpec();\r\n\r\n\t\t\t\tSystem.out.println(&quot;Car Name : &quot;+ objCar.getSerialNumber());\r\n\t\t\t\tSystem.out.println(&quot;Car Name : &quot;+ objCar.getPrice());\r\n\t        \tSystem.out.println(&quot;Model : &quot;+ objSpec.getModel());\r\n\t        \tSystem.out.println(&quot;Builder : &quot;+ objSpec.getBuilder());\r\n\t        \tSystem.out.println(&quot;Type : &quot;+ objSpec.getType());\r\n\t        \tSystem.out.println(&quot;-------------------------------&quot;);\r\n\t\t\t}\r\n        }else\r\n        {\r\n        \tSystem.out.println(&quot;Sorry! We are unable to Find Car...&quot;);\r\n        }\r\n\t}\r\n\r\n\tpublic static void initializeCar(Garage pobjGarage) {\r\n\t\t\/\/pserialNumber, pmodel, pbuilder, pprice, ptype\r\n\t\tpobjGarage.addCar(&quot;101&quot;, &quot;Mustang&quot;, new CarSpec(Builder.FORD.toString(), &quot;3200&quot;, Type.PETROL.toString()));\r\n\t\tpobjGarage.addCar(&quot;201&quot;, &quot;Corolla&quot;, new CarSpec(Builder.TOYOTA.toString(), &quot;3500&quot;, Type.DIESEL.toString()));\r\n\t\tpobjGarage.addCar(&quot;102&quot;, &quot;Endevadour&quot;, new CarSpec(Builder.FORD.toString(), &quot;5200&quot;, Type.PETROL.toString()));\r\n\t\tpobjGarage.addCar(&quot;301&quot;, &quot;Civic&quot;, new CarSpec(Builder.HONDA.toString(), &quot;3000&quot;, Type.PETROL.toString()));\r\n\t}\r\n}\r\n<\/pre>\n<p>Now we are going to move the Search Car Code from <strong>Garage.java<\/strong> to <strong>CarSpec.java<\/strong>.By doing this we are delegating the job to highly coherent class.<br \/>\nIn case any code to be added in future the changes to be done are only confined within this class.<strong>We are also going to perform object to object comparison.<\/strong><\/p>\n<p><strong>Garage.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\n\r\npublic List&lt;Car&gt; searchCar(CarSpec searchCarSpec)\r\n\t{\r\n\t\tList&lt;Car&gt; resultCarList = new ArrayList&lt;Car&gt;(); \r\n\r\n\t\tfor (Iterator iterator = carList.iterator(); iterator.hasNext();) {\r\n\t\t\tCar objCar = (Car) iterator.next();\r\n\r\n\t\t\tCarSpec objCarSpec = objCar.getCarSpec();\r\n\r\n\t\t\tif(objCarSpec.findMatchingCar(searchCarSpec))\r\n\t\t\t resultCarList.add(objCar);\r\n\t\t}\r\n\r\n\t\treturn resultCarList;\r\n\t}\r\n\r\n.\r\n.\r\n.\r\n\r\n<\/pre>\n<p>we are calling findMatchingCar method of CarSpec.java instead of doing the comparison in the same class.<\/p>\n<p><strong>CarSpec.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n.\r\npublic boolean findMatchingCar(CarSpec objCarsPEC)\r\n\t{\r\n\t\tif(builder !=  objCarsPEC.getBuilder())\r\n\t\t return false;\r\n\r\n\t\tif(type !=  objCarsPEC.getType())\r\n\t\t return false;\r\n\r\n\t\treturn true;\r\n\t}\r\n.\r\n.\r\n.\r\n\r\n<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p><strong>Things Learned<\/strong><\/p>\n<ol>\n<li>Objects should do what their name indicate and should represent a single concept(high cohesion).<br \/>\nBy Moving the specifications on which the search is made into new class CarSpec.java instead of keeping them in Car.java we have achieved high cohesion.\n<\/li>\n<li>\n By Delegating the search functionality to class CarSpec.java we have achieved flexibility. In case there<br \/>\nis new property say four wheel drive or power window added as search criteria the only class file to be changed is CarSpec.java\n<\/li>\n<li>\n High cohesion achieved is also evident from the fact that we can do object to object comparing to match the search criteria. <strong>findMatchingCar() Method<\/strong> is doing the same.\n<\/li>\n<li>If object is used with null value or no value you are using object for more than one job.Simple example is doing a null check on a object property which is not a good business code implementation<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>The Software development can be grouped in to three phase. 1.Meeting Customer Requirement 2.Applying OOAD Principles 3.Design Patterns We need to create an app which does search of cars in garage.The search is going to take specification of cars as input and display the matching cars. Phase 1: Car.java public class Car { private String&hellip; <a href=\"https:\/\/codethataint.com\/blog\/object-orientation-search-screen\/\">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":[169],"tags":[],"class_list":["post-921","post","type-post","status-publish","format-standard","hentry","category-ooad"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/921","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=921"}],"version-history":[{"count":11,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/921\/revisions"}],"predecessor-version":[{"id":934,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/921\/revisions\/934"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}