The decorator pattern is widely used to dynamically add responsibilities to objects without altering their existing structure. This example demonstrates how to enhance a basic home security sensor incrementally: from a simple audio-alerting sensor to a comprehensive system with vibrations and strobe lights.
Abstract Class: HomeSensor
Defines the core interface for security sensor functionality. The abstract class has a method triggerAlert() to handle alert logic when an event is detected.
package security.sensor;
public abstract class HomeSensor {
public void triggerAlert() {
// Default alert behavior (can be overridden by subclasses or decorators)
}
}
Concrete Component: BasicSensor Implements the base sensor functionality. When an event is detected, it simply sends an audio alert.
package security.sensor;
public class BasicSensor extends HomeSensor {
@Override
public void triggerAlert() {
System.out.println("BasicSensor: Emits an audio alert to notify residents of a security event.");
}
}
Decorator Base Class: SensorDecorator Acts as the base class for all decorators. It holds a reference to a HomeSensor instance and delegates the triggerAlert() method to it. This class ensures compatibility between concrete components and decorators.
package security.sensor;
public class SensorDecorator extends HomeSensor {
private HomeSensor targetSensor;
public SensorDecorator(HomeSensor sensor) {
if (sensor != null) {
this.targetSensor = sensor;
} else {
this.targetSensor = new BasicSensor(); // Default to basic sensor if null
}
}
@Override
public void triggerAlert() {
targetSensor.triggerAlert();
}
}
Concreet Decorator 1: MotionVibrationDecorator Enhances a sensor by adding vibration alerts to the existing alert behavior. Extends SensorDecorator and overrides triggerAlert() to add vibration logic after calling the base functionality.
package security.sensor;
public class MotionVibrationDecorator extends SensorDecorator {
public MotionVibrationDecorator(HomeSensor sensor) {
super(sensor);
}
@Override
public void triggerAlert() {
super.triggerAlert();
System.out.println("MotionVibrationDecorator: Adds strong vibration alerts alongside existing notifications.");
}
}
Concrete Decorator 2: ComprehensiveAlertDecorator The most advanced decorator, adding strobe light alerts to the vibration and audio alerts. Extends SensorDecorator and overrides triggerAlert() to include strobe light logic.
package security.sensor;
public class ComprehensiveAlertDecorator extends SensorDecorator {
public ComprehensiveAlertDecorator(HomeSensor sensor) {
super(sensor);
}
@Override
public void triggerAlert() {
super.triggerAlert();
System.out.println("ComprehensiveAlertDecorator: Integrates strobe light alerts with existing audio and vibration.");
}
}
Client Class: SensorEnhancementDemo Demonstrates how to use the decorator pattern to upgrade a basic sensor incrementally. Creates a basic sensor, then wraps it with decorators to add vibration and strobe light capabilities.
package security.sensor;
public class SensorEnhancementDemo {
public static void main(String[] args) {
// Step 1: Create a basic security sensor
HomeSensor basicSensor = new BasicSensor();
System.out.println("=== Deploying Basic Security Sensor ===");
basicSensor.triggerAlert();
System.out.println();
// Step 2: Upgrade to include vibration alerts
HomeSensor vibrationEnhancedSensor = new MotionVibrationDecorator(basicSensor);
System.out.println("=== Upgrading to Motion + Vibration Sensor ===");
vibrationEnhancedSensor.triggerAlert();
System.out.println();
// Step 3: Upgrade to comprehensive alert system
HomeSensor comprehensiveSensor = new ComprehensiveAlertDecorator(vibrationEnhancedSensor);
System.out.println("=== Upgrading to Comprehensive Alert System ===");
comprehensiveSensor.triggerAlert();
}
}