abstract factory design pattern with realtime example

abstract factory design pattern with realtime example: what is an abstract factory design pattern?  It is similar to the factory method design pattern. we need to use this pattern when we have multiple factories. there will be a grouping of factories defined in this pattern. factory method pattern is a subset of abstract factory design pattern. we are excited to explain this concept with code. but we need to go through some theory. They have the same advantages as factory patterns. abstract factory relies on object composition whereas the factory method deals with inheritance.

For example, we want to have a WFH setup at our home. for that we need a comfortable chair. the chair can be made up of wood or steel. if we want a wood chair, we will go to a carpenter and pay him to make a chair for us. if we want a steel chair, we will go to a ready-made furniture showroom to buy it.  we have two choices in front of us to buy a chair.

  1. chair type factory
  2. color factory

assembling the CPU, monitor, mouse, and keyboards as different factories. we will one abstract factory on top of these all factories.

Let’s combine these two factories into one abstract factory ( chair factory). the abstract factory contains a set of factories. when we have a different type of products, this abstract design pattern is useful.

Let’s jump into code for a better understanding of the abstract factory design pattern.

abstract factory design pattern with realtime example:

public class AbstractFactoryDesignPatternExample {

public static void main(String[] args) {

typeFactory tfactory = FactoryProducer.getChairTypeFactory(); 
colorFactory cfactory = FactoryProducer.getColorFactory(); 
Chair woodChair=tfactory.chairType("WOOD");
woodChair.chairtype();
color chairColor =cfactory.fillColor("BROWN");
chairColor.chaircolor();

}

}
interface Chair
{
void chairtype();
}
interface color
{
void chaircolor();
}
class Woodchair implements Chair
{

@Override
public void chairtype() {
System.out.println("it is wood chair....");

}

}
class Steelchair implements Chair
{

@Override
public void chairtype() {
System.out.println("it is Steel chair....");

}

}
class Whitefill implements color
{

@Override
public void chaircolor() {
System.out.println("white color coated");

}

}
class Brownfill implements color
{

@Override
public void chaircolor() {
System.out.println("Brown color coated");

}

}

abstract class colorFactory
{
abstract color fillColor(String color); // color is one factory
}
abstract class typeFactory
{
abstract Chair chairType(String type);
}
class ChairColorFactory extends colorFactory
{

@Override
color fillColor(String color) {
if(color.equals("BROWN")) {
return new Brownfill();
}
if(color.equals("WHITE")) {
return new Whitefill();
}
return null;
}

}
class chairTypeFactory extends typeFactory
{

@Override
Chair chairType(String type) {
if(type.equals("WOOD")) {
return new Woodchair();
}
if(type.equals("STEEL")) {
return new Steelchair();
}
return null;
}

}
class FactoryProducer
{
public static colorFactory getColorFactory()
{
return new ChairColorFactory();

}
public static typeFactory getChairTypeFactory()
{
return new chairTypeFactory();

}
}

Please feel free to drop your comments. this may not be the best example of the abstract factory design pattern. feel free to drop your best examples in the comment section. thanks 🙂

 


Posted

in

by

Tags:

Comments

One response to “abstract factory design pattern with realtime example”

  1. […] to create concrete objects. it will help the system for easier modifications in the future. Abstract factory: In addition to the factory method, how instances are created and combined into one unit. it is […]

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started