Design patterns are reusable solutions to common problems in software design
. They provide a standard terminology and are specific to particular scenarios.
Singleton
: Ensures a class has only one instance and provides a global point of access to it.Factory Method
: Creates objects without specifying the exact class of object that will be created.Abstract Factory
: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.Builder
: Separates the construction of a complex object from its representation.Prototype
: Creates new objects by copying an existing object, known as the prototype.Adapter
: Allows incompatible interfaces to work together.Bridge
: Separates an object’s abstraction from its implementation so the two can vary independently.Composite
: Composes objects into tree structures to represent part-whole hierarchies.Decorator
: Adds responsibilities to objects dynamically.Facade
: Provides a simplified interface to a complex subsystem.Flyweight
: Reduces the cost of creating and manipulating a large number of similar objects.Proxy
: Provides a surrogate or placeholder for another object to control access to it.Chain of Responsibility
: Passes a request along a chain of handlers.Command
: Encapsulates a request as an object.Interpreter
: Implements a specialized language.Iterator
: Provides a way to access elements of a collection sequentially.Mediator
: Defines an object that encapsulates how a set of objects interact.Memento
: Captures and restores an object’s internal state.Observer
: Defines a one-to-many dependency between objects.State
: Allows an object to alter its behavior when its internal state changes.Strategy
: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.Template Method
: Defines the skeleton of an algorithm, deferring some steps to subclasses.Visitor
: Represents an operation to be performed on the elements of an object structure.Standardization
: Provide a standard terminology and are well known in the software development community, facilitating communication among developers
.Reusability
: Offer reusable solutions
that can be applied to common problems, saving time and effort in the development process.Best Practices
: Incorporate the best practices and principles of object-oriented design, helping developers create more robust and maintainable code
.Improved Design
: Help in designing systems that are more flexible, reusable, and easier to manage
and modify over time.Documentation and Maintenance
: Serve as a guide for documenting and understanding existing code
, which aids in maintaining and extending the system.The Singleton pattern ensures that a class has only one instance
and provides a global point of access to that instance
.
This is useful when exactly one object is needed to coordinate actions across the system.
The lock statement in C# is used to ensure that a block of code runs only by one thread at a time. It provides a mechanism for thread synchronization, which is essential in multi-threaded applications to avoid race conditions and ensure data integrity.
using System;public class Singleton{// Private static variable that holds the single instance of the classprivate static Singleton instance = null;// Lock synchronization objectprivate static readonly object padlock = new object();// Private constructor to prevent instantiation from outsideprivate Singleton(){}// Public static method to provide a global point of access to the instancepublic static Singleton Instance{get{// Double-check locking to ensure thread safetyif (instance == null){lock (padlock){if (instance == null){instance = new Singleton();}}}return instance;}}// Example method to demonstrate functionalitypublic void ShowMessage(){Console.WriteLine("Hello from the Singleton instance!");}}class Program{static void Main(string[] args){// Access the Singleton instance and call a method on itSingleton.Instance.ShowMessage();// Verify that only one instance is createdSingleton s1 = Singleton.Instance;Singleton s2 = Singleton.Instance;if (s1 == s2){Console.WriteLine("Both instances are the same.");}else{Console.WriteLine("Different instances.");}}}
The Repository pattern is a design pattern that mediates data access logic to business logic
by encapsulating the set of objects stored
in a database and the
operations performed over them. It provides a more object-oriented approach to accessing data and abstracts the details of data access, allowing the business
logic to interact with a more meaningful domain model rather than directly with the database.
public class Product{public int Id { get; set; }public string Name { get; set; }public decimal Price { get; set; }}
using System.Collections.Generic;public interface IProductRepository{IEnumerable<Product> GetAll();Product GetById(int id);void Add(Product product);void Update(Product product);void Delete(int id);}
using System.Collections.Generic;using System.Linq;public class ProductRepository : IProductRepository{private readonly List<Product> _products = new List<Product>();public IEnumerable<Product> GetAll(){return _products;}public Product GetById(int id){return _products.FirstOrDefault(p => p.Id == id);}public void Add(Product product){_products.Add(product);}public void Update(Product product){var existingProduct = _products.FirstOrDefault(p => p.Id == product.Id);if (existingProduct != null){existingProduct.Name = product.Name;existingProduct.Price = product.Price;}}public void Delete(int id){var product = _products.FirstOrDefault(p => p.Id == id);if (product != null){_products.Remove(product);}}}
class Program{static void Main(string[] args){IProductRepository productRepository = new ProductRepository();// Add some productsproductRepository.Add(new Product { Id = 1, Name = "Product 1", Price = 10.0m });productRepository.Add(new Product { Id = 2, Name = "Product 2", Price = 20.0m });// Retrieve and display all productsvar products = productRepository.GetAll();foreach (var product in products){Console.WriteLine($"Id: {product.Id}, Name: {product.Name}, Price: {product.Price}");}// Update a productvar productToUpdate = productRepository.GetById(1);if (productToUpdate != null){productToUpdate.Name = "Updated Product 1";productToUpdate.Price = 15.0m;productRepository.Update(productToUpdate);}// Delete a productproductRepository.Delete(2);// Retrieve and display all products againproducts = productRepository.GetAll();foreach (var product in products){Console.WriteLine($"Id: {product.Id}, Name: {product.Name}, Price: {product.Price}");}}}
The SOLID principles are a set of five design principles in object-oriented
programming that help developers create more maintainable, understandable, and flexible software.
should have only one reason to change
, meaning it should have only one responsibility or job.focus on a single task or functionality
. This makes the class easier to maintain and test, as changes to one responsibility won’t affect others.should be open for extension but closed for modification.
. This can be achieved through
inheritance, interfaces`, or other forms of polymorphism.subclass is used in place of a superclass, the program should still behave correctly
. This ensures that derived classes extend the base class without changing its fundamental behavior.Instead of having one large, complex interface, it's better to have multiple smaller, more specific interfaces
. This allows classes to implement only the methods they need, making the code more modular and easier to maintain.decoupling of software components
. By depending on abstractions rather than concrete implementations, the code becomes more flexible and easier to change or extend.Implement design patterns such as Factory, Singleton, Repository, and Dependency Injection to solve common problems in a standardized way. Design patterns help in writing code that is more flexible, reusable, and easier to maintain.
Quick Links
Legal Stuff
Social Media