GithubHelp home page GithubHelp logo

decorator-pattern's Introduction

DecoratorPattern

Intent

Attach additional responsibilities to an object dynamically.

Decorators provide a flexible alternative to subclassing for extending functionality.

Explanation

Implement a list menu with description and prices.

he open the current implementation of the menu and found this implementation:

alt text

So he added all the new options as a subclass under Coffee and SuperClass So the result is:

alt text

the problems in this implementation:

this is an overcrowded and condenser implementation.

If you want to add a new category or new coffee type you will create its own Class and implement it.

What happens if the milk price will change?! you will change in all cost method in all coffee classes. (open for modification)

#The Core Implementation

alt text

  • component can be used by each own or wrapped by a decoration.
  • ConcreteComponent: it is the subclass that can be decorated by any decoration item.
  • Decorator implements same interface or abstract class as the component it is going to decorate.
  • each decorator HAS-A (wraps) a component, which means that the decorator hold a reference variable of the component is going to decorate it.
  • note: the wrappedObject: coffee in each subClass Of the decorator just for simplification the implementation in the code below

#Our coffee-shop implementation

alt text

we met the SOLID Principles by this implementation into:

  • we didn't modify the coffee parent class.
  • it is simple to add new decorator like (soy) or add a new type of coffee by extending the parents classes and without modifying them, so it is open for extension, but closed for modification.

decorator-pattern's People

Contributors

crakdelpol avatar

Watchers

 avatar  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.