diff --git a/.gitignore b/.gitignore index 6143e53..7cd76db 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Compiled class file +.idea/* *.class - # Log file *.log diff --git a/README.md b/README.md index 9e0034c..791d600 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,12 @@ -# DesignPatternsJava9 +# Video 1.8: Issues faced due to force fitting everything in object inheritance +In the branch, inheritance is expalined with its issues. Needs Java 9 and mongodb setup to run the example. + +# Definition of inheritance +Inheritance is a mechanism in which one object acquires all the properties and behaviours of the parent object. Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation to maintain the same behaviour to reuse code. + +# Issues with inheritance +The main problem with implementation inheritance is that it introduces unnecessary coupling in the form of the "fragile base class problem" modifications to the base class implementation can cause inadvertent behavioral changes in subclasses, creates strong coupling and loose flexibility. + +## 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/com.premaseem.inheritance/com.premaseem.inheritance.iml b/com.premaseem.inheritance/com.premaseem.inheritance.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/com.premaseem.inheritance/com.premaseem.inheritance.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/com.premaseem.inheritance/src/com/premaseem/inheritance/Client.java b/com.premaseem.inheritance/src/com/premaseem/inheritance/Client.java new file mode 100644 index 0000000..750c0d9 --- /dev/null +++ b/com.premaseem.inheritance/src/com/premaseem/inheritance/Client.java @@ -0,0 +1,27 @@ +/* +@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.inheritance; + +import com.premaseem.inheritance.model.Developer; +import com.premaseem.inheritance.model.Hr; +import com.premaseem.inheritance.model.QE; + + +public class Client { + public static void main (String[] args) { + Developer employee1 = new Developer("Aseem", 20,100); + QE employee2 = new QE("Aseem", 20,100); + Hr employee3 = new Hr("Sony",1000,10); + + System.out.println("Inheritance concept"); + System.out.println(employee1); + System.out.println(employee2); + System.out.println(employee3); + } + +} diff --git a/com.premaseem.inheritance/src/com/premaseem/inheritance/model/Developer.java b/com.premaseem.inheritance/src/com/premaseem/inheritance/model/Developer.java new file mode 100644 index 0000000..de4da8c --- /dev/null +++ b/com.premaseem.inheritance/src/com/premaseem/inheritance/model/Developer.java @@ -0,0 +1,26 @@ +package com.premaseem.inheritance.model; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Developer extends Employee{ + + public Developer(String name, Integer workHours, Integer rate) { + super(name, rate, workHours,null,null); + } + + @Override + public String toString() { + return "Developer{" + + "name='" + getName() + '\'' + + ", salary=" + getSalary() + + ", workHours=" + getWorkHours() + + ", rate=" + getRate() + + '}'; + } + + +} diff --git a/com.premaseem.inheritance/src/com/premaseem/inheritance/model/Employee.java b/com.premaseem.inheritance/src/com/premaseem/inheritance/model/Employee.java new file mode 100644 index 0000000..43dd0f9 --- /dev/null +++ b/com.premaseem.inheritance/src/com/premaseem/inheritance/model/Employee.java @@ -0,0 +1,59 @@ +package com.premaseem.inheritance.model; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Employee { + + protected String name; + protected Integer workHours; + protected Integer rate; + private Float salary; + protected Integer baseSalary; + protected Integer bonusPercent; + + public Employee (String name, Integer rate, Integer workHours,Integer baseSalary,Integer bonusPercent) { + this.name = name; + this.rate = rate; + this.workHours = workHours; + this.baseSalary = baseSalary; + this.bonusPercent = bonusPercent; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + + public Float getSalary() { + if (this.getClass().getName().equalsIgnoreCase("com.premaseem.inheritance.model.Hr") ){ + salary = (float) baseSalary + bonusPercent; + }else{ + salary = (float) workHours * rate; + } + return salary; + } + + public Integer getWorkHours() { + return workHours; + } + + public void setWorkHours(Integer workHours) { + this.workHours = workHours; + } + + public Integer getRate() { + return rate; + } + + public void setRate(Integer rate) { + this.rate = rate; + } +} diff --git a/com.premaseem.inheritance/src/com/premaseem/inheritance/model/Hr.java b/com.premaseem.inheritance/src/com/premaseem/inheritance/model/Hr.java new file mode 100644 index 0000000..94b6397 --- /dev/null +++ b/com.premaseem.inheritance/src/com/premaseem/inheritance/model/Hr.java @@ -0,0 +1,24 @@ +package com.premaseem.inheritance.model; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class Hr extends Employee{ + + public Hr (String name, Integer baseSalary, Integer bonusPercent) { + super(name,null,null, baseSalary, bonusPercent); + } + + @Override + public String toString() { + return "Hr{" + + "name='" + getName() + '\'' + + ", salary=" + getSalary() + + '}'; + } + + +} diff --git a/com.premaseem.inheritance/src/com/premaseem/inheritance/model/QE.java b/com.premaseem.inheritance/src/com/premaseem/inheritance/model/QE.java new file mode 100644 index 0000000..62463c2 --- /dev/null +++ b/com.premaseem.inheritance/src/com/premaseem/inheritance/model/QE.java @@ -0,0 +1,26 @@ +package com.premaseem.inheritance.model; + +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +public class QE extends Employee{ + + public QE (String name, Integer workHours, Integer rate) { + super(name, rate, workHours,null,null); + } + + @Override + public String toString() { + return "Developer{" + + "name='" + getName() + '\'' + + ", salary=" + getSalary() + + ", workHours=" + getWorkHours() + + ", rate=" + getRate() + + '}'; + } + + +} diff --git a/com.premaseem.inheritance/src/module-info.java b/com.premaseem.inheritance/src/module-info.java new file mode 100644 index 0000000..4fd16c1 --- /dev/null +++ b/com.premaseem.inheritance/src/module-info.java @@ -0,0 +1,9 @@ +/* +@author: Aseem Jain +@title: Design Patterns with Java 9 +@link: https://premaseem.wordpress.com/category/computers/design-patterns/ +@copyright: 2018 Packt Publication +*/ +module com.premaseem.inheritance { + +} \ No newline at end of file