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..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. + 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 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 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() + + +