From 0cb0577083344cabbd7ea415396133398c1a7ec6 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Sun, 24 Dec 2017 00:51:28 -0600 Subject: [PATCH 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 436e9513dfff58edec52126882b84540c19157a1 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Sun, 7 Jan 2018 01:57:40 -0600 Subject: [PATCH 7/8] Concept of tight coupling and loose coupling --- README.md | 14 +++++ src/CyclomatricComplexity.java | 53 ------------------- src/CyclomatricSimplicity.java | 35 ------------ src/com/premaseem/loose/Bike.java | 15 ++++++ src/com/premaseem/loose/Car.java | 16 ++++++ src/com/premaseem/loose/Client.java | 32 +++++++++++ .../loose/LooselyCoupledTraveller.java | 30 +++++++++++ src/com/premaseem/loose/Truck.java | 13 +++++ src/com/premaseem/loose/Vehicle.java | 11 ++++ src/com/premaseem/tight/Bike.java | 15 ++++++ src/com/premaseem/tight/Car.java | 15 ++++++ src/com/premaseem/tight/Client.java | 21 ++++++++ .../tight/TightCoupledTraveller.java | 44 +++++++++++++++ src/com/premaseem/tight/Truck.java | 13 +++++ 14 files changed, 239 insertions(+), 88 deletions(-) delete mode 100644 src/CyclomatricComplexity.java delete mode 100644 src/CyclomatricSimplicity.java create mode 100644 src/com/premaseem/loose/Bike.java create mode 100644 src/com/premaseem/loose/Car.java create mode 100644 src/com/premaseem/loose/Client.java create mode 100644 src/com/premaseem/loose/LooselyCoupledTraveller.java create mode 100644 src/com/premaseem/loose/Truck.java create mode 100644 src/com/premaseem/loose/Vehicle.java create mode 100644 src/com/premaseem/tight/Bike.java create mode 100644 src/com/premaseem/tight/Car.java create mode 100644 src/com/premaseem/tight/Client.java create mode 100644 src/com/premaseem/tight/TightCoupledTraveller.java create mode 100644 src/com/premaseem/tight/Truck.java diff --git a/README.md b/README.md index 9e0034c..4bf1200 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,16 @@ +## Video 2.1 of Design Patterns Tutorial uses this code + +# What is tight coupling +This scenario arises when a class is not desiged to be independent and assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class. + +The tight coupling between classes creates a ripple effect, which means when one class is changed it impacts too many other classes. A minor change in one class would need to modify and compile many other classes. + +# What is loose coupling +Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns. If the only knowledge that class A has about class B, is what class B has exposed through its interface, then class A and class B are said to be loosely coupled. + +A loosely-coupled class can be consumed and tested independently of other (concrete) classes. + +Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces rather than other concrete classes, and any class can be on the other end of that communication simply by implementing the interface. + # 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. 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. diff --git a/src/com/premaseem/loose/Bike.java b/src/com/premaseem/loose/Bike.java new file mode 100644 index 0000000..2ee6968 --- /dev/null +++ b/src/com/premaseem/loose/Bike.java @@ -0,0 +1,15 @@ +package com.premaseem.loose; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Bike implements Vehicle { + + @Override + public void move () { + System.out.println("Moving"); + } +} diff --git a/src/com/premaseem/loose/Car.java b/src/com/premaseem/loose/Car.java new file mode 100644 index 0000000..92ce598 --- /dev/null +++ b/src/com/premaseem/loose/Car.java @@ -0,0 +1,16 @@ +package com.premaseem.loose; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Car implements Vehicle { + + + @Override + public void move () { + System.out.println("Moving"); + } +} diff --git a/src/com/premaseem/loose/Client.java b/src/com/premaseem/loose/Client.java new file mode 100644 index 0000000..9899da1 --- /dev/null +++ b/src/com/premaseem/loose/Client.java @@ -0,0 +1,32 @@ +package com.premaseem.loose; + +/* +@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) { + LooselyCoupledTraveller traveller = new LooselyCoupledTraveller(); + + // Here client does not need to know about implementor + + Vehicle car= traveller.getCar(); + Vehicle bike = traveller.getBike(); + Vehicle truck= traveller.getTruck(); + // Easy to add any other vehicle without + // Vehicle plane= traveller.getPlane(); + + traveller.startJourney(car); + // Polymorphism in full force + traveller.startJourney(bike); + traveller.startJourney(truck); + + // Take Away: + // Class needs not to be modified or changes, compiled or retested + // when any sub class needs to be added or changed. + // All classes are independent + + } +} diff --git a/src/com/premaseem/loose/LooselyCoupledTraveller.java b/src/com/premaseem/loose/LooselyCoupledTraveller.java new file mode 100644 index 0000000..bce381f --- /dev/null +++ b/src/com/premaseem/loose/LooselyCoupledTraveller.java @@ -0,0 +1,30 @@ +package com.premaseem.loose; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class LooselyCoupledTraveller { + Vehicle car= new Car(); + Vehicle bike = new Bike(); + Vehicle truck= new Truck(); + + public Vehicle getCar () { + return car; + } + + public Vehicle getBike () { + return bike; + } + + public Vehicle getTruck () { + return truck; + } + + public void startJourney (Vehicle car){ + car.move(); + } + +} diff --git a/src/com/premaseem/loose/Truck.java b/src/com/premaseem/loose/Truck.java new file mode 100644 index 0000000..8585b8e --- /dev/null +++ b/src/com/premaseem/loose/Truck.java @@ -0,0 +1,13 @@ +package com.premaseem.loose; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Truck implements Vehicle { + public void move () { + System.out.println("Moving"); + } +} diff --git a/src/com/premaseem/loose/Vehicle.java b/src/com/premaseem/loose/Vehicle.java new file mode 100644 index 0000000..448a041 --- /dev/null +++ b/src/com/premaseem/loose/Vehicle.java @@ -0,0 +1,11 @@ +package com.premaseem.loose; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public interface Vehicle { + void move (); +} diff --git a/src/com/premaseem/tight/Bike.java b/src/com/premaseem/tight/Bike.java new file mode 100644 index 0000000..4e0833d --- /dev/null +++ b/src/com/premaseem/tight/Bike.java @@ -0,0 +1,15 @@ +package com.premaseem.tight; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Bike { + + + public void move () { + System.out.println("Moving"); + } +} diff --git a/src/com/premaseem/tight/Car.java b/src/com/premaseem/tight/Car.java new file mode 100644 index 0000000..bf22fa2 --- /dev/null +++ b/src/com/premaseem/tight/Car.java @@ -0,0 +1,15 @@ +package com.premaseem.tight; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Car { + + + public void move () { + System.out.println("Moving"); + } +} diff --git a/src/com/premaseem/tight/Client.java b/src/com/premaseem/tight/Client.java new file mode 100644 index 0000000..93f69f4 --- /dev/null +++ b/src/com/premaseem/tight/Client.java @@ -0,0 +1,21 @@ +package com.premaseem.tight; + +/* +@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) { + TightCoupledTraveller traveller = new TightCoupledTraveller(); + + Car car= traveller.getCar(); + Bike bike = traveller.getBike(); + Truck truck= traveller.getTruck(); + + traveller.startJourney(car); + traveller.startJourney(bike); + traveller.startJourney(truck); + } +} diff --git a/src/com/premaseem/tight/TightCoupledTraveller.java b/src/com/premaseem/tight/TightCoupledTraveller.java new file mode 100644 index 0000000..25036f9 --- /dev/null +++ b/src/com/premaseem/tight/TightCoupledTraveller.java @@ -0,0 +1,44 @@ +package com.premaseem.tight; + +import com.premaseem.tight.Bike; +import com.premaseem.tight.Car; +import com.premaseem.tight.Truck; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class TightCoupledTraveller { + Car car= new Car(); + Bike bike = new Bike(); + Truck truck= new Truck(); + + public Car getCar () { + return car; + } + + public Bike getBike () { + return bike; + } + + public Truck getTruck () { + return truck; + } + + public void startJourney (Car car){ + car.move(); + } + + public void startJourney (Bike bike){ + bike.move(); + } + + public void startJourney (Truck truck){ + truck.move(); + } + + + +} diff --git a/src/com/premaseem/tight/Truck.java b/src/com/premaseem/tight/Truck.java new file mode 100644 index 0000000..e181c65 --- /dev/null +++ b/src/com/premaseem/tight/Truck.java @@ -0,0 +1,13 @@ +package com.premaseem.tight; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Truck { + public void move () { + System.out.println("Moving"); + } +} From ab294ba1e7efb47e7f53d0492b8494d1fc36d7a2 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 8 Aug 2018 19:04:13 -0500 Subject: [PATCH 8/8] Update README.md --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4bf1200..53112d8 100644 --- a/README.md +++ b/README.md @@ -12,5 +12,19 @@ A loosely-coupled class can be consumed and tested independently of other (concr Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces rather than other concrete classes, and any class can be on the other end of that communication simply by implementing the interface. -# 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. +# The fragile base-class problem +Base classes are considered fragile because you can modify a base class in a seemingly safe way, but this new behavior, when inherited by the derived classes, might cause the derived classes to malfunction. You can't tell whether a base-class change is safe simply by examining the base class's methods in isolation; you must look at (and test) all derived classes as well. Moreover, you must check all code that uses both base-class and derived-class objects too, since this code might also be broken by the new behavior. A simple change to a key base class can render an entire program inoperable. + +### 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/