Flutterexperts

Empowering Vision with FlutterExperts' Expertise
Explore Decorator Design Pattern In Dart

One of the well-known design patterns, the Decorator one, explains how to design flexible and reusable object-oriented software by resolving common design issues. We’ll cover the decorator design pattern, a structural design pattern often referred to as a wrapper.

This blog will Explore Decorator Design Pattern In Dart. We see how to execute a demo program in your applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.


Table Of Contents::

What is the Decorator Design Pattern?

When to use?

Code Implement

Drawbacks

Conclusion



What is the Decorator Design Pattern?:

“Design Patterns, Elements of Reusable Object-Oriented Software,” a book, dynamically assigns additional responsibilities to an object. When it comes to expanding functionality, decorators offer a flexible alternative to subclassing.

Let’s try to understand this with a real-world example that might make more sense: suppose you order a plain coffee from a coffee shop. Here, coffee acts as a class object. You want to spice up your coffee now. You request that some milk be added. The barista pours some milk into your coffee. Here, the milk enhances your coffee without altering it in any way.

You decide later that you also want some sugar. Sugar is added to your coffee by the barista. Another decorator is sugar. It enhances your coffee even more without altering the milk or coffee.

In this scenario, your coffee is the object, and milk and sugar are the decorators. To put it more succinctly, the Decorator design pattern does not alter the structure of objects but rather adds new functionality to them.

When to use?:

  • When would you like to add new responsibilities to an object transparently and dynamically?
  • Because there are too many separate extensions, subclassing is not a realistic way to increase functionality.
  • When would you like to assign tasks to objects in layers or phases?
  • When you need to add optional functionality but yet want to keep a class focused on a particular task.

Code Implement:

A basic coffee is called plain coffee. Asking the barista to add more milk or sugar is what ExtraMilkDecorator and ExtraSugarDecorator do. Without changing the original PlainCoffee, they can modify the coffee’s price and description.

abstract class Coffee {
  String get description;
  double get cost;
}

class PlainCoffee implements Coffee {
  @override
  String get description => 'Plain Coffee';

  @override
  double get cost => 60.0;
}

class CoffeeDecorator implements Coffee {
  final Coffee coffee;
  CoffeeDecorator(this.coffee);

  @override
  String get description => coffee.description;

  @override
  double get cost => coffee.cost;
}

class ExtraMilkDecorator extends CoffeeDecorator {
  ExtraMilkDecorator(super.coffee);

  @override
  String get description => '${coffee.description} + Extra Milk';

  @override
  double get cost => coffee.cost + 15.0;
}

class ExtraSugarDecorator extends CoffeeDecorator {
  ExtraSugarDecorator(super.coffee);

  @override
  String get description => '${coffee.description} + Extra Sugar';

  @override
  double get cost => coffee.cost + 20.0;
}

void main() {
  Coffee coffee = PlainCoffee();
  print("${coffee.description}: \$${coffee.cost}");

  coffee = ExtraMilkDecorator(coffee);
  print("${coffee.description}: \$${coffee.cost}");

  coffee = ExtraSugarDecorator(coffee);
  print("${coffee.description}: \$${coffee.cost}");
}

When we run the application, we ought to get the screen’s output like the underneath console output.

Plain Coffee: $60.0
Plain Coffee + Extra Milk: $75.0
Plain Coffee + Extra Milk + Extra Sugar: $95.0

Process finished with exit code 0

Drawbacks:

  • A complicated codebase that is difficult to maintain can result from overusing the Decorator pattern.
  • A more complex class hierarchy is produced by creating multiple minor classes for every new functionality.
  • It necessitates numerous small classes, all of which are fairly similar to one another, potentially making the design unduly complex.

Conclusion:

In the article, I have explained the Decorator Design Pattern basic structure in a dart; you can modify this code according to your choice. This was a small introduction to the Decorator Design Pattern On User Interaction from my side, and it’s working using Dart.

I hope this blog will provide you with sufficient information on trying the Explore Decorator Design Pattern In Dart in your projects. One of the most widely used design patterns for dynamically adding additional functionality to an object without altering its structure is the Decorator pattern.

It provides a versatile substitute for subclassing, especially when handling numerous independent extensions.ut it’s crucial to utilise this approach sparingly because excessive use might result in a complicated and challenging-to-maintain codebase. Despite these difficulties, the Decorator design can greatly improve your code’s readability and modularity when applied properly. 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.


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 Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

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 comment

Your email address will not be published. Required fields are marked with *.