abstract factory pattern (example & implementation in java)

40
Abstract Factory Pattern to the Rescue Presented to: Mr. Mohammad Samiullah Presented by: Salimullah Saqib (18) Jubayer Al Mahmud (26)

Upload: jubayer-rony

Post on 10-Aug-2015

134 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Abstract Factory Pattern (Example & Implementation in Java)

Abstract Factory Pattern to the Rescue

Presented to:Mr. Mohammad Samiullah

Presented by:Salimullah Saqib (18)

Jubayer Al Mahmud (26)

Page 2: Abstract Factory Pattern (Example & Implementation in Java)

Outline• Real life problem• Design pattern• Types of design pattern• Abstract Factory Pattern• Abstract Factory Pattern VS Factory Method• General structure• Recall the problem• Solution• Implementation• Application• Advantage• Disadvantage• Conclusion• References

Page 3: Abstract Factory Pattern (Example & Implementation in Java)

The problem

Page 4: Abstract Factory Pattern (Example & Implementation in Java)

The Problem• You are a multinational restaurant owner.

You have only one chef to cook Chinese food.

• What if your customer is Admiral General Aladeen?

• He wants to get Chinese food cooked by Chinese chef, Indian food cooked by Indian chef and Mexican food cooked by a Mexican chef.

Page 5: Abstract Factory Pattern (Example & Implementation in Java)

The Problem• To meet the demand you have to add new

items to your menu and new chefs.• Obviously a Chinese chef will cook

Chinese food, but Aladeen wants him to cook Mexican and Indian items too.

Page 6: Abstract Factory Pattern (Example & Implementation in Java)

The Problem Just Got Real !

Page 7: Abstract Factory Pattern (Example & Implementation in Java)

Deal with it!

Page 8: Abstract Factory Pattern (Example & Implementation in Java)

Are you having new chefs or execution?

Page 9: Abstract Factory Pattern (Example & Implementation in Java)

Design Pattern• A general reusable solution to a commonly

occurring problem• Template for how to solve a problem• Already solved problem• Saves time [1]

Page 10: Abstract Factory Pattern (Example & Implementation in Java)

Types of Design Pattern

1. Behavioral

2. Structural

3. Creational

Page 11: Abstract Factory Pattern (Example & Implementation in Java)

Abstract Factory Design Pattern• Provides a way to encapsulate a group of

individual factories [2]• Independent families of products can be

used• Reveals interfaces, hides implementation

[3]

Page 12: Abstract Factory Pattern (Example & Implementation in Java)

Abstract Factory VS Factory Method

• An extension of Factory Method

• Create objects without being concerned about the class

• Allows more types of objects [4]

Page 13: Abstract Factory Pattern (Example & Implementation in Java)

General Structure

Page 14: Abstract Factory Pattern (Example & Implementation in Java)

Recall the Problem

Page 15: Abstract Factory Pattern (Example & Implementation in Java)

Solution

Page 16: Abstract Factory Pattern (Example & Implementation in Java)

Add Factory

Page 17: Abstract Factory Pattern (Example & Implementation in Java)

Add Abstract Factory

Page 18: Abstract Factory Pattern (Example & Implementation in Java)

Problem Solved!!!

Page 19: Abstract Factory Pattern (Example & Implementation in Java)

Implementation

Page 20: Abstract Factory Pattern (Example & Implementation in Java)

Product interface 1

• public interface ChineseFood• {• void ChineseRice();• void ChineseNoodles();• }

Page 21: Abstract Factory Pattern (Example & Implementation in Java)

Product interface 2• public interface MexicanFood• {• void MexicanNachos();• }

Page 22: Abstract Factory Pattern (Example & Implementation in Java)

Product interface 3• public interface IndianFood• {• void IndianCurry();• void IndianKebab();• }

Page 23: Abstract Factory Pattern (Example & Implementation in Java)

Concrete product class 1• public class ChinesePack implements ChineseFood• {• public void ChineseRice() • {• System.out.println("Chinese Food - Package 1 - Chinese Rice

Ordered");• }• public void ChineseNoodles() {• System.out.println("Chinese Food - Package 2 - Chinese Noodles

Ordered");• } • }

Page 24: Abstract Factory Pattern (Example & Implementation in Java)

Concrete product class 2• public class MexicanPack implements MexicanFood• {• public void MexicanNachos()• {• System.out.println("Mexican Food – Packag 1 -

Mexican Nachos Ordered");• }• }

Page 25: Abstract Factory Pattern (Example & Implementation in Java)

Concrete product class 3public class IndianPack implements IndianFood

{

public void IndianCurry()

{

System.out.println("Indian Food - Package 1 - Indian Curry Ordered");

}

public void IndianKebab()

{

System.out.println("Indian Food - Package 2 - Indian Kebab Ordered");

}

}

Page 26: Abstract Factory Pattern (Example & Implementation in Java)

Abstract factory• public interface Kitchen• {• ChineseFood getchineseFood();• MexicanFood getMexicanFood();• IndianFood getIndianFood();• int Chinese=1;• int Mexican=2;• int Indian=3;• }

Page 27: Abstract Factory Pattern (Example & Implementation in Java)

Concrete factory 1• public class Chef1 implements Kitchen {• private int order;• public Chef1(int order) {• this.order=order;• }• public ChineseFood getchineseFood() {• switch(order) {• case Chinese: • System.out.println("Order for Chef 1");• return new ChinesePack();• }• return null;• }

Page 28: Abstract Factory Pattern (Example & Implementation in Java)

Concrete factory 1(cont)• public MexicanFood getMexicanFood() {• switch(order) {• case Mexican: • System.out.println("Order for Chef 1");• return new MexicanPack();• }• return null;• }• public IndianFood getIndianFood() {• switch(order) {• case Indian: • System.out.println("Order for Chef 1");return new IndianPack(); • }• return null; • } • }

Page 29: Abstract Factory Pattern (Example & Implementation in Java)

Concrete factory 2• public class Chef2 implements Kitchen {• private int order;• public Chef2(int order) {• this.order=order;• }• public ChineseFood getchineseFood() {• switch(order) {• case Chinese: • System.out.println("Order for Chef 2");• return new ChinesePack();• }• return null;• }

Page 30: Abstract Factory Pattern (Example & Implementation in Java)

Concrete factory 2 (cont.)

• public class Chef2 implements Kitchen {• private int order;• public Chef2(int order) {• this.order=order;• }• public ChineseFood getchineseFood() {• switch(order) {• case Chinese: • System.out.println("Order for Chef 2");• return new ChinesePack();• }• return null;• }

Page 31: Abstract Factory Pattern (Example & Implementation in Java)

Concrete factory 3public class Chef3 implements Kitchen {

private int order;

public Chef3(int order) {

this.order=order;

}

public ChineseFood getchineseFood() {

switch(order) {

case Chinese:

System.out.println("Order for Chef 3");

return new ChinesePack();

}

return null;

}

Page 32: Abstract Factory Pattern (Example & Implementation in Java)

Concrete factory 3 (cont.)• public MexicanFood getMexicanFood() {• switch(order) {• case Mexican: • System.out.println("Order for Chef 3");• return new MexicanPack();• }• return null;• }• public IndianFood getIndianFood() {• switch(order) {• case Indian: • System.out.println("Order for Chef 3");return new IndianPack(); • }• return null; • } • }

Page 33: Abstract Factory Pattern (Example & Implementation in Java)

Main• public class main• {• public static void main(String args[])• {• Kitchen kitchen= new Chef1(Kitchen.Chinese);• kitchen.getchineseFood().ChineseRice();• Kitchen kitchen1= new Chef1(Kitchen.Chinese);• kitchen1.getchineseFood().ChineseNoodles(); • Kitchen kitchen2= new Chef1(Kitchen.Indian);• kitchen2.getIndianFood().IndianCurry(); • Kitchen kitchen3= new Chef2(Kitchen.Mexican);• kitchen3.getMexicanFood().MexicanNachos(); • Kitchen kitchen4= new Chef3(Kitchen.Indian);• kitchen4.getIndianFood().IndianKebab(); • }• }

Page 34: Abstract Factory Pattern (Example & Implementation in Java)

Output

Page 35: Abstract Factory Pattern (Example & Implementation in Java)

Application• To create families of dependent objects

without specifying their concrete classes

• A hierarchy that encapsulates: many possible "platforms", and the construction of a suite of "products"

• When the ‘new’ operator considered harmful [5]

Page 36: Abstract Factory Pattern (Example & Implementation in Java)

Advantage• Isolates the concrete classes that are

generated

• Implementing classes are not needed to be known at the client side

• Any factory can be added under the abstract factory [6]

Page 37: Abstract Factory Pattern (Example & Implementation in Java)

Disadvantage• More difficult to read and understand

• Isolates concrete classes

• Consisteny among the products harder to enforce [7]

Page 38: Abstract Factory Pattern (Example & Implementation in Java)

Conclusion• Encapsulates a group of individual

factories that have a common theme

• The factories of object are hidden to client

• Concrete implementation can be interchanged without changing code [8]

Page 39: Abstract Factory Pattern (Example & Implementation in Java)

Referenes• [1]- https://en.wikipedia.org/wiki/Software_design_pattern• [2]-http://en.wikipedia.org/wiki/Abstract_factory_pattern• [3]- http://www.slideshare.net/guestcb0002/abstract-factory-pattern?

related=11 • [4]-https://www.youtube.com/watch?v=mIq-R6f6m30• [5]-https://sourcemaking.com/design_patterns/abstract_factory• [6]- https://sourcemaking.com/design_patterns/abstract_factory• [7]http://www.tutorialspoint.com/design_pattern/abstract_factory_pattern.htm• [8]-https://sourcemaking.com/design_patterns/abstract_factory

Page 40: Abstract Factory Pattern (Example & Implementation in Java)