Flutter Design Patterns: 3— Abstract Factory Method

Omer Shafique
4 min readNov 1, 2022

--

In this article, we will explore the Abstract Factory Method in Flutter. We will see how to implement a demo program and we are going to learn about how we can to use it in your flutter applications.

What Is a Factory Method?

How To Implement a Factory Method In Dart:

Conclusion

What Is Abstract Factory Method?

define an interface or abstract class for creating families of related (or dependent) objects but without specifying their concrete sub-classes

That means Abstract Factory lets a class returns a factory of classes. So, this is the reason that Abstract Factory Pattern is one level higher than the Factory Pattern.

The Abstract Factory design pattern is one of the Creational patterns. If you are familiar with factory design pattern, you will notice that we have a single Factory class. This factory class returns different subclasses based on the input provided and the factory class uses an if-else or switch statement to achieve this.

In the Abstract Factory pattern, we get rid of the if-else block and have a factory class for each sub-class. Then an Abstract Factory class will return the sub-class based on the input factory class.

At first, it seems confusing but once you see the implementation, it’s really easy to grasp and understand the minor difference between Factory and Abstract Factory pattern.

An Abstract Factory Pattern is also known as Kit.

Abstract factory pattern implementation provides us with a framework that allows us to create objects that follow a general pattern. So at runtime, the abstract factory is coupled with any desired concrete factory which can create objects of the desired type.
Let's see the GOFs representation of the Abstract Factory Pattern :

  • AbstractFactory: Declares an interface for operations that create abstract product objects.
  • ConcreteFactory: Implements the operations declared in the AbstractFactory to create concrete product objects.
  • Product: Defines a product object to be created by the corresponding concrete factory and implements the AbstractProduct interface.
  • Client: Uses only interfaces declared by AbstractFactory and AbstractProduct classes.

Abstract Factory provides interfaces for creating families of related or dependent objects without specifying their concrete classes.
Client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create concrete objects that are part of the family of objects.
The client does not know or care which concrete objects it gets from each of these concrete factories since it uses only the generic interfaces of their products.

How To Implement Abstract Factory Method In Dart:

Create an abstract class Shape.

abstract class Shape
{
void draw();
}

Now create concrete classes implementing the same class Shape.

class RoundedRectangle implements Shape {
@override
void draw() {
print("RoundedRectangle");
}
}
class RoundedSquare implements Shape {
@override
void draw() {
print("RoundedSquare");
}
}
class Rectangle implements Shape {
@override
void draw() {
print("Rectangle");
}
}
class Square implements Shape {
@override
void draw() {
print("Square");
}
}

Now create an Abstract class to get factories for Normal and Rounded Shape Objects.

abstract class AbstractFactory
{

Shape getShape(String shapeType);
}

Now create Factory classes extending AbstractFactory to generate an object of concrete class based on given information.

class ShapeFactory extends AbstractFactory
{
@Override
Shape getShape(String shapeType)
{
if (shapeType == "RECTANGLE") {
return new Rectangle();
} else if (shapeType == "SQUARE") {
return new Square();
}
return null;
}
}
class RoundedShapeFactory extends AbstractFactory
{
@Override
Shape getShape(String shapeType)
{
if (shapeType == "RECTANGLE") {
return new RoundedRectangle();
} else if (shapeType == "SQUARE") {
return new RoundedSquare();
}
return null;
}
}

Now create a Factory generator/producer class to get factories by passing information such as Shape.

class FactoryProducer
{
static AbstractFactory getFactory(bool rounded)
{
if (rounded) {
return new RoundedShapeFactory();
} else {
return new ShapeFactory();
}
}
}

Use the FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing information such as type.

class AbstractFactoryPatternDemo
{
static void main()
{

//get shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory(false);

//get an object of Shape Rectangle
Shape shape1 = shapeFactory.getShape("RECTANGLE");
//call draw method of Shape Rectangle
shape1.draw(); //Prints "Rectangle"
//get an object of Shape Square
Shape shape2 = shapeFactory.getShape("SQUARE");
//call draw method of Shape Square
shape2.draw(); //Prints "SQUARE"
//get shape factory
AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true);
//get an object of Shape RoundedRectangle
Shape shape3 = shapeFactory1.getShape("RECTANGLE");
//call draw method of Shape RoundedRectangle
shape3.draw(); //Prints "RoundedRectangle"
//get an object of Shape RoundedSquare
Shape shape4 = shapeFactory1.getShape("Square");
//call draw method of Shape RoundedSquare
shape4.draw(); //Prints "RoundedSquare"

}
}

Conclusion:

In the article, I have explained the basic structure of Abstract Factory Design Patterns For Dart and Flutter; you can modify this code according to your choice.

I hope this blog will provide you with sufficient information on Trying up the Abstract Factory Design Patterns For Dart and Flutter in your projects. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.

--

--