Skip to main content

Posts

Showing posts with the label behavioural pattern

Understanding the Mediator Design Pattern in C#

  The Mediator design pattern is a behavioral pattern that defines an object (mediator) that encapsulates how a set of objects interact. By centralizing the communication between objects, the Mediator pattern reduces the dependencies between them, promoting loose coupling. Understanding the State Design Pattern in C# Example Without Mediator Pattern Let's consider a scenario where we have a chat application with multiple users. Each user can send and receive messages. Instead of having users communicate directly with each other, we'll use a ChatRoom mediator to handle all interactions. using System; using System.Collections.Generic; namespace WithoutMediatorPattern { // User class class User { public string Name { get ; private set ; } private List<User> _contacts = new List<User>(); public User ( string name ) { Name = name; } public void AddContact ( User user ) {

Understanding the Command Design Pattern in C#

  The Command design pattern is a behavioral pattern that turns a request into a stand-alone object that contains all information about the request. This transformation allows for parameterizing methods with different requests, queuing or logging requests, and supporting undoable operations. Understanding the State Design Pattern in C# Let's consider a scenario with a text editor application that supports basic text operations like writing text and undoing the last operation. Example without Command Design Pattern using System; namespace WithoutCommandPattern { class TextEditor { public string Text { get ; private set ; } = "" ; public void Write ( string text ) { Text += text; Console.WriteLine( $"Text after write: {Text} " ); } public void UndoWrite ( string text ) { if (Text.EndsWith(text)) { Text = Text.Substring( 0 , Te

Understanding the Iterator Design Pattern in C#

  The Iterator design pattern is a behavioral pattern that provides a way to access elements of a collection sequentially without exposing its underlying representation. This pattern is useful for traversing different data structures in a uniform way. Understanding the Memento Design Pattern in C# Let's consider a scenario where we have a  Book  class and a  BookCollection  class. We want to iterate over the  BookCollection  without exposing its internal details. Example without Iterator Design Pattern using System; using System.Collections.Generic; namespace WithoutIteratorPattern { // Book class class Book { public string Title { get ; private set ; } public Book ( string title ) { Title = title; } } // Concrete collection class BookCollection { private readonly List<Book> _books = new List<Book>(); public int Count => _books.Count; public Book

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 TextEditorHis

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 Draft

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 string