Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory Design Pattern #28

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
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
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# 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.
# What is Factory Design Pattern
Factory pattern creates object without exposing the creation logic to the client and refer to newly created object using a common interface.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/factory/diagrams/3.4%20Factory%20Design%20Pattern.png "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/factory/diagrams/Factory%20Icecream%20sequene%20diagram.png "Diagram")

### When to use Factory Design Pattern
Factory design pattern is used when we have a super class with multiple sub-classes and based on input one of the sub-class is expected to be returned.

### 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/

### Note:
* This code base will work on Java 9 and above versions.
* `diagrams` folders carry UML diagrams.
* `pattern` folder has code of primary example.
* `patternBonus` folder has code of secondary or bonus example.
Binary file added diagrams/3.4 Factory Design Pattern.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/Factory Icecream sequene diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 54 additions & 1 deletion pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,61 @@
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/

import com.premaseem.icecreams.ChocolateIceCream;
import com.premaseem.icecreams.IceCream;
import com.premaseem.icecreams.StrawberryIceCream;

import java.util.Scanner;

public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
System.out.println("Ice cream factory example ");
System.out.println("Please enter your ice cream choice");
System.out.println("Strawberry");
System.out.println("Chocolate");

Scanner scan = new Scanner(System.in);
String iceCreamChoice = scan.next();

// Tight coupling
// StrawberryIceCream strawberryIceCream =null;
// ChocolateIceCream chocolateIceCream = null;

// Loose coupling using interface
IceCream iceCream = null;

// Sphegati code with if else ladder
// if (iceCreamChoice.equalsIgnoreCase("Strawberry")){
// strawberryIceCream = new StrawberryIceCream();
// }else if (iceCreamChoice.equalsIgnoreCase("Chocolate")){
// chocolateIceCream = new ChocolateIceCream();
// }

// Crisp, reusable, centralized code using factory
// IceCreamFactory iceCreamFactory = new IceCreamFactory();

// Virtual constructor ( takes care of configuration )
iceCream = IceCreamFactory.createIceCream(iceCreamChoice);

// Repeatative code to even print
// System.out.print("Ice cream of your choice is ");
// if (strawberryIceCream != null){
// System.out.println(strawberryIceCream);
// }
//
// if (chocolateIceCream != null){
// System.out.println(chocolateIceCream);
// }

System.out.print("Ice cream of your choice is "+ iceCream.getIceCreamName());
System.out.println(iceCream);

System.out.println("Lessons Learnt");
System.out.println("Creation is easy and simplified with virtual constructor");
System.out.println("Centralized code easy to maintain and extend");
System.out.println("Loosely coupled code with interface");
System.out.println("Polymorphism in full force");

}
}
27 changes: 27 additions & 0 deletions pattern/src/com/premaseem/IceCreamFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.premaseem;

import com.premaseem.icecreams.ChocolateIceCream;
import com.premaseem.icecreams.IceCream;
import com.premaseem.icecreams.StrawberryIceCream;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/

public class IceCreamFactory {

static public IceCream createIceCream(String iceCreamChoice){
IceCream iceCream = null;

if (iceCreamChoice.equalsIgnoreCase("Strawberry")){
iceCream = new StrawberryIceCream(2,120);

}else if (iceCreamChoice.equalsIgnoreCase("Chocolate")){
iceCream = new ChocolateIceCream(2,250);
}

return iceCream;
}
}
37 changes: 37 additions & 0 deletions pattern/src/com/premaseem/icecreams/ChocolateIceCream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.premaseem.icecreams;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class ChocolateIceCream implements IceCream {

public ChocolateIceCream (Integer cost, Integer calories){
this.cost =cost;
this.calories =calories;
}

String brand = "";
Integer cost = 0;
Integer calories = 0;

public String toString () {
return this.getClass().getSimpleName() + " with Calories: " + getCalories() + " and cost: $" + getCost();
}

@Override
public Integer getCalories () {
return calories;
}

@Override
public Integer getCost () {
return cost;
}

@Override
public String getIceCreamName () {
return this.getClass().getSimpleName();
}
}
16 changes: 16 additions & 0 deletions pattern/src/com/premaseem/icecreams/IceCream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.premaseem.icecreams;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public interface IceCream {

Integer getCalories();
Integer getCost();
String getIceCreamName();



}
37 changes: 37 additions & 0 deletions pattern/src/com/premaseem/icecreams/StrawberryIceCream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.premaseem.icecreams;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class StrawberryIceCream implements IceCream {

public StrawberryIceCream(Integer cost,Integer calories){
this.cost =cost;
this.calories =calories;
}

String brand = "";
Integer cost = 0;
Integer calories = 0;

public String toString () {
return this.getClass().getSimpleName() + " with Calories: " + getCalories() + " and cost: $" + getCost();
}

@Override
public Integer getCalories () {
return calories;
}

@Override
public Integer getCost () {
return cost;
}

@Override
public String getIceCreamName () {
return this.getClass().getSimpleName();
}
}
33 changes: 31 additions & 2 deletions patternBonus/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
package com.premaseem;

import com.premaseem.factory.PizzaBase;
import com.premaseem.factory.PizzaFactory;

import java.util.Scanner;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int repeatRunFlag = 1;
while (repeatRunFlag == 1) {

System.out.println("This is the Client Main Factory Pattern ");

System.out.println("What kind of Pizza would you like to have ? ");
System.out.println("Enter veg for veg pizza ");
System.out.println("Enter non-veg for Non veg pizza ");
System.out.println("Enter max for Non Mexican pizza ");

String pizzaType = scan.next();
PizzaBase pizza = PizzaFactory.getPizza(pizzaType);

System.out.println("Your final order is");
System.out.println(pizza.getDescription());
System.out.println("Toal cost of order is " + pizza.getCost());
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
System.out.println("Do you want to Re-run this program - Press 1 for yes and 0 or other digits to EXIT ");
try{
repeatRunFlag = scan.nextInt();
}catch(Exception e){
repeatRunFlag = 0;
}
}
}
}
17 changes: 17 additions & 0 deletions patternBonus/src/com/premaseem/factory/DefaultPizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.premaseem.factory;

public class DefaultPizza implements PizzaBase {

@Override
public double getCost() {
// TODO Auto-generated method stub
return 5;
}

@Override
public String getDescription() {
// TODO Auto-generated method stub
return "complementry pizza as order item is not available right now with us";
}

}
17 changes: 17 additions & 0 deletions patternBonus/src/com/premaseem/factory/MexicanPizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.premaseem.factory;

public class MexicanPizza implements PizzaBase {

@Override
public double getCost() {
// TODO Auto-generated method stub
return 13;
}

@Override
public String getDescription() {
// TODO Auto-generated method stub
return "Mexican Pizza";
}

}
17 changes: 17 additions & 0 deletions patternBonus/src/com/premaseem/factory/NonVegPizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.premaseem.factory;

public class NonVegPizza implements PizzaBase {

@Override
public double getCost() {
// TODO Auto-generated method stub
return 24;
}

@Override
public String getDescription() {
// TODO Auto-generated method stub
return "Non Veg Pizza";
}

}
8 changes: 8 additions & 0 deletions patternBonus/src/com/premaseem/factory/PizzaBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.premaseem.factory;

public interface PizzaBase {

double getCost();
String getDescription();

}
23 changes: 23 additions & 0 deletions patternBonus/src/com/premaseem/factory/PizzaFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.premaseem.factory;

public class PizzaFactory {


static public PizzaBase getPizza(String pizzaType){
PizzaBase pizza = null;

if(pizzaType.equalsIgnoreCase("veg")){
pizza = new VegPizza();
}else
if(pizzaType.equalsIgnoreCase("nonveg")){
pizza = new NonVegPizza();
}else
if(pizzaType.equalsIgnoreCase("mexican")){
pizza = new MexicanPizza();
}else{
pizza = new DefaultPizza();
}
return pizza;
}

}
17 changes: 17 additions & 0 deletions patternBonus/src/com/premaseem/factory/VegPizza.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.premaseem.factory;

public class VegPizza implements PizzaBase {

@Override
public double getCost() {
// TODO Auto-generated method stub
return 10;
}

@Override
public String getDescription() {
// TODO Auto-generated method stub
return "Veg Pizza";
}

}