
2. Design Patterns, Factory, Abstract, Singleton Pattern
Design Patterns: Factory, Abstract, Singleton Pattern
Design patterns are a critical aspect of software engineering, offering solutions to common problems encountered in software design. They can increase code maintainability, readability, and modifiability by providing tested, proven development paradigms.
Definition of Design Patterns
A design pattern provides a general, reusable solution to a common software design problem. It's not a finished design that can be converted directly into code, but a template for solving a problem that can be used in many different situations.
Major components of a pattern are:
- Name: The handle we use to describe a design problem, its solutions, and consequences.
- Problem: Describes when to apply the pattern.
- Solution: Describes the elements that make up the design, their relationships, responsibilities, and collaborations.
- Result: The consequences, both good and bad, of applying the pattern.
Types of Design Patterns
There are primarily three types of design patterns:
- Creational Design Patterns: They deal with object creation mechanisms, trying to create objects suitable to the situation.
- Structural Design Patterns: They deal with the composition of classes or objects.
- Behavioral Design Patterns: They are concerned with communication between objects.
This note focuses on three creational design patterns: Factory, Abstract, Singleton.
Factory Method
The Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created. This pattern helps to connect concrete products with the client code without enforcing the client code to depend on the product classes.
Example in C#:
public abstract class Logistic
{
public abstract ITransport CreateTransport();
public void PlanDelivery()
{
ITransport transport = CreateTransport();
transport.Deliver();
}
}
public class RoadLogistic : Logistic
{
public override ITransport CreateTransport()
{
return new Truck();
}
}
public class SeaLogistic : Logistic
{
public override ITransport CreateTransport()
{
return new Ship();
}
}
Abstract Factory
The Abstract Factory pattern is a creational design pattern that lets you produce families of related objects without specifying their concrete classes. It provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Example in C#:
public interface IFurnitureFactory
{
IChair CreateChair();
ISofa CreateSofa();
ITable CreateTable();
}
public class ModernFurnitureFactory : IFurnitureFactory
{
public IChair CreateChair()
{
return new ModernChair();
}
public ISofa CreateSofa()
{
return new ModernSofa();
}
public ITable CreateTable()
{
return new ModernTable();
}
}
public class VictorianFurnitureFactory : IFurnitureFactory
{
public IChair CreateChair()
{
return new VictorianChair();
}
public ISofa CreateSofa()
{
return new VictorianSofa();
}
public ITable CreateTable()
{
return new VictorianTable();
}
}
Singleton Pattern
The Singleton pattern is a creational design pattern that ensures a class has only one instance while providing a global access point to this instance. It restricts the instantiation of a class to a single instance and provides a global point of access to it.
Example in C#:
public class Singleton
{
private static Singleton _instance;
private static readonly object _lock = new object();
private Singleton() {}
public static Singleton Instance
{
get
{
lock (_lock)
{
if (_instance == null)
{
_instance = new Singleton();
}
return _instance;
}
}
}
}
In the example above, the lock
keyword ensures that if the method is accessed by multiple threads simultaneously, only one can work with it at the same time. The first thread to acquire the lock, holds it until the method is finished. As a result, the Singleton instance is truly unique.
Conclusion
Design patterns serve as templates that software developers can use to solve common problems when designing an application or system. Creational design patterns like the Factory, Abstract, and Singleton methods provide solutions for common issues related to object creation. Understanding these patterns can greatly enhance the quality of software design.
Reference
The content in this document is based on the original notes provided in Azerbaijani. For further details, you can refer to the original document using the following link:
Original Note - Azerbaijani Version