diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..f06d999 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore index 6143e53..719dd1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,10 @@ # Compiled class file +.idea/* *.class # Log file *.log - +*.iws # BlueJ files *.ctxt 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/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 diff --git a/README.md b/README.md index 9e0034c..53112d8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,30 @@ -# 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. +## 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. + +# 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/ 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"); + } +}