abstract factory pattern (example & implementation in java)

Post on 10-Aug-2015

134 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Abstract Factory Pattern to the Rescue

Presented to:Mr. Mohammad Samiullah

Presented by:Salimullah Saqib (18)

Jubayer Al Mahmud (26)

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

The problem

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.

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.

The Problem Just Got Real !

Deal with it!

Are you having new chefs or execution?

Design Pattern• A general reusable solution to a commonly

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

Types of Design Pattern

1. Behavioral

2. Structural

3. Creational

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]

Abstract Factory VS Factory Method

• An extension of Factory Method

• Create objects without being concerned about the class

• Allows more types of objects [4]

General Structure

Recall the Problem

Solution

Add Factory

Add Abstract Factory

Problem Solved!!!

Implementation

Product interface 1

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

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

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

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");• } • }

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

Mexican Nachos Ordered");• }• }

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");

}

}

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

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;• }

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; • } • }

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;• }

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;• }

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;

}

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; • } • }

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(); • }• }

Output

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]

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]

Disadvantage• More difficult to read and understand

• Isolates concrete classes

• Consisteny among the products harder to enforce [7]

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]

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

top related