Software Design & Architecture
Software Design & Architecture
(SDA)
DESIGN PATTERNS
2
30/30
Structural Patterns
Facade provides an interface to collections of objects
5
Real-World Example of Decorator Design Pattern
Consider a video streaming platform where users can watch movies and TV
shows. Each video content may have additional features or options available,
such as subtitles, language preferences, video quality options, and audio
enhancements.
In this scenario, the base component is the video content itself, while the
decorators represent the various additional features that users can enable or
customize.
For example, a user might select the option to enable subtitles, change the
language of the audio track, or adjust the video quality settings.
Each of these options acts as a decorator that enhances the viewing
experience without altering the underlying video content.
By using the Decorator pattern, the streaming platform can dynamically apply
these additional features to the video content based on user preferences,
providing a customizable viewing experience.
6
Key Components of the Decorator Design Pattern
Component Interface: This is an abstract class or interface that defines
the common interface for both the concrete components and decorators.
It specifies the operations that can be performed on the objects.
Concrete Component: These are the basic objects or classes that
implement the Component interface. They are the objects to which we
want to add new behavior or responsibilities.
Decorator: This is an abstract class that also implements the Component
interface and has a reference to a Component object. Decorators are
responsible for adding new behaviors to the wrapped Component object.
Concrete Decorator: These are the concrete classes that extend the
Decorator class. They add specific behaviors or responsibilities to the
Component. Each Concrete Decorator can add one or more behaviors to
the Component.
7
Example
Suppose we are building a coffee shop application where customers can
order different types of coffee. Each coffee can have various optional add-
ons such as milk, sugar, whipped cream, etc. We want to implement a
system where we can dynamically add these add-ons to a coffee order
without modifying the coffee classes themselves.
8
Class Diagram of Decorator
9
Example OF DECORATOR
We're going to create a Shape interface and concrete classes
implementing the Shape interface. We will then create an abstract
decorator class ShapeDecorator implementing the Shape interface and
having Shape object as its instance variable.
RedShapeDecorator is concrete class implementing ShapeDecorator.
DecoratorPatternDemo, our demo class will use RedShapeDecorator to
decorate Shape objects.
10
Class diagram for decorator
11
Implementation steps
1. Create an interface (e.g. Shape.java)
2. Create concrete classes implementing the same interface( e.g.
Rectangle.java & Circle.java)
3. Create abstract decorator class implementing the Shape interface
(e.g. ShapeDecorator.java)
4. Create concrete decorator class extending the ShapeDecorator
class.
5. Use the RedShapeDecorator to decorate Shape objects.
Circle with normal border Shape:
6. Verify the output Circle
13
References
Chapter 7 “Software Engineering Design, Theory and practice”
https://www.geeksforgeeks.org/proxy-design-pattern/
https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm
14
15