From 0cb0577083344cabbd7ea415396133398c1a7ec6 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Sun, 24 Dec 2017 00:51:28 -0600 Subject: [PATCH 01/11] updated gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6143e53..079beb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Compiled class file +.idea/* *.class - +*.iml # Log file *.log From cd5f5942e1a06360d10ce07fdbc950c0f5c9d1f7 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Sun, 24 Dec 2017 01:07:57 -0600 Subject: [PATCH 02/11] entered empty module.info --- inheritance/src/module-info.java | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 inheritance/src/module-info.java diff --git a/inheritance/src/module-info.java b/inheritance/src/module-info.java new file mode 100644 index 0000000..de8e3dd --- /dev/null +++ b/inheritance/src/module-info.java @@ -0,0 +1,3 @@ +module inheritance { + +} \ No newline at end of file From a0fbd97418c81f8d2e6a227b20aad828ab00283f Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Mon, 25 Dec 2017 13:34:15 -0600 Subject: [PATCH 03/11] cleaned up master --- inheritance/src/module-info.java | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 inheritance/src/module-info.java diff --git a/inheritance/src/module-info.java b/inheritance/src/module-info.java deleted file mode 100644 index de8e3dd..0000000 --- a/inheritance/src/module-info.java +++ /dev/null @@ -1,3 +0,0 @@ -module inheritance { - -} \ No newline at end of file From 9472a4bc7ec594c90ed33d64b32eb52750e56394 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 3 Jan 2018 00:48:15 -0600 Subject: [PATCH 04/11] added modules files --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 079beb8..719dd1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,10 @@ # Compiled class file .idea/* *.class -*.iml + # Log file *.log - +*.iws # BlueJ files *.ctxt From 1abc5156ce28f907c7cfdeedb9e18c1a12181862 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Fri, 5 Jan 2018 07:00:17 -0600 Subject: [PATCH 05/11] Concept of complexity explained --- .classpath | 6 ++++ .project | 15 ++++++++++ DesignPatternsJava9.eml | 6 ++++ src/CyclomatricComplexity.java | 53 ++++++++++++++++++++++++++++++++++ src/CyclomatricSimplicity.java | 35 ++++++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 DesignPatternsJava9.eml create mode 100644 src/CyclomatricComplexity.java create mode 100644 src/CyclomatricSimplicity.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f06d999 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..a33e785 --- /dev/null +++ b/.project @@ -0,0 +1,15 @@ + + + DesignPatternsJava9 + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.jdt.core.javanature + + diff --git a/DesignPatternsJava9.eml b/DesignPatternsJava9.eml new file mode 100644 index 0000000..82e1875 --- /dev/null +++ b/DesignPatternsJava9.eml @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/CyclomatricComplexity.java b/src/CyclomatricComplexity.java new file mode 100644 index 0000000..861c192 --- /dev/null +++ b/src/CyclomatricComplexity.java @@ -0,0 +1,53 @@ +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ +public class CyclomatricComplexity { + + /* + Problem Statement: Need to print biggest number from given numbers + */ + + public static void main (String[] args) { + + Integer a=7,b=4,c=10,d=18; + + if (a > b) { + if(a > c){ + if (a >d){ + System.out.println(a+ " is biggest"); + } + } + } + + if (b > c) { + if(b > d){ + if (b >a){ + System.out.println(b+ " is biggest"); + } + } + } + + if (c > b) { + if(c > a){ + if (c >d){ + System.out.println(c+" is biggest"); + } + } + } + + if (d > b) { + if(d > c){ + if (d >a){ + System.out.println(d+" is biggest"); + } + } + } + } +} + +// Take Away: +// If the decision points are more, then complexity of the program is more. +// If program has high complexity number, then probability of error is high +// with increased time for maintenance and trouble shoot. \ No newline at end of file diff --git a/src/CyclomatricSimplicity.java b/src/CyclomatricSimplicity.java new file mode 100644 index 0000000..f5b659c --- /dev/null +++ b/src/CyclomatricSimplicity.java @@ -0,0 +1,35 @@ +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ +public class CyclomatricSimplicity { + + /* + Problem Statement: Need to print biggest number from given numbers + */ + + public static void main (String[] args) { + + Integer a=7,b=4,c=10,d=18; + // Event when we need to modify parameter, things can be done + // with minimal code changes + + PrintBiggestNumber(a,b,c,d); + } + + private static void PrintBiggestNumber (Integer... numbers) { + System.out.println(); + List list = Arrays.asList(numbers); + Comparable max = Collections.max(list); + System.out.println(max + " is biggest (KISS)"); + } +} + +// Take Away: +// KISS - Keep it simple stupid. +// Design clean dry less code which can absorb changes. From 0a20831bd74e3c879f8c66bbb76d27d493448bb0 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Fri, 5 Jan 2018 07:01:44 -0600 Subject: [PATCH 06/11] Concept of complexity explained --- DesignPatternsJava9.iml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 DesignPatternsJava9.iml diff --git a/DesignPatternsJava9.iml b/DesignPatternsJava9.iml new file mode 100644 index 0000000..5a95221 --- /dev/null +++ b/DesignPatternsJava9.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file From b643cbeb8a189878e9b3127456af4a4e00045f62 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Tue, 9 Jan 2018 12:02:24 -0600 Subject: [PATCH 07/11] removed non required classes --- src/CyclomatricComplexity.java | 53 ---------------------------------- src/CyclomatricSimplicity.java | 35 ---------------------- 2 files changed, 88 deletions(-) delete mode 100644 src/CyclomatricComplexity.java delete mode 100644 src/CyclomatricSimplicity.java diff --git a/src/CyclomatricComplexity.java b/src/CyclomatricComplexity.java deleted file mode 100644 index 861c192..0000000 --- a/src/CyclomatricComplexity.java +++ /dev/null @@ -1,53 +0,0 @@ -/* -@author: Aseem Jain -@title: Design Patterns with Java 9 -@link: https://premaseem.wordpress.com/category/computers/design-patterns/ -*/ -public class CyclomatricComplexity { - - /* - Problem Statement: Need to print biggest number from given numbers - */ - - public static void main (String[] args) { - - Integer a=7,b=4,c=10,d=18; - - if (a > b) { - if(a > c){ - if (a >d){ - System.out.println(a+ " is biggest"); - } - } - } - - if (b > c) { - if(b > d){ - if (b >a){ - System.out.println(b+ " is biggest"); - } - } - } - - if (c > b) { - if(c > a){ - if (c >d){ - System.out.println(c+" is biggest"); - } - } - } - - if (d > b) { - if(d > c){ - if (d >a){ - System.out.println(d+" is biggest"); - } - } - } - } -} - -// Take Away: -// If the decision points are more, then complexity of the program is more. -// If program has high complexity number, then probability of error is high -// with increased time for maintenance and trouble shoot. \ No newline at end of file diff --git a/src/CyclomatricSimplicity.java b/src/CyclomatricSimplicity.java deleted file mode 100644 index f5b659c..0000000 --- a/src/CyclomatricSimplicity.java +++ /dev/null @@ -1,35 +0,0 @@ -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -/* -@author: Aseem Jain -@title: Design Patterns with Java 9 -@link: https://premaseem.wordpress.com/category/computers/design-patterns/ -*/ -public class CyclomatricSimplicity { - - /* - Problem Statement: Need to print biggest number from given numbers - */ - - public static void main (String[] args) { - - Integer a=7,b=4,c=10,d=18; - // Event when we need to modify parameter, things can be done - // with minimal code changes - - PrintBiggestNumber(a,b,c,d); - } - - private static void PrintBiggestNumber (Integer... numbers) { - System.out.println(); - List list = Arrays.asList(numbers); - Comparable max = Collections.max(list); - System.out.println(max + " is biggest (KISS)"); - } -} - -// Take Away: -// KISS - Keep it simple stupid. -// Design clean dry less code which can absorb changes. From 45fc3392b37e183703eccb7e9f603593a737252c Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Tue, 9 Jan 2018 12:03:05 -0600 Subject: [PATCH 08/11] step 1: added calculator class --- src/com/premaseem/Calculator.java | 13 +++++++++++++ src/com/premaseem/Client.java | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/com/premaseem/Calculator.java create mode 100644 src/com/premaseem/Client.java diff --git a/src/com/premaseem/Calculator.java b/src/com/premaseem/Calculator.java new file mode 100644 index 0000000..63397ce --- /dev/null +++ b/src/com/premaseem/Calculator.java @@ -0,0 +1,13 @@ +package com.premaseem; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +*/ +public class Calculator { + + public Integer add(Integer sum, Integer number){ + return sum + number; + } +} diff --git a/src/com/premaseem/Client.java b/src/com/premaseem/Client.java new file mode 100644 index 0000000..cc0b9e5 --- /dev/null +++ b/src/com/premaseem/Client.java @@ -0,0 +1,24 @@ +package com.premaseem; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Client { + public static void main (String[] args) { + System.out.println("program to add number"); + Integer sum = calculateSum(); + System.out.println("total of all numbers is "+ sum); + } + + private static Integer calculateSum () { + Integer sum = 0; + for (int i =0; i< 1000; i++){ + Calculator calc = new Calculator(); + sum = calc.add(sum,i); + } + return sum; + } +} From 25f460bf2415b75a402c11ec7e0c56519410ac4b Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Tue, 9 Jan 2018 12:07:04 -0600 Subject: [PATCH 09/11] step 2: monitored memory loss --- src/com/premaseem/Calculator.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/com/premaseem/Calculator.java b/src/com/premaseem/Calculator.java index 63397ce..18b4e9d 100644 --- a/src/com/premaseem/Calculator.java +++ b/src/com/premaseem/Calculator.java @@ -6,7 +6,11 @@ @link: https://premaseem.wordpress.com/category/computers/design-patterns/ */ public class Calculator { - +static int num = 0; + public Calculator(){ + num++; + System.out.println(num+ " wasting memory with new object"); + } public Integer add(Integer sum, Integer number){ return sum + number; } From 68dabebf14e16db9be54b92ad84a3524c74c4f45 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Tue, 9 Jan 2018 13:23:24 -0600 Subject: [PATCH 10/11] step 3: avoided memory loss by applying singleton design pattern --- src/com/premaseem/Calculator.java | 18 ++++++++++++++---- src/com/premaseem/Client.java | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/com/premaseem/Calculator.java b/src/com/premaseem/Calculator.java index 18b4e9d..f4bc469 100644 --- a/src/com/premaseem/Calculator.java +++ b/src/com/premaseem/Calculator.java @@ -6,12 +6,22 @@ @link: https://premaseem.wordpress.com/category/computers/design-patterns/ */ public class Calculator { -static int num = 0; - public Calculator(){ + static int num = 0; + private static Calculator instance = null; + + public static Calculator getInstance(){ + if (instance == null){ + instance = new Calculator(); + } + return instance; + } + + private Calculator () { num++; - System.out.println(num+ " wasting memory with new object"); + System.out.println(num + " wasting memory with new object"); } - public Integer add(Integer sum, Integer number){ + + public Integer add (Integer sum, Integer number) { return sum + number; } } diff --git a/src/com/premaseem/Client.java b/src/com/premaseem/Client.java index cc0b9e5..7767822 100644 --- a/src/com/premaseem/Client.java +++ b/src/com/premaseem/Client.java @@ -16,7 +16,7 @@ public static void main (String[] args) { private static Integer calculateSum () { Integer sum = 0; for (int i =0; i< 1000; i++){ - Calculator calc = new Calculator(); + Calculator calc = Calculator.getInstance(); sum = calc.add(sum,i); } return sum; From 4f66234e33ba820d69d8a97799589e889ddfba63 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 8 Aug 2018 18:58:04 -0500 Subject: [PATCH 11/11] Update README.md --- README.md | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9e0034c..f6a0e2b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,21 @@ -# DesignPatternsJava9 -This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns. +# What is memory issue / memory wastage +Memory is one of the most critical resource of a software program and it should be used wisely. +A memory leak reduces the performance of the computer by reducing the amount of available memory. Eventually, in the worst case, too much of the available memory may become allocated and part of the system or device stops working correctly, the application fails, or the system slows down + +Each design pattern is focused to solve certain problem and it might have few side effects as well. A design that worked on a small software might not work on big enterprise software. + +Hence, First thing is to find the primary target of the design and then pick and choose the design pattern based on issue in hand. + +### Learn Design Patterns with Java by Aseem Jain +This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain". + +### Course link: +https://www.packtpub.com/application-development/learn-design-patterns-java-9-video + +### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem + +### Authors blog on design patterns: +https://premaseem.wordpress.com/category/computers/design-patterns/ + +### Software Design pattern community face book page: +https://www.facebook.com/DesignPatternGuru/