What is Decorator Pattern?
Decorator pattern is a structural design pattern that attaches additional behaviors to an object dynamically. Decorators provide a flexible extension reason by composition rather than subclassing (inheritance).
When to use it?
Use Decorator pattern when you want to construct an object by adding small behaviors at runtime.
UML diagram
Decorator class uses composition and inheritance, it's crucial to understand their intent.
In Decorator pattern, we use the same type for both components & decorators. Decorator composites Component object to get behavior, that is, obtaining fields or methods defined in Component object. While Decorator inherits (extends) Component so that Decorator object can be declared as Component object.
Decorator pattern achieves open-closed principle, namely, open for extension and closed for modification. It's easy to add components or decorators. for instance, if you want to add another concrete decorator, you just need to create a class representing it and extends Decorator class.
Example
Imagine we're developing a system for an ice cream shop. The shop has various ice creams and toppings. The system needs to display an ice cream description (including its toppings) and cost.
- Components (ice creams) and Decorators (toppings) have common interface IceCream class, they both declare as IceCream object.
- Each concrete ice cream overrides cost method since prices are different for each.
- Topping class provides interface for concrete toppings and holds reference to an IceCream.
- If system needs another topping, say caramel source, what you need to do is just creating CaramelSource class which extends Topping class.
Implementation in Java
IceCream class:
// Component class public abstract class IceCream { public String description = "Unknown ice cream"; public String getDescription() { return description; } public abstract double cost(); }
ChocolateIceCream class:
// Concrete component class public class ChocolateIceCream extends IceCream { public ChocolateIceCream() { description = "ChocolateIceCream"; } @Override public double cost() { return 1.99; } }
Topping class:
// Base decorator class public abstract class Topping extends IceCream { public IceCream iceCream; // All subclasses (concrete decorator classes) need to implement getDescription method, // by declaring this method as abstract, we enforce all subclasses to implement this method public abstract String getDescription(); }
MapleNuts class:
// Concrete decorator class public class MapleNuts extends Topping { public MapleNuts(IceCream iceCream) { this.iceCream = iceCream; } @Override public String getDescription() { return iceCream.getDescription() + ", MapleNuts"; } @Override public double cost() { return iceCream.cost() + .30; } }
PeanutButterShell class:
// Concrete decorator class public class PeanutButterShell extends Topping { public PeanutButterShell(IceCream iceCream) { this.iceCream = iceCream; } @Override public String getDescription() { return iceCream.getDescription() + ", PeanutButterShell"; } @Override public double cost() { return iceCream.cost() + .30; } }
Client class:
public class Client { public static void main(String[] args) { IceCream iceCream = new ChocolateIceCream(); System.out.println(iceCream.getDescription() + ", $" + iceCream.cost()); iceCream = new MapleNuts(iceCream); System.out.println(iceCream.getDescription() + ", $" + iceCream.cost()); iceCream = new PeanutButterShell(iceCream); System.out.println(iceCream.getDescription() + ", $" + iceCream.cost()); } }
Output:
ChocolateIceCream, .99 ChocolateIceCream, MapleNuts, .29 ChocolateIceCream, MapleNuts, PeanutButterShell, .59
You can check all the design pattern implementations here.
GitHub Repository
P.S.
I'm new to write tech blog, if you have advice to improve my writing, or have any confusing point, please leave a comment!
Thank you for reading :)
The above is the detailed content of Decorator Pattern. For more information, please follow other related articles on the PHP Chinese website!

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
