Skip to content

The Decorator is a structural pattern used to add new responsibilities to an object dynamically without extending functionality. That lets you dynamically change the behavior of an object at runtime by wrapping it in an object of a decorator class.

Notifications You must be signed in to change notification settings

Dynotum/Decorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Decorator

The Decorator is a structural pattern used to add new responsibilities to an object dynamically without extending functionality. That lets you dynamically change the behavior of an object at runtime by wrapping it in an object of a decorator class.

Decorators are used for adding some behavior that is not part of the core functionality to all interface methods. Decorator pattern perfectly suits for the following tasks:

  • caching the work results;
  • measuring the execution time of methods;
  • user access control.

The decorator pattern has the following components:

  • Component is the interface for the objects that will get new responsibilities from the decorators;
  • Concrete Component defines objects which implement the Component interface and will get new responsibilities from the concrete decorators;
  • Decorator has reference to the Component and overridden component methods;
  • Concrete Decorator extends Decorator class and adds new functions, properties or state without creating new classes;

image

The decorator pattern in JDK:

Streams: java.io package;
Collections: java.util package.

The decorator pattern is applicable in the following cases:

  • When you want to add new properties and functions to the object dynamically;
  • When the extension of classes is superfluous.

About

The Decorator is a structural pattern used to add new responsibilities to an object dynamically without extending functionality. That lets you dynamically change the behavior of an object at runtime by wrapping it in an object of a decorator class.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages