Skip to main content

Posts

Understanding the Memento Design Pattern in C#

  The Memento design pattern is a behavioral pattern that allows you to capture and externalize an object's internal state without violating encapsulation, so the object can be restored to this state later. This pattern is particularly useful for implementing undo-redo functionality. Understanding the State Design Pattern in C# Let's consider a scenario where we have a  TextEditor  class that allows text editing and supports undo functionality. The  TextEditor  can save its state (the current text) and restore it to a previous state. Example without Memento Design Pattern using System; using System.Collections.Generic; namespace WithoutMementoPattern { // Originator class class TextEditor { public string Text { get ; private set ; } public void SetText ( string text ) { Text = text; Console.WriteLine( $"Current Text: {Text} " ); } } // Caretaker class class TextEdi...

Top 10 Questions and Answers on Dependency Injection

  Dependency Injection (DI) is a design pattern used to implement Inversion of Control (IoC), allowing a class to receive its dependencies from an external source rather than creating them itself. DI helps in making code more modular, testable, and maintainable by decoupling the creation and management of dependencies from the class that uses them. Top 10 Questions and Answers on Abstract Class vs. Interface in C# 1. What is Dependency Injection (DI)? Dependency Injection is a design pattern that allows an object to receive other objects it depends on, known as dependencies, rather than creating them itself. This is achieved by injecting these dependencies through constructors, properties, or method parameters. 2. What are the benefits of using Dependency Injection? Decoupling: Reduces tight coupling between classes. Testability: Makes unit testing easier by allowing the injection of mock dependencies. Maintainability: Simplifies code maintenance and updates by managing dependen...

Top 10 Questions and Answers on Abstract Class vs. Interface in C#

In object-oriented programming, both abstract classes and interfaces are used to achieve abstraction. They define contracts for what a class should do but differ significantly in how they define these contracts and how they can be used. Top 10 Questions and Answers on Static vs Singleton in C# 1. What is an abstract class in C#? An abstract class is a class that cannot be instantiated and can include abstract methods (methods without implementation) as well as concrete methods (methods with implementation). It is meant to be inherited by other classes. public abstract class Animal { public abstract void MakeSound () ; public void Sleep () { Console.WriteLine( "Sleeping" ); } } public class Dog : Animal { public override void MakeSound () { Console.WriteLine( "Bark" ); } } // Usage Animal myDog = new Dog(); myDog.MakeSound(); // Output: Bark myDog.Sleep(); // Output: Sleeping 2. What is an inter...

Top 10 Questions and Answers on Static vs Singleton in C#

  In C#, both static classes and singleton patterns are used to ensure a single instance of a class is used throughout an application. However, they have different use cases, benefits, and limitations. Understanding the differences between them is crucial for making the right design decisions. Top 10 Questions and Answers on the Liskov Substitution Principle in C# 1. What is a static class in C#? A static class in C# is a class that cannot be instantiated. It contains only static members, meaning all methods, properties, and fields belong to the class itself rather than any object. public static class Utility { public static void PrintMessage ( string message ) { Console.WriteLine(message); } } // Usage Utility.PrintMessage( "Hello, Static Class!" ); 2. What is a singleton in C#? A singleton is a design pattern that restricts the instantiation of a class to a single instance. This is typically achieved by making the constructor private and provi...

Top 10 Questions and Answers on the Liskov Substitution Principle in C#

The Liskov Substitution Principle (LSP) is a fundamental principle in object-oriented design, part of the SOLID principles. It states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. In essence, a subclass should enhance, not break, the functionality of a superclass. Top 10 Questions and Answers on Delegates in C# 1. What is the Liskov Substitution Principle (LSP)? The Liskov Substitution Principle states that if S is a subtype of T , then objects of type T can be replaced with objects of type S without altering the desirable properties of the program. 2. Why is LSP important in object-oriented design? LSP ensures that a subclass can be used anywhere its parent class is expected without causing errors or unexpected behavior. It promotes the robustness and reliability of the software by ensuring that derived classes maintain the integrity of the base class. 3. What are the key characteristics of LSP? Behav...

Understanding the State Design Pattern in C#

  The State design pattern is a behavioral pattern that allows an object to change its behavior when its internal state changes. This pattern encapsulates state-specific behavior and transitions inside state objects, making it easy to add or modify states without altering the context object. Understanding the Template Method Design Pattern in C# Example without State Design Pattern Let's consider a scenario where we have a  Document  class that can be in different states: Draft, Moderation, and Published. Each state defines specific behavior for the  Publish  and  Edit  actions. using System; namespace WithoutStatePattern { // Context class class Document { public string State { get ; private set ; } public Document () { State = "Draft" ; } public void Publish () { if (State == "Draft" ) { Console.WriteLine( "Document is in ...

Understanding the Visitor Design Pattern in C#

  The Visitor design pattern is a behavioral pattern that allows adding further operations to objects without modifying them. It achieves this by allowing you to define a new operation on the objects by creating a visitor object that implements the operation. The visitor object then "visits" each element to perform the operation. Example without Visitor Design Pattern Let's consider a scenario where we have different types of elements in a document (e.g.,  TextElement ,  ImageElement ,  TableElement ). We want to perform different operations (e.g., rendering, printing, exporting) on these elements without modifying their classes. using System; using System.Collections.Generic; namespace WithoutVisitorPattern { // Base class for elements abstract class Element { public abstract void Render () ; public abstract void Print () ; } // Concrete element for text class TextElement : Element { public str...