Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Compiled class file
.idea/*
*.class

# Log file
*.log

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

11 changes: 11 additions & 0 deletions carFactory/carFactory.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
62 changes: 62 additions & 0 deletions carFactory/src/com/premaseem/CarFactory.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}


8 changes: 8 additions & 0 deletions carFactory/src/module-info.java
Original file line number Diff line number Diff line change
@@ -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 {
}
39 changes: 39 additions & 0 deletions python_Factory_design_pattern/car_factory.py
Original file line number Diff line number Diff line change
@@ -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()