0% found this document useful (0 votes)
11 views

C# Concepts 3

Uploaded by

musabinshabeer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C# Concepts 3

Uploaded by

musabinshabeer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

C# Concepts

Singleton vs. Static Classes


C#: Singleton vs. Static Classes

• Singleton and Static classes are both ways to implement globally


accessible functionalities, but they have distinct characteristics and
use-cases.
C#: Singleton Pattern

• Definition: The Singleton pattern ensures that a class has only one
instance and provides a global point of access to that instance.
Singleton Pattern : Characteristics
• Controlled Instance Creation: Only one instance exists throughout
the application's lifecycle.

• Lazy Initialization: The instance can be created when it's first


needed.

• Inheritance-Friendly: Can implement interfaces and be inherited


from, allowing for more flexible designs.

• State Management: Can maintain state across the application


Singleton Pattern - Example
public class Singleton
{
private static Singleton _instance;
private static readonly object _lock = new object();

// Private constructor to prevent external


instantiation
private Singleton() { }
Singleton Pattern - Example
// Public property to provide global access
public static Singleton Instance
{
get
{
if (_instance == null)
{
lock(_lock)
{
if (_instance == null)
_instance = new Singleton();
}
}
return _instance;
}
Singleton Pattern - Example
public void DoWork()
{
Console.WriteLine("Singleton instance is working.");
}
}

// Usage
class Program
{
static void Main(string[] args)
{
Singleton.Instance.DoWork();
}
}
Singleton Pattern - Example
Simple Factory Pattern - Advantages
• Controlled Access: Only one instance is created, ensuring
consistency.

• Lazy Loading: The instance is created only when needed, saving


resources.

• Testability: Can implement interfaces for easier mocking in tests.


Simple Factory Pattern -
Disadvantages
• Global State: Can lead to hidden dependencies and difficulties in
tracking state.

• Concurrency Issues: Requires careful handling in multithreaded


environments.

• Singleton as Anti-Pattern: Overuse can lead to tightly coupled


code.
C#: Static Classes

• Definition: Static classes cannot be instantiated and all their members


are static. They are often used for utility or helper functions.
Static Classes: Characteristics
• No Instance: Cannot create instances; members are accessed directly
via the class name.

• No Inheritance: Cannot implement interfaces or be inherited from.

• Statelessness Encouraged: Typically stateless, although static fields


can maintain state.

• Performance: Slightly faster access since no instance is needed.


Static Classes - Example
public static class MathHelper
{
public static int Add(int a, int b) => a + b;

public static int Subtract(int a, int b) => a - b;


}
Static Classes - Example
// Usage
class Program
{
static void Main(string[] args)
{
int sum = MathHelper.Add(5, 3);
Console.WriteLine($"Sum: {sum}");

int difference = MathHelper.Subtract(5, 3);


Console.WriteLine($"Difference: {difference}");
}
}
Static Classes - Example
Static Classes - Advantages

• Simplicity: Easy to use for stateless utility functions.

• Performance: Faster access as no instance is required.

• No Instantiation Overhead: Suitable for methods that don't


require object state.
Static Classes - Disadvantages
• Lack of Flexibility: Cannot implement interfaces, making them
harder to mock or replace in tests.

• Global State Risks: If static fields are used to maintain state, it


can lead to issues similar to singletons.

• No Inheritance: Limits design flexibility and reuse.


Comparison: Singleton vs. Static
Aspect Singleton Static Class

Instantiation One instance controlled by the class itself One instance controlled by the class itself
State Can maintain state through instance
Can maintain state through instance fields
Management fields
Inheritance Can implement interfaces and be Can implement interfaces and be inherited
inherited from from
Testability More testable via interfaces and mocking More testable via interfaces and mocking
Lazy Initialization Supports lazy initialization Supports lazy initialization
Usage Scenario When a single instance with state is
When a single instance with state is needed
needed
Choosing Between Singleton and
Static:
• Use Singleton:
• When you need a single instance that maintains state.
• When you need to implement interfaces or inherit from other classes.
• When you require lazy initialization.

• Use Static:
• For stateless utility or helper functions.
• When no instance-specific behavior is needed.
• When simplicity and performance are priorities.

You might also like