KEMBAR78
design_pattern.pptx design_pattern design_pattern | PPTX
Design Patterns in Software Architecture:
Singleton Pattern
Dr. Najmul
Hassan
Pattern Overview
▶ Design patterns capture the essence of proven solutions.
▶ Patterns offer a reusable design structure to solve
specific problems.
▶ They help designers decide if the pattern fits the current
work, can be reused, or serve as a guide for creating new
patterns.
Pattern Name: Singleton Pattern
▶ The Singleton Pattern ensures a single instance of a
class.
▶ It controls access to this one instance.
Intent of Singleton Pattern
▶ The Singleton Pattern restricts instantiation to only
one object.
▶ Ensures consistent access to this one object.
Motivation: Problem in E-commerce
▶ Imagine an e-commerce website where each user has
one shopping cart.
▶ All pages (product, checkout) need to access the same
cart instance.
▶ Without Singleton, multiple cart instances could be
created, confusing users.
Applicability
▶ Use Singleton when a class needs to be uniquely
accessible.
▶ Applicable for shared resources (like a logging or
configuration manager).
▶ In our example, the cart must be accessible across
multiple pages but remain a single instance.
Structure of Singleton Pattern
▶ A class has:
▶ A private static instance variable.
▶ A static method to access or create the instance.
▶ A private constructor to prevent external
instantiation.
Participants
▶ Singleton class (e.g., ‘ShoppingCart‘)
▶ Instance variable to store the single instance.
▶ Method to access the instance ensuring only one cart per
user
.
Collaborations
▶ Multiple classes (e.g., Product Page, Checkout Page)
access the ‘ShoppingCart‘ instance.
▶ They call the Singleton instance using a method
like ‘ShoppingCart::getInstance()‘.
Consequences of Singleton Pattern
▶ Advantages: Ensures consistency and saves memory.
▶ Trade-offs: Singleton can be difficult to test due to its
global state.
▶ Design Forces: Balancing single access with easy
accessibility.
Related Patterns
▶ Factory Pattern: Used to manage object creation.
▶ Monostate Pattern: Provides a similar effect by sharing
state among multiple instances.
Wrap-Up
▶ Singleton is a pattern that ensuresone instance of a
class.
▶ Useful for consistent shared resources.
▶ Practical for things like shopping carts in e-commerce.
Questions?

design_pattern.pptx design_pattern design_pattern

  • 1.
    Design Patterns inSoftware Architecture: Singleton Pattern Dr. Najmul Hassan
  • 2.
    Pattern Overview ▶ Designpatterns capture the essence of proven solutions. ▶ Patterns offer a reusable design structure to solve specific problems. ▶ They help designers decide if the pattern fits the current work, can be reused, or serve as a guide for creating new patterns.
  • 3.
    Pattern Name: SingletonPattern ▶ The Singleton Pattern ensures a single instance of a class. ▶ It controls access to this one instance.
  • 4.
    Intent of SingletonPattern ▶ The Singleton Pattern restricts instantiation to only one object. ▶ Ensures consistent access to this one object.
  • 5.
    Motivation: Problem inE-commerce ▶ Imagine an e-commerce website where each user has one shopping cart. ▶ All pages (product, checkout) need to access the same cart instance. ▶ Without Singleton, multiple cart instances could be created, confusing users.
  • 6.
    Applicability ▶ Use Singletonwhen a class needs to be uniquely accessible. ▶ Applicable for shared resources (like a logging or configuration manager). ▶ In our example, the cart must be accessible across multiple pages but remain a single instance.
  • 7.
    Structure of SingletonPattern ▶ A class has: ▶ A private static instance variable. ▶ A static method to access or create the instance. ▶ A private constructor to prevent external instantiation.
  • 8.
    Participants ▶ Singleton class(e.g., ‘ShoppingCart‘) ▶ Instance variable to store the single instance. ▶ Method to access the instance ensuring only one cart per user .
  • 9.
    Collaborations ▶ Multiple classes(e.g., Product Page, Checkout Page) access the ‘ShoppingCart‘ instance. ▶ They call the Singleton instance using a method like ‘ShoppingCart::getInstance()‘.
  • 10.
    Consequences of SingletonPattern ▶ Advantages: Ensures consistency and saves memory. ▶ Trade-offs: Singleton can be difficult to test due to its global state. ▶ Design Forces: Balancing single access with easy accessibility.
  • 11.
    Related Patterns ▶ FactoryPattern: Used to manage object creation. ▶ Monostate Pattern: Provides a similar effect by sharing state among multiple instances.
  • 12.
    Wrap-Up ▶ Singleton isa pattern that ensuresone instance of a class. ▶ Useful for consistent shared resources. ▶ Practical for things like shopping carts in e-commerce. Questions?