From 0cb0577083344cabbd7ea415396133398c1a7ec6 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Sun, 24 Dec 2017 00:51:28 -0600 Subject: [PATCH 01/13] 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/13] 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/13] 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 6856cb7642c099a09c04fc07781978c0cdbb070f Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Mon, 25 Dec 2017 20:12:03 -0600 Subject: [PATCH 04/13] Read me for DRY and WET design principles --- README.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e0034c..36a2a84 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,24 @@ -# DesignPatternsJava9 +# Design Principles +Software design principles represent a set of guidelines that helps us to avoid having a bad design. + +## DRY +Don’t Repeat Yourself is a software development principle, the main aim of which is to reduce repetition of code. +## WET +Write Every Time is a cheeky abbreviation to mean the opposite i.e. code that doesn’t adhere to DRY principle. + +## Advantages of DRY + +### Maintainability +If password for data base connection changes, then you have to make change only at one place. + +### Reuse +DRY inherently promotes reuse of code because we are merging 2 or more instances of repeating code into a single block of code. Reusable code pays of in the long run as it speeds development time. + +### Cost +More code costs more. More code takes more people more time to maintain and to address bugs. + +### Testing +If code is not repeated, you just have to test one main path. Of course, different behaviors still need to be tested. + +## 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. From 7488ba4aa6fd6b54b35d8fb58ffe035366c7dca5 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 27 Dec 2017 14:39:00 -0600 Subject: [PATCH 05/13] WET - Write Every Time example code of employees --- dry/src/com/premaseem/main/Main.java | 25 ++++++++++ wet/src/com/premaseem/Employee.java | 71 ++++++++++++++++++++++++++++ wet/src/module-info.java | 4 ++ 3 files changed, 100 insertions(+) create mode 100644 dry/src/com/premaseem/main/Main.java create mode 100644 wet/src/com/premaseem/Employee.java create mode 100644 wet/src/module-info.java diff --git a/dry/src/com/premaseem/main/Main.java b/dry/src/com/premaseem/main/Main.java new file mode 100644 index 0000000..e18cc04 --- /dev/null +++ b/dry/src/com/premaseem/main/Main.java @@ -0,0 +1,25 @@ +package com.premaseem.main; + +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import org.bson.Document; + + +public class Main { + public static void main(String[] args) { + MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); + MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); + MongoCollection employees = designPatternsdb.getCollection("employees"); + FindIterable documents = employees.find(); + for(Document doc : documents){ + System.out.println(doc); + } + + } + Main m = new Main(); + + +} diff --git a/wet/src/com/premaseem/Employee.java b/wet/src/com/premaseem/Employee.java new file mode 100644 index 0000000..9a0c762 --- /dev/null +++ b/wet/src/com/premaseem/Employee.java @@ -0,0 +1,71 @@ +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ + +package com.premaseem; + +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import org.bson.Document; + +import java.util.Date; + +public class Employee { + + public static void main(String[] args) { + countEmployees(); + addEmployees(); + listEmployees(); + countEmployees(); + } + + private static void listEmployees() { + MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); + MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); + MongoCollection employees = designPatternsdb.getCollection("employees"); + FindIterable documents = employees.find(); + for(Document doc : documents){ + System.out.println(doc); + } + } + + private static void countEmployees() { + MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); + MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); + MongoCollection employees = designPatternsdb.getCollection("employees"); + long count = employees.count(); + System.out.println("total number of employees = " + count); + + } + + private static void addEmployees(){ + Document emp1 = new Document(); + emp1.put("name","Aseem Jain"); + emp1.put("role","Developer"); + emp1.put("createdDate", new Date()); + + Document emp2 = new Document(); + emp1.put("name","Meera Jain"); + emp1.put("role","Manager"); + emp1.put("createdDate", new Date()); + + Document emp3 = new Document(); + emp1.put("name","Sony Jain"); + emp1.put("role","QE"); + emp1.put("createdDate", new Date()); + + MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); + MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); + MongoCollection employees = designPatternsdb.getCollection("employees"); + + employees.insertOne(emp1); + employees.insertOne(emp2); + employees.insertOne(emp3); + } +} diff --git a/wet/src/module-info.java b/wet/src/module-info.java new file mode 100644 index 0000000..ed21bef --- /dev/null +++ b/wet/src/module-info.java @@ -0,0 +1,4 @@ +module wet { + exports com.premaseem; + requires mongo.java.driver; +} \ No newline at end of file From 58a9bdbbaa19ed670c94faec735f52079151cef7 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 27 Dec 2017 14:44:51 -0600 Subject: [PATCH 06/13] refactoring module names --- dry/src/com/premaseem/main/Main.java | 25 ------------------------- wet/src/module-info.java | 2 +- 2 files changed, 1 insertion(+), 26 deletions(-) delete mode 100644 dry/src/com/premaseem/main/Main.java diff --git a/dry/src/com/premaseem/main/Main.java b/dry/src/com/premaseem/main/Main.java deleted file mode 100644 index e18cc04..0000000 --- a/dry/src/com/premaseem/main/Main.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.premaseem.main; - -import com.mongodb.client.FindIterable; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.MongoDatabase; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; -import org.bson.Document; - - -public class Main { - public static void main(String[] args) { - MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); - MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); - MongoCollection employees = designPatternsdb.getCollection("employees"); - FindIterable documents = employees.find(); - for(Document doc : documents){ - System.out.println(doc); - } - - } - Main m = new Main(); - - -} diff --git a/wet/src/module-info.java b/wet/src/module-info.java index ed21bef..a4e0106 100644 --- a/wet/src/module-info.java +++ b/wet/src/module-info.java @@ -1,4 +1,4 @@ -module wet { +module com.premaesem.wet { exports com.premaseem; requires mongo.java.driver; } \ No newline at end of file From 9bba839ab90368d22385d53920b5d32fc138b49d Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 27 Dec 2017 15:09:29 -0600 Subject: [PATCH 07/13] added dry module --- com.premaseem.dry/src/module-info.java | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 com.premaseem.dry/src/module-info.java diff --git a/com.premaseem.dry/src/module-info.java b/com.premaseem.dry/src/module-info.java new file mode 100644 index 0000000..0470007 --- /dev/null +++ b/com.premaseem.dry/src/module-info.java @@ -0,0 +1,3 @@ +module com.premaseem.dry { + requires mongo.java.driver; +} \ No newline at end of file From 8d4ba3f30174d8bf75783a6a0f183c844b2799e1 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 27 Dec 2017 15:10:10 -0600 Subject: [PATCH 08/13] DB connector component added --- .../src/com/premaseem/DBconnector.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 com.premaseem.dry/src/com/premaseem/DBconnector.java diff --git a/com.premaseem.dry/src/com/premaseem/DBconnector.java b/com.premaseem.dry/src/com/premaseem/DBconnector.java new file mode 100644 index 0000000..facfce0 --- /dev/null +++ b/com.premaseem.dry/src/com/premaseem/DBconnector.java @@ -0,0 +1,46 @@ +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 +*/ + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; + +public class DBconnector { + + MongoDatabase mongoDatabase = null; + MongoDatabase designPatternsdb = null; + + public DBconnector(){ + MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); + MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); + } + + public MongoCollection getTable(String tableName){ + return designPatternsdb.getCollection(tableName); + } + + + public MongoDatabase getMongoDatabase () { + return mongoDatabase; + } + + public void setMongoDatabase (MongoDatabase mongoDatabase) { + this.mongoDatabase = mongoDatabase; + } + + public MongoDatabase getDesignPatternsdb () { + return designPatternsdb; + } + + public void setDesignPatternsdb (MongoDatabase designPatternsdb) { + this.designPatternsdb = designPatternsdb; + } +} From 8b8488daef1cb8574c9f4fdc5b4f0fdac742bfbc Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 27 Dec 2017 16:21:18 -0600 Subject: [PATCH 09/13] =?UTF-8?q?DRY=20-=20Don=E2=80=99t=20Repeat=20Yourse?= =?UTF-8?q?lf=20code=20added=20in=20dry=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/premaseem/DBconnector.java | 27 +------- .../src/com/premaseem/Employee.java | 67 +++++++++++++++++++ 2 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 com.premaseem.dry/src/com/premaseem/Employee.java diff --git a/com.premaseem.dry/src/com/premaseem/DBconnector.java b/com.premaseem.dry/src/com/premaseem/DBconnector.java index facfce0..04f7a06 100644 --- a/com.premaseem.dry/src/com/premaseem/DBconnector.java +++ b/com.premaseem.dry/src/com/premaseem/DBconnector.java @@ -15,32 +15,9 @@ public class DBconnector { - MongoDatabase mongoDatabase = null; - MongoDatabase designPatternsdb = null; - - public DBconnector(){ + public static MongoCollection getDBconnection () { MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); - } - - public MongoCollection getTable(String tableName){ - return designPatternsdb.getCollection(tableName); - } - - - public MongoDatabase getMongoDatabase () { - return mongoDatabase; - } - - public void setMongoDatabase (MongoDatabase mongoDatabase) { - this.mongoDatabase = mongoDatabase; - } - - public MongoDatabase getDesignPatternsdb () { - return designPatternsdb; - } - - public void setDesignPatternsdb (MongoDatabase designPatternsdb) { - this.designPatternsdb = designPatternsdb; + return designPatternsdb.getCollection("employees"); } } diff --git a/com.premaseem.dry/src/com/premaseem/Employee.java b/com.premaseem.dry/src/com/premaseem/Employee.java new file mode 100644 index 0000000..ed5c6d1 --- /dev/null +++ b/com.premaseem.dry/src/com/premaseem/Employee.java @@ -0,0 +1,67 @@ +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ + +package com.premaseem; + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; + +import java.util.Date; + +public class Employee { + + public Employee(){ + } + + public static void main(String[] args) { + Employee employee = new Employee(); + employee.listEmployees(); + employee.countEmployees(); + employee.addEmployees(); + employee.listEmployees(); + employee.countEmployees(); + } + + private void listEmployees() { + MongoCollection employees = DBconnector.getDBconnection(); + FindIterable documents = employees.find(); + for(Document doc : documents){ + System.out.println(doc); + } + } + + private static void countEmployees() { + MongoCollection employees = DBconnector.getDBconnection();long count = employees.count(); + System.out.println("total number of employees = " + count); + } + + private static void addEmployees(){ + Document emp1 = new Document(); + emp1.put("name","Aseem Jain"); + emp1.put("role","Developer"); + emp1.put("createdDate", new Date()); + + Document emp2 = new Document(); + emp1.put("name","Meera Jain"); + emp1.put("role","Manager"); + emp1.put("createdDate", new Date()); + + Document emp3 = new Document(); + emp1.put("name","Sony Jain"); + emp1.put("role","QE"); + emp1.put("createdDate", new Date()); + + MongoCollection employees = DBconnector.getDBconnection(); + employees.insertOne(emp1); + employees.insertOne(emp2); + employees.insertOne(emp3); + } +} From af27fa59bf87ac7edef20f3afec9c43213e70302 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 27 Dec 2017 18:35:46 -0600 Subject: [PATCH 10/13] final touch --- .../src/com/premaseem/Employee.java | 3 --- wet/src/com/premaseem/Employee.java | 18 +++++++++--------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/com.premaseem.dry/src/com/premaseem/Employee.java b/com.premaseem.dry/src/com/premaseem/Employee.java index ed5c6d1..49109b9 100644 --- a/com.premaseem.dry/src/com/premaseem/Employee.java +++ b/com.premaseem.dry/src/com/premaseem/Employee.java @@ -7,11 +7,8 @@ package com.premaseem; -import com.mongodb.MongoClient; -import com.mongodb.MongoClientURI; import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; -import com.mongodb.client.MongoDatabase; import org.bson.Document; import java.util.Date; diff --git a/wet/src/com/premaseem/Employee.java b/wet/src/com/premaseem/Employee.java index 9a0c762..e890007 100644 --- a/wet/src/com/premaseem/Employee.java +++ b/wet/src/com/premaseem/Employee.java @@ -26,7 +26,7 @@ public static void main(String[] args) { } private static void listEmployees() { - MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); + MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27018")); MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); MongoCollection employees = designPatternsdb.getCollection("employees"); FindIterable documents = employees.find(); @@ -36,7 +36,7 @@ private static void listEmployees() { } private static void countEmployees() { - MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); + MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27018")); MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); MongoCollection employees = designPatternsdb.getCollection("employees"); long count = employees.count(); @@ -51,16 +51,16 @@ private static void addEmployees(){ emp1.put("createdDate", new Date()); Document emp2 = new Document(); - emp1.put("name","Meera Jain"); - emp1.put("role","Manager"); - emp1.put("createdDate", new Date()); + emp2.put("name","Meera Jain"); + emp2.put("role","Manager"); + emp2.put("createdDate", new Date()); Document emp3 = new Document(); - emp1.put("name","Sony Jain"); - emp1.put("role","QE"); - emp1.put("createdDate", new Date()); + emp3.put("name","Sony Jain"); + emp3.put("role","QE"); + emp3.put("createdDate", new Date()); - MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017")); + MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27018")); MongoDatabase designPatternsdb = mongoClient.getDatabase("designPatterns"); MongoCollection employees = designPatternsdb.getCollection("employees"); From a8268ada394c382197f597327b49ad1374c600b8 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 27 Dec 2017 20:04:49 -0600 Subject: [PATCH 11/13] Idea intellij project files --- .gitignore | 1 - DesignPatternsJava9.iml | 9 +++++++++ com.premaseem.dry/com.premaseem.dry.iml | 20 ++++++++++++++++++++ wet/com.premaseem.wet.iml | 22 ++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 DesignPatternsJava9.iml create mode 100644 com.premaseem.dry/com.premaseem.dry.iml create mode 100644 wet/com.premaseem.wet.iml diff --git a/.gitignore b/.gitignore index 079beb8..7cd76db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ # Compiled class file .idea/* *.class -*.iml # Log file *.log diff --git a/DesignPatternsJava9.iml b/DesignPatternsJava9.iml new file mode 100644 index 0000000..8021953 --- /dev/null +++ b/DesignPatternsJava9.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/com.premaseem.dry/com.premaseem.dry.iml b/com.premaseem.dry/com.premaseem.dry.iml new file mode 100644 index 0000000..f9c4e9d --- /dev/null +++ b/com.premaseem.dry/com.premaseem.dry.iml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/wet/com.premaseem.wet.iml b/wet/com.premaseem.wet.iml new file mode 100644 index 0000000..be8e252 --- /dev/null +++ b/wet/com.premaseem.wet.iml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 462ec14c27dd27b050cea1f51a83f8a226c9d103 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Sun, 31 Dec 2017 18:11:16 -0600 Subject: [PATCH 12/13] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 36a2a84..0bd2cd4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ +# Video 1.9: Fundamental concepts of code reusability +The code in this branch was used in video 1.9 of video course Design patterhs with Java 9. +This brach contains code to demonstrate fundamental concepts of code reusability. + # Design Principles Software design principles represent a set of guidelines that helps us to avoid having a bad design. From 1b22cf2f89f4cfd86327f178703e62172a076993 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Wed, 8 Aug 2018 19:10:53 -0500 Subject: [PATCH 13/13] Update README.md --- README.md | 85 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 0bd2cd4..5bb69db 100644 --- a/README.md +++ b/README.md @@ -2,27 +2,90 @@ The code in this branch was used in video 1.9 of video course Design patterhs with Java 9. This brach contains code to demonstrate fundamental concepts of code reusability. -# Design Principles +## What are Software Design Principles? Software design principles represent a set of guidelines that helps us to avoid having a bad design. +there are 3 important characteristics of a bad design that should be avoided: -## DRY +* Rigidity - It is hard to change because every change affects too many other parts of the system. +* Fragility - When you make a change, unexpected parts of the system break. +* Immobility - It is hard to reuse in another application because it cannot be disentangled from the current application. + +## Is your code Solid ? +S.O.L.I.D is an acronym for the first five object-oriented design(OOD) principles +S.O.L.I.D stands for: +* S - Single-responsiblity principle +* O - Open-closed principle +* L - Liskov substitution principle +* I - Interface segregation principle +* D - Dependency Inversion Principle + +### Single Responsibility Principle +A class should have only one reason to change. +In this context a responsibility is considered to be one reason to change. This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes. When we need to make a change in a class having more responsibilities the change might affect the other functionality of the classes. +Code Example: Base class is doing too many things, the salary calculator should be a class in itself. + +### Open Close Principle +Software entities like classes, modules and functions should be open for extension but closed for modifications. +You can consider it when writing your classes to make sure that when you need to extend their behavior you dont have to change the class but to extend it. + +When referring to the classes Open Close Principle can be ensured by use of Abstract Classes or interface and concrete classes for implementing their behavior. This will enforce having Concrete Classes extending Abstract Classes instead of changing them. Some particular cases of this are Template Pattern and Strategy Pattern. + +Example: To get desired behavior from a library you consume (if that library follows open close principal), instead of making modifcation in its base class you should be able extend or subclass it and apply your logic to get desired output. +Code Example: Base class is doing too many things, the salary calculator should be a class in itself. + + +### Interface Segregation Principle +Clients should not be forced to depend upon interfaces that they don't use. +This principle teaches us to take care how we write our interfaces. When we write our interfaces we should take care to add only methods that should be there. If we add methods that should not be there the classes implementing the interface will have to implement those methods as well. For example if we create an interface called Worker and add a method lunch break, all the workers will have to implement it. What if the worker is a robot? +As a conclusion Interfaces containing methods that are not specific to it are called polluted or fat interfaces. We should avoid them. +Code Example: Weapon allowance in our base class. + + +### Liskov's Substitution Principle +Derived types must be completely substitutable for their base types. +This principle is just an extension of the Open Close Principle in terms of behavior meaning that we must make sure that new derived classes are extending the base classes without changing their behavior. The new derived classes should be able to replace the base classes without any change in the code. + +### Dependency Inversion Principle +Abstractions should not depend on details. Details should depend on abstractions. +Dependency Inversion Principle states that we should decouple high level modules from low level modules, introducing an abstraction layer between the high level classes and low level classes. + +By applying the Dependency Inversion the modules can be easily changed by other modules just by changing the dependency module. +Factories and Abstract Factories can be used as dependency frameworks, but there are specialized frameworks for that, known as Inversion of Control Container. + +code Example: simplest example is hibernate which is nothing but implementation of JPA interface. + +JPA itself is just a specification, not a product, it cannot perform persistence or anything else by itself. JPA is just a set of interfaces, and requires an implementation. +Hibernate is not coupled becuase of JPA interface and it can be seemlesly replaced by any other product or JPA provide which implements JPA Interface :-) + +### DRY Don’t Repeat Yourself is a software development principle, the main aim of which is to reduce repetition of code. -## WET -Write Every Time is a cheeky abbreviation to mean the opposite i.e. code that doesn’t adhere to DRY principle. -## Advantages of DRY +### WET +Write Every Time is a cheeky abbreviation to mean the opposite i.e. code that doesn’t adhere to DRY principle. -### Maintainability +##### Advantages of DRY +* Maintainability If password for data base connection changes, then you have to make change only at one place. -### Reuse +* Reuse DRY inherently promotes reuse of code because we are merging 2 or more instances of repeating code into a single block of code. Reusable code pays of in the long run as it speeds development time. -### Cost +* Cost More code costs more. More code takes more people more time to maintain and to address bugs. -### Testing +* Testing If code is not repeated, you just have to test one main path. Of course, different behaviors still need to be tested. -## 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. +### 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/