Code Monkey home page Code Monkey logo

dp-decorator's Introduction

Decorator Design Pattern Example

Although this project is not based on any particular tutorial, I mainly used the Refactoring Guru explanation as reference when implementing the code.

Project Overview

There is a base class representing a business entity (MIPOccurrence). This entity has base operations that define its basic behaviour (IOccurrence). However, we assume a dashboard needs additional operations to generate charts. Instead of adding these chart operations into the entity, we extended the entity by using the decorator pattern (ChartDecoratorMIPOccurrence).

Class Structure

  • IOccurrence represents the set of common operations for any MIPOccurrence entity. This interface is used by all classes in this project;

  • MIPOccurrence is the base class. It implements the IOccurrence operations. This represents the basic behaviour of an occurrence;

  • BaseDecorator is the superclass that defines a decorator. As MIPOccurrence, it implements IOccurrence operations. Thus, both MIPOccurrence & BaseDecorator are IOccurrence. Classes that add new behaviour to MIPOccurrence must extend BaseDecorator. This class has a parameter in the constructor, used to wrap the base class (MIPOccurrence);

  • ChartDecoratorMIPOccurrence represents the operations necessary for creating dashboard charts. This class extends BaseDecorator and, therefore, is also an IOccurrence. To extend MIPOccurrence behaviour, it wraps MIPOccurrence by using the constructor parameter, on the class instantiation. The ChartDecoratorMIPOccurrence method, getChartData() uses MIPOccurrence base operations, implemented from IOccurrence, to generate data for the chart.

Flow Structure

The Figure shows the test execution. A MIPOccurrence is firstly created. Its method getOccurrences() is called to test the entity is working. Second, the ChartDecoratorMIPOccurrence class is instantiated, as it is BaseDecorator, due to the inheritance.

During the instantiation, ChartDecoratorMIPOccurrence wraps the MIPOccurrence instance. Finally, the ChartDecoratorMIPOccurrence method, getChartData(), uses the getOccurrences() method to extract data for generating the chart.

Notice that this diagram is deeper than necessary, as it shows both BaseDecorator and IOccurrence - but, it may help understand better the inheritance role.

Project Setup

mvn clean test

dp-decorator's People

Contributors

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