Posts

Showing posts from June, 2023

Factory Method pattern in C#

Hi there! Welcome to my blog where I share my passion for C# and design patterns. Today I’m going to talk about one of the most popular and useful design patterns: the Factory Method pattern.😎 What is the Factory Method pattern? The Factory Method pattern is a creational design pattern that defines an interface for creating an object but lets subclasses decide which class to instantiate.  This pattern lets a class defer instantiation to subclasses . In other words, the Factory Method pattern allows us to create objects without exposing the creation logic to the client. Instead of using a constructor or a simple factory, we use an abstract method (or an interface) that returns an object of a specific type. The subclasses can then override this method and return different types of objects based on some criteria. Why use the Factory Method pattern? The Factory Method pattern is useful when we want to: Decouple the creation of objects from their usage Encapsulate the object creation l...

Singleton pattern im C#

Hello, C# lovers! πŸ‘‹ In this post, I’m going to talk about one of the most popular and useful design patterns in C#: the  Singleton  pattern. 🌟 The Singleton pattern is a way to ensure that only one instance of a class exists in the program and that it can be accessed globally by other classes. This can be useful when you need to share some state or resources across the application, such as a database connection, a configuration file, a logger, etc. 😎 But how do we implement the Singleton pattern in C#? There are many ways to do it, but they all follow some common guidelines: The class constructor should be  private  and  parameterless , so that it cannot be instantiated from outside the class. The class should be  sealed , so that it cannot be inherited by other classes. The class should have a  private static  field that holds the reference to the single instance of the class. The class should have a  public static  property or metho...