Skip to content

Abstract factory design pattern #29

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

Open
wants to merge 6 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 Abstract Factory Design Pattern
Abstract factory is also called as factory of factories. Abstract Factory patterns work around a super-factory which creates other factories. In Abstract Factory pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/abstract-factory/diagrams/3_5%20Abstract%20Factory%20Design%20Pattern%20-%20class%20diagram.jpeg "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/abstract-factory/diagrams/3_5%20Abstract%20Factory%20design%20pattern%20-%20sequence%20diagrams.png "Diagram")

### When to use Abstract Factory Design Pattern
When application needs a level of indirection that abstracts the creation of families of related or dependent objects without directly specifying their concrete classes. The "factory" object has the responsibility for providing creation services for the entire platform family.

### 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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions pattern/src/com/premaseem/AbstractIceCreamFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.premaseem;

import com.premaseem.icecreams.IceCream;
import com.premaseem.milkshake.MilkShake;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public abstract class AbstractIceCreamFactory {
abstract IceCream createIceCream(String choice);
abstract MilkShake createMilkShake(String choice);

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

import com.premaseem.icecreams.ChocolateIceCream;
import com.premaseem.icecreams.IceCream;
import com.premaseem.icecreams.StrawberryIceCream;
import com.premaseem.milkshake.ChocolateMilkShake;
import com.premaseem.milkshake.MilkShake;
import com.premaseem.milkshake.StrawberryMilkShake;

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

/**
* This factory creates all daily products of Amul Brand
*/
public class AmulIceCreamFactory extends AbstractIceCreamFactory{

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

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

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

return iceCream;
}

@Override
MilkShake createMilkShake (String choice) {
MilkShake milkShake = null;

if (choice.equalsIgnoreCase("Strawberry")){
milkShake = new StrawberryMilkShake("Amul",2);

}else if (choice.equalsIgnoreCase("Chocolate")){
milkShake = new ChocolateMilkShake("Amul",2);
}
return milkShake;
}
}
77 changes: 75 additions & 2 deletions pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,83 @@
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/

import com.premaseem.icecreams.IceCream;
import com.premaseem.milkshake.MilkShake;

import java.util.Scanner;

public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
Scanner scan = new Scanner(System.in);
System.out.println("Ice cream Abstract factory example ");


System.out.println("What you would like to order ... ");
System.out.println("Icecream");
System.out.println("Milkshake");
String choice = scan.next();

System.out.println("Please enter your ice cream flavor ... ");
System.out.println("Strawberry");
System.out.println("Chocolate");
String flavor = scan.next();

System.out.println("Please enter the Brand of Ice cream ... ");
System.out.println("Dairy Queen");
System.out.println("Amul");
String brand = scan.next();

AbstractIceCreamFactory factory = FactoryCreator.getFactory(brand);

// Virtual constructor ( takes care of configuration )
if ( choice.equalsIgnoreCase("icecream")) {
IceCream iceCream = factory.createIceCream(flavor);
System.out.println(iceCream);
}else {
MilkShake milkShake = factory.createMilkShake(flavor);
System.out.println(milkShake);
}

// Family of similar products with different brands. ( Tight couple)
// DairyQueenStrawberryIceCream dairyQueenStrawberryIceCream =null;
// DairyQueenChocolateIceCream dairyQueenChocolateIceCream = null;
// AmulStrawberryIceCream amulStrawberryIceCream =null;
// AmulChocolateIceCream amulchocolateIceCream = null;

// // Sphegati code with if else ladder
// if (iceCreamBrand.equalsIgnoreCase("Amul")
// && iceCreamChoice.equalsIgnoreCase("Strawberry")){
// amulStrawberryIceCream = new AmulStrawberryIceCream();
//
// }else if (iceCreamBrand.equalsIgnoreCase("Amul") &&
// iceCreamChoice.equalsIgnoreCase("Chocolate")){
// amulchocolateIceCream = new AmulChocolateIceCream();
//
// }else if (iceCreamBrand.equalsIgnoreCase("Dairy Queen")
// && iceCreamChoice.equalsIgnoreCase("Strawberry")){
// dairyQueenStrawberryIceCream = new DairyQueenStrawberryIceCream();
//
// }else if (iceCreamBrand.equalsIgnoreCase("Dairy Queen") &&
// iceCreamChoice.equalsIgnoreCase("Chocolate")){
// dairyQueenChocolateIceCream = new DairyQueenChocolateIceCream();
// }
//
// // Ice cream of your choice is :
// System.out.print("Ice cream of your choice is ");
// if (amulStrawberryIceCream != null){
// System.out.println(amulStrawberryIceCream);
// }
// if (amulchocolateIceCream != null){
// System.out.println(amulchocolateIceCream);
// }
// if (dairyQueenStrawberryIceCream != null){
// System.out.println(dairyQueenStrawberryIceCream);
// }
// if (dairyQueenChocolateIceCream != null){
// System.out.println(dairyQueenChocolateIceCream);
// }

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

import com.premaseem.icecreams.ChocolateIceCream;
import com.premaseem.icecreams.IceCream;
import com.premaseem.icecreams.StrawberryIceCream;
import com.premaseem.milkshake.ChocolateMilkShake;
import com.premaseem.milkshake.MilkShake;
import com.premaseem.milkshake.StrawberryMilkShake;

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

/**
* This factory creates all daily products of Dairy Queen Brand
*/
public class DairyQueenCreamFactory extends AbstractIceCreamFactory{

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

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

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

return iceCream;
}

@Override
MilkShake createMilkShake (String choice) {
MilkShake milkShake = null;

if (choice.equalsIgnoreCase("Strawberry")){
milkShake = new StrawberryMilkShake("Dairy Queen",2);

}else if (choice.equalsIgnoreCase("Chocolate")){
milkShake = new ChocolateMilkShake("Dairy Queen",2);
}
return milkShake;
}
}
16 changes: 16 additions & 0 deletions pattern/src/com/premaseem/FactoryCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class FactoryCreator {
static AbstractIceCreamFactory getFactory (String brand) {
if (brand.equalsIgnoreCase("Amul")){
return new AmulIceCreamFactory();
}else {
return new DairyQueenCreamFactory();
}
}
}
40 changes: 40 additions & 0 deletions pattern/src/com/premaseem/icecreams/ChocolateIceCream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 (String brand,Integer cost, Integer calories){
this.brand = brand;
this.cost =cost;
this.calories =calories;
}

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


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

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

@Override
public String getBrand () {
return brand;
}

public String toString () {
return this.getClass().getSimpleName() + "of Brand " + brand +
" with Calories: " + getCalories() + " and cost: $" + getCost();
}
}
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 getBrand();



}
40 changes: 40 additions & 0 deletions pattern/src/com/premaseem/icecreams/StrawberryIceCream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 {

String brand;
Integer cost;
Integer calories;

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

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

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

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

@Override
public String getBrand () {
return brand;
}

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

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class ChocolateMilkShake implements MilkShake{
String brand;
Integer iceCubes;

public ChocolateMilkShake (String brand, Integer iceCubes){
this.brand = brand;
this.iceCubes = iceCubes;
}
@Override
public Integer getIceAmount () {
return this.iceCubes;
}

@Override
public String getBrand () {
return this.brand;
}

@Override
public String toString () {
return super.toString();
}
}
11 changes: 11 additions & 0 deletions pattern/src/com/premaseem/milkshake/MilkShake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.premaseem.milkshake;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public interface MilkShake {
Integer getIceAmount();
String getBrand();
}
Loading