0% found this document useful (0 votes)
38 views15 pages

Software Design & Architecture

Uploaded by

zz.sarwar12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views15 pages

Software Design & Architecture

Uploaded by

zz.sarwar12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Software Design & Architecture

(SDA)

DESIGN PATTERNS

Software Planning Software Design


Software
and Requirement (Architectural &
Development
analysis Detailed)
1
Outline
1. Creational Design Pattern  covered
2. Structural Design Patterns  this lecture
i. Façade
ii. Décorator
iii. Composite
iv. Adapter
v. Flyweight
vi. Proxy
3. Behavioral Design Patterns  Next Lecture

2
30/30
Structural Patterns
Facade provides an interface to collections of objects

Decorator allows a user to add new functionality to an existing


object without altering its structure

Composite represents trees of objects

Adapter works as a bridge between two incompatible interfaces

Flyweight gains the advantages of using multiple instances


while minimizing space penalties

Proxy avoids calling expensive operations unnecessarily


9/30

2 . Decorator Design Pattern


Allows a user to add new functionality to an existing object
without altering its structure

Creates a decorator class which wraps the original class and


provides additional functionality keeping class methods signature
intact.

Add responsibilities to an object at runtime.


2 . Decorator Design Pattern
Decorator is a structural design pattern that lets you attach new behaviors
to objects by placing these objects inside special wrapper objects that
contain the behaviors.
This type of design pattern comes under structural pattern as this pattern
acts as a wrapper to existing class.

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.

Using the Decorator Pattern allows us to add optional features (add-ons)


to coffee orders dynamically without altering the core coffee classes. This
promotes code flexibility, scalability, and maintainability as new add-ons
can be easily introduced and combined with different types of coffee
orders.

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

Circle of red border


Shape: Circle
Border Color: Red

Rectangle of red border


Shape: Rectangle
Border Color: Red
12
Characteristics of the Decorator Pattern
This pattern promotes flexibility and extensibility in software systems by
allowing developers to compose objects with different combinations of
functionalities at runtime.
It follows the open/closed principle, as new decorators can be added
without modifying existing code, making it a powerful tool for building
modular and customizable software components.
The Decorator Pattern is commonly used in scenarios where a variety of
optional features or behaviors need to be added to objects in a flexible
and reusable manner, such as in text formatting, graphical user
interfaces, or customization of products like coffee or ice cream.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy