CASHBOX SAVERS PROMO!!!

Our savers are our partners, without you there is no us. So this month of November, we have decided to reward many of our savers. All through the month, grow your savings balance to at least ₦5,000…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Factory Design Pattern

This is coming under the creational pattern as Singleton. What the factory design does is it acts like a factory when we want to get an instance from different classes. It gives whatever the objects according to our needs.

We can imagine that as a garment factory from the real world. In garment factory, they make different kind of clothes like blouses, frocks, t-shirts, shirts and all other things. If we need the blouses we ask from the garment fro blouses and it has responsibility to provide items which we required. We don’t know how to make those clothes. But we can just ask and use the made clothes only communicating with the garment factory. You can imagine Factory design pattern also with your cases.

As an example, if you need to get the colours. You may have a Colour Factory which may provide any colour. Then simply, you can come and ask from the Colour Factory for the colour which you want.

So, today we are going to look at how we can use factory design pattern like these situations with the coding example.

Yeah, we have to accept both of them are coming under creation design pattern. But that doesn’t mean these are same. If those are same, why we need to have two patterns. right?

In Singleton, it ensures it will provide only one instance from a class while Factory pattern defines an interface/ abstract class for creating required objects.

Further arguments should not be passed when we use singleton. In factory, user pass the argument saying which kind of instance you want to get. You don’t see instantiation logic in factory pattern.

Sure, why not?

I will provide the real world use case in java and also demo example with interesting use case.

1.When it comes to java, NumberFormat use the factory pattern.

How NumberFormat works

In here, you can see we can get different kind of instances like IntegerInstance, NumberInstance and so on.

2.Next, I’m going to provide a demo for Factory design pattern.

Everyone may like to eat pizza right? Most of the people like to eat pizza surely. Then what about we map our confused factory pattern to make a pizza. That’s sounds cool, right?

Sometimes, you may have complicated things to learn. Don’t try hard. Be smart instead of trying hard. It will give more effective results for your learning.

So we are going to map buying a pizza into factory design pattern. But how do we do that?

Background for demo from real world:

Please understand this well. Then you will have a good idea about factory pattern in the end.

Let’s take Pizza shop which provides 3 kind of pizza such as Chicken Pizza, Cheese Pizza and Vegetable Pizza. Form people to people choices are different. So shop will provide 3 categories from pizza.

Normally when shop make pizza they use different ingredients for different pizza.

In our case, to have simplicity, Let’s assume we have only 3 ingredients such as Vegetable, Chicken and Pizza.

If someone needs Vegetable Pizza -> It will have only Vegetables.

If someone needs Chicken Pizza -> It will have Vegetables and Chicken.

If someone needs Cheese Pizza -> It will have all Vegetables, Chicken and Cheese as ingredients.

When we buy a pizza, we don’t need to make pizza and no need to have a knowledge how to make pizza. Instead of doing that, we have pizza shop, we can go and ask for whatever pizza we want from their pizza categories. Then they will provide the pizza which we asked.

How to map buying pizza into Factory Pattern:

1.In this case, what is the factory?

Pizza shop is our factory. (Because they make pizza and provide us)

2.What do we need to do , to have a pizza?

We have to order pizza saying which pizza we want.(Ex: Chicken Pizza)

3.After you order, what will happen?

Pizza shop will make pizza with relevant ingredients. (Ex: They will make Chicken Pizza using Vegetables and Chicken)

Then, shop will provide the Chicken Pizza.

Now you have idea, Our Factory is Pizza shop. Then we order some pizza. Pizza shop make pizza in the shop that means pizza factory make pizza according to the requirement. If requirement is Chicken Pizza, ingredients are Vegetable and Chicken. Shop will provide the Chicken Pizza to you which is instance in factory pattern case.

Pizza Factory Demo:

You have a main class(Pizz.java) a to go and ask for some pizza. That is our main class. Then you order pizza and PizzaFactory will take the argument and create new instance from required Pizza(Ex. Chicken Pizza, then it will create a instance from ChickenPizza.) Inside the Chicken Pizza class we have makePizza() and it will add ingredients like Vegetable and Chicken. Then it will return the Chicken Pizza instance.

According to our requirement we will make folder called Ingredients and PizzaCategory.

Project structure

1.Ingredient.java is abstarct class which will be extended by CheeseI, ChickenI and VegetableI. (I will provide example, assuming Chicken Pizza is user required one)

2.Now, we have Ingredients. Then we want to have classes to create Pizza adding ingredients. For that, we will use abstract class called PizzaCategory.java which will be extended by ChickenPizza, CheesePizza and VegetablePizza classes.

What we do is, we create abstract class and it will have a ArrayList which can keep list of Ingredients. Using makePizza() method, each and every Pizza classes will make the pizza with ingredients.

In ChickenPizza.java, we will add vegetable and chicken as ingredients. To do that, we will add new instances from VegetableI and ChickenI to ingredientsList arraylist.

3.Now we have all ingredients classes and pizza category classes. Now we can use our factory and create new Pizza.

In PizzaFactory we have orderPizza() method. It will take the user requirement and check what user wants.

We can simply use enum to keep the Pizza codes such as VEG,CHICKEN and CHEESY.

If user ask for Chicken Pizza, in PizzaFactory, it will go to switch case and return new ChickenPizza() instance.

That’s what happen in PizzaFactory.

4.Finally, we can have our main application which will allow to ask for pizza.

After running the main application, we can have results like this.

You can see here, First time user ask for Chicken Pizza. So user enter 2. Then It will say “Dear Customer, Your Required Pizza has-> [[Vegetable, Chicken]]” because Chicken Pizza makes with Vegetable and Chicken.

In second time, user asked for a Cheese Pizza and it will provide instance which made with Vegetable, Chicken, Cheese.

I hope you will have a good idea now what the factory pattern is and how we can implement the factory design pattern.

Code examples:

Add a comment

Related posts:

5 must know Excel functions and why you should learn them

Every Data professional at some point needs to use Excel, many will be comfortable with other tools (PowerBI, Alteryx) and languages (SQL, Python) but Excel remains the backbone for many businesses…

What Makes Some Men So Obsessed With Barely Legal Girls?

It was a 30-year-old man showing off his 18-year-old girlfriend. She was sitting on a couch, with her knees drawn up to her chest, lipsyncing to baby voices. All cringe of that aside, it’s not…

dias comuns e coisas que me fazem falta

nos dias comuns estou acordando um pouco mais cedo que nas últimas semanas. alguns dias produtivos estão dando espaço ao ócio que estava sentindo por conta da pandemia. fiquei bastante tempo parado e…