Lab Report
On
Design Pattern
(Singleton, Decorator and Strategy)
Course Code: CSE - 418
Course Title: Software Engineering & Design Pattern Lab
Submitted to:
Plabon Talukder
Lecturer
Department of Computer Science & Engineering
Metropolitan University, Bangladesh
Submitted by:
Shajib Chanda
ID: 222-115-172
Batch: 57th (E)
Department of Computer Science & Engineering
Metropolitan University, Bangladesh
Date of Submission: 01February 2025
1. Singleton Design Pattern-
Theory:
The Singleton Pattern ensures that a class has only one instance and provides a global point
of access to it. This is useful in scenarios where multiple instances could cause
inconsistencies, such as logging, configuration management, or database connections.
Source: geeksforgeeks
Key Features:
• Ensures only one instance of a class is created.
• Provides global access to that instance.
• Uses a private constructor to restrict instantiation.
Code:
Output Example:
Output:
2. Decorator Design Pattern-
Theory:
The Decorator Pattern dynamically adds responsibilities to objects without modifying their
structure. Instead of subclassing, it wraps an object with new functionality, making it more
flexible and reusable.
Source: geeksforgeeks
Key Features:
• Allows behavior to be added dynamically at runtime.
• Follows the Open-Closed Principle (open for extension, closed for modification).
• Uses composition rather than inheritance.
Code:
Output:
3. Strategy Design Pattern-
Theory:
The Strategy Pattern allows defining a family of algorithms, encapsulating them in separate
classes, and making them interchangeable at runtime. This promotes flexibility by enabling
dynamic selection of the desired algorithm.
Source: geeksforgeeks
Key Features:
• Defines a set of interchangeable algorithms.
• Promotes loose coupling between algorithm and client.
• Enhances maintainability and scalability.
Code:
Output: