Giter Site home page Giter Site logo

decorator-pattern's Introduction

Decorator-Pattern

  • The Decorator Design Pattern is a structural pattern that allows you to dynamically add additional behavior or responsibilities to objects without modifying their structure.
  • It is useful when you need to extend the functionality of a class in a flexible and reusable way.
  • The pattern involves creating a set of decorator classes that wrap the original class and provide additional features.

Implementation

  • Let's consider a real-world example where we have a basic iOS application that provides different types of beverages (e.g., coffee , decafCoffe).
  • We want to offer various extras or toppings (e.g., Soy, Caramel) that can be added to the beverage.
  • Instead of creating separate classes for each combination of the beverage and toppings, we'll use the Decorator pattern.

Pros & Cons

✅ Positive aspects:

1️⃣ Flexibility: The Decorator pattern allows us to add or remove decorations dynamically at runtime without modifying the original classes, making it easy to mix and match functionalities.

2️⃣ Reusability: Decorators are independent and can be reused in different contexts, providing different combinations of behavior to different objects.

3️⃣ Open/Closed Principle: The pattern follows the Open/Closed Principle, as it allows extending the behavior of classes without modifying their code, reducing the risk of introducing bugs in existing functionality.

❌ Negative aspects:

1️⃣ Complexity: The Decorator pattern can lead to a large number of classes when multiple decorators are used, potentially making the codebase harder to manage.

2️⃣ Order of wrapping: The order in which decorators are applied can affect the final result, which may lead to unintended behavior if not carefully managed.

3️⃣ Maintenance: If the base component's interface changes, it can impact the decorators, requiring modifications in multiple classes.

decorator-pattern's People

Contributors

eng-oday avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.