Abstract Factory Method Design Patterns For Dart & Flutter

0
50

Abstract Factory design pattern is one of the Creational designs. Abstract Factory design is practically like Factory Pattern and is considered as one more layer of reflection over factory design. Abstract Factory designs work around a super-factory facility that makes different factories.
Abstract factory design execution furnishes us with a system that permits us to make protests that follow a general pattern. So at runtime, the abstract factory is combined with any ideal substantial factory which can make objects of the selected type.

This article will explore the Abstract Factory Method Design Patterns For Flutter. We will perceive how to execute a demo program and we are going to learn about how we can use it in your flutter applications.

Table Of Contents ::

What Is Abstract Factory Method?

Advantages of Abstract Factory Pattern

Implementation

Conclusion



What Is Abstract Factory Method?

Characterize an interface or abstract class for making groups of related (or subordinate) objects yet without indicating their substantial sub-classes

That implies Abstract Factory lets a class returns a factory of classes. Thus, this is the explanation that Abstract Factory Pattern is one level higher than the Factory Pattern. This factory class returns various subclasses given the info given and the factory class utilizes an if-else or change proclamation to accomplish this.

From the get-go, it appears to be confounding yet when you see the execution, it’s truly simple to get a handle on and comprehend the minor distinction between Factory and Abstract Factory patterns.

An Abstract Factory Pattern is also known as Kit.

How about we see the GOFs portrayal of the Abstract Factory Pattern :

  • > AbstractFactory:- Proclaims an interface of interaction for tasks that make abstract product objects.
  • > ConcreteFactory:- Executes the operations pronounced in the Abstract Factory to make substantial product objects.
  • > Product:- Characterizes a product object to be made by comparing substantial factories and executes the AbstractProduct interface.
  • > Client:- Utilizes connection points proclaimed by AbstractFactory and AbstractProduct classes.

Abstract Factory gives connection points to making groups of related or subordinate objects without indicating their substantial classes.

Client programming makes a substantial execution of the abstract factory and afterward utilizes the nonexclusive connection points to make substantial items that are essential for the group of objects.

Advantages of Abstract Factory Pattern:

  • > Isolation of concrete classes: The Abstract Factory pattern assists you with controlling the classes of objects that an application makes. Since a factory exemplifies the obligation and the most common way of making product objects, it disconnects clients from execution classes.
  • > Exchanging Product Families quickly: The class of a substantial factory shows up just a single time in an application, that is where it’s launched. This makes it simple to change the substantial factory an application utilizes. It can utilize different product setups just by changing the substantial factory.
  • > Promoting consistency among products: When product objects in a family are intended to cooperate, an application genuinely should utilize objects from just a single family at a time.

Implementation:

Lets create an abstract class Color.

abstract class Color
{
void paint();
}

Presently make substantial classes executing a similar class Shape.

class RedColor implements Color {
@override
void paint() {
print("RedColor");
}
}class BlueColor implements Color {
@override
void paint() {
print("BlueColor");
}
}class Yellow implements Color {
@override
void paint() {
print("Yellow");
}
}class Black implements Color {
@override
void paint() {
print("Black");
}
}

Now create an Abstract class to get factories for Color Objects.

abstract class AbstractFactory
{
Color getColor(String colorType);
}

Presently make Factory classes extending out AbstractFactory to create an object of the substantial class based of given data.

class ColorFactory extends AbstractFactory
{
@Override
Color getColor(String colorType)
{
if (colorType == "YELLOW") {
return new Yellow();
} else if (colorType == "BLACK") {
return new Black();
}
return null;
}
}
class RedColorFactory extends AbstractFactory
{
@Override
Color getColor(String colorType)
{
if (colorType == "YELLOW") {
return new RedYellow();
} else if (colorType == "BLACK") {
return new RedBlack();
}
return null;
}
}

Presently make a Factory generator/maker class to get factories plants by passing data like Color.

class FactoryProducer
{
static AbstractFactory getFactory(bool red)
{
if (red) {
return new RedColorFactory();
} else {
return new ColorFactory();
}
}
}

Utilize the FactoryProducer to set AbstractFactory up to get factories of substantial classes by passing data like sort.

class AbstractFactoryPatternDemo
{
static void main()
{

AbstractFactory colorFactory = FactoryProducer.getFactory(false);

Color color1 = colorFactory.getColor("YELLOW");
color1.paint(); //Prints "Yellow"
Color color2 = colorFactory.getColor("BLACK");
color2.paint(); //Prints "Black"
AbstractFactory colorFactory1 = FactoryProducer.getFactory(true);
Color color3 = colorFactory1.getColor("YELLOW");
color3.paint(); //Prints "RedYellow"
Color color4 = colorFactory1.getColor("BLACK");
color4.paint(); //Prints "RedBlack"

}
}

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 Method Design Patterns For Dart and Flutter in your projectsSo 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.


Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on FacebookGitHubTwitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


LEAVE A REPLY

Please enter your comment!
Please enter your name here