• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Home
  • Contact Us
Logo

All about code

As an Amazon Associate, I earn from qualifying purchases.

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns

A Quick Explanation of The Java Decorator Pattern

wordpress blogging writing typing 923188

Introduction

The Java Decorator Pattern falls into the category of structural pattern and is meant to be used to dynamically extend the behavior of classes, overcoming the static nature of Java inheritance. The following section explains briefly the involved design.

The Java Decorator Pattern

The following picture shows how the Java Decorator Pattern works. An abstract decorator class is defined which extends the class that we want to “decorate”. Besides extending it, it also uses an internal instance of that class to implement all the non-abstract, overridden methods. That is to say, each of the non-abstract, overridden methods only executes the corresponding method of the parent class without doing anything else. The abstract methods then are implemented by the concrete class.

The Java Decorator Pattern
The Java Decorator Pattern

Using the above schema, we can assign the concrete decorator to any variable whose type is ToDecorateClass. This way wherever the variables with ToDecorateClass type are used, the extended behavior is in place.

Conclusion

The Decorator Pattern allows us to customize the functionalities of an existing piece of software in a dynamic way. In the following article, hosted by dzone.com, a possible use case is shown: https://dzone.com/articles/decorator-pattern-to-solve-messy-scenarios.

Primary Sidebar

Legal

Privacy Policy
Terms and Conditions

Social

  • Facebook
  • LinkedIn
  • Twitter

Advertisment

As an Amazon Associate, I earn from qualifying purchases.

Archives

  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • How to Better Understand Java HashMap
  • Spring Cloud: How to Deal with Microservice Configuration (Part II)
  • Java Map: How to Get Sorted Keys
  • Spring Cloud: How to Deal with Microservice Configuration (Part I)
  • Java Map: Preserve Insertion Order

TAGS

Java Spring Boot Spring Cloud

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress