From 0cb0577083344cabbd7ea415396133398c1a7ec6 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Sun, 24 Dec 2017 00:51:28 -0600 Subject: [PATCH 1/7] 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/7] 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/7] 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 3b3ed2930f3a65a4022811149a0fde880159042c Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Thu, 28 Dec 2017 19:00:04 -0600 Subject: [PATCH 4/7] Java implementation of factory pattern --- .gitignore | 1 - carFactory/src/com/premaseem/CarFactory.java | 62 ++++++++++++++++++++ carFactory/src/module-info.java | 8 +++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 carFactory/src/com/premaseem/CarFactory.java create mode 100644 carFactory/src/module-info.java 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/carFactory/src/com/premaseem/CarFactory.java b/carFactory/src/com/premaseem/CarFactory.java new file mode 100644 index 0000000..c12f6d5 --- /dev/null +++ b/carFactory/src/com/premaseem/CarFactory.java @@ -0,0 +1,62 @@ +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 +*/ + +//Class: Factory Design pattern to generate cars based on input parameter +public class CarFactory { + + public static void main(String[] args) { + System.out.println("Car factory creating Cars based on params "); + Generator generator = new Generator(); + + Car car = generator.factory("racecar"); + car.drive(); + + car = generator.factory("Van"); + car.drive(); + } + +} + +class Generator{ + Car factory(String type){ + if (type.equalsIgnoreCase("racecar")){ + return new Racecar(); + } + if (type.equalsIgnoreCase("Van")){ + return new Van(); + } + if (type.equalsIgnoreCase("SUV")){ + return new SUV(); + } + return null; + } +} +interface Car{ + void drive(); +} + +class Racecar implements Car{ + public void drive(){ + System.out.println("Racecar driving."); + } +} + +class Van implements Car{ + public void drive(){ + System.out.println("Van driving."); + } +} + +class SUV implements Car{ + public void drive(){ + System.out.println("SUV driving."); + } +} + + diff --git a/carFactory/src/module-info.java b/carFactory/src/module-info.java new file mode 100644 index 0000000..a9f9a46 --- /dev/null +++ b/carFactory/src/module-info.java @@ -0,0 +1,8 @@ +/* +@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.carFactory { +} \ No newline at end of file From 4d7e8e55b1490e74d6d3b0bfaf30053d3e72dd41 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Thu, 28 Dec 2017 19:00:40 -0600 Subject: [PATCH 5/7] intellij idea project file --- carFactory/carFactory.iml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 carFactory/carFactory.iml diff --git a/carFactory/carFactory.iml b/carFactory/carFactory.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/carFactory/carFactory.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From a8ac3fc20ca5ead29d79f5b5381cb6e8ca61cc8c Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Thu, 28 Dec 2017 19:03:41 -0600 Subject: [PATCH 6/7] python implemetation of factory pattern --- python_Factory_design_pattern/car_factory.py | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 python_Factory_design_pattern/car_factory.py diff --git a/python_Factory_design_pattern/car_factory.py b/python_Factory_design_pattern/car_factory.py new file mode 100644 index 0000000..28a8d14 --- /dev/null +++ b/python_Factory_design_pattern/car_factory.py @@ -0,0 +1,39 @@ +__author__ = 'Aseem Jain' + +class Car(object): + + def factory(type): + if type == "Racecar": + return Racecar() + if type == "Van": + return Van() + if type == "SUV": + return Van() + assert 0, "Bad car creation: " + type + + factory = staticmethod(factory) + +class Racecar(Car): + def drive(self): + print("Racecar driving.") + + +class Van(Car): + def drive(self): + print("Van driving.") + +class SUV(Car): + def drive(self): + print("SUV driving.") + +# Create object using factory. +print("Car factory creating Cars based on params ") +car = Car.factory("Racecar") +car.drive() + +# Create object using factory. +car = Car.factory("Van") +car.drive() + + + From a49147c96c9ba749b87edf50a1e810df526a9d96 Mon Sep 17 00:00:00 2001 From: Aseem Jain Date: Sun, 31 Dec 2017 17:57:47 -0600 Subject: [PATCH 7/7] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 9e0034c..8ee2693 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ +# Video 1.6: Are design patterns different for different languages +Design pattern is language independent and since they are guide lines to the design solution they can be implemented with any programming language, however implementation details might differ. +In the code base, factory pattern is implemented in Java 9 and Python. + # 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. +