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

All about code

  • 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

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

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

Recent Posts

  • How to Create a Deep or a Shallow Copy in Java
  • Spring Cloud: How to Implement Service Discovery (Part II)
  • Spring Cloud: How to Implement Service Discovery (Part I)
  • How to Better Understand Java HashMap
  • Spring Cloud: How to Deal with Microservice Configuration (Part II)

TAGS

Java Spring Boot Spring Cloud

Footer

Privacy Policy Cookie Policy Terms and Conditions

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress