Category: designPatterns
-
Pattern: Factory
Description A factory is a function or method that returns objects of a varying prototype or class from some method call,which is assumed to be new Example in .NET : Factory [code lang=”csharp”] using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Internal; using System.Data.Common; using System.Globalization; using System.Net; using System.Web.Mvc; namespace Factory; internal class FactoryDemo { public static…
-
Pattern: Prototype
Description It is used when the type of objects to create is determined by a prototypical instance,which is cloned to produce new objects Example in .NET : ICloneable [code lang=”csharp”] using System; namespace Prototype; class Parent : ICloneable { public int Age { get; set; } public Child MyChild { get; set; } public object…
-
Pattern: Singleton
Description Singleton pattern restricts the instantiation of a class to one object. It is used when you want to have one instance of a class that is shared across the application. Example in .NET : Singleton [code lang=”csharp”] using System.Data.OleDb; namespace Singleton; /// <summary> /// ///sealed class Singleton ///{ /// private Singleton() { } ///…
-
Pattern: Strategy
Description Strategy pattern allows a client to choose from a family of algorithms at runtime. It is used when the client expects to have multiple algorithms and wants to choose one of them at runtime. Example in .NET : Strategy [code lang=’csharp’] using System; using System.Collections.Generic; namespace Strategy; internal class StrategyDemo { public static void…
-
Pattern: Visitor
Description Visitor pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying the structures. Example in .NET : Visitor [code lang=’csharp’] using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; namespace Visitor; internal…
-
Pattern: Adapter
Description Adapter design pattern allows the interface of an existing class to be used as another interface.It is often used to make existing classes work with others without modifying their source code. Examples in .NET : SQLiteDataAdapter [code lang=”csharp”] namespace Adaptor; internal class SQLiteDataAdapterDemo { /// <summary> ///Adaptee – Command ///Target – DataTable ///Adapter –…
-
Pattern: Builder
Description The intent of the Builder design pattern is to separate the construction of a complex object from its representation Examples in .NET : UriBuilder [code lang=”csharp”] namespace Builder; static class UriBuilderDemo { public static void UriMod() { //start example 1 var uri = new Uri("https://msprogrammer.serviciipeweb.ro/category/friday-links/"); var b = new UriBuilder(uri); //changing part b.Scheme =…
-
Pattern: Iterator
Description Iterator design pattern allows to traverse a container and access the container’s elements. Example in .NET : DirectoryEnumerable [code lang=”csharp”] namespace Iterator; internal class DirectoryEnumerableDemo { public static void DirectoryEnumerableFiles(int nrMaxFiles) { var count = 0; //what if we called Directory.GetFiles foreach (var file in Directory.EnumerateFiles(@"c:\","*.*",SearchOption.AllDirectories)) { count++; if (count > nrMaxFiles) break; Console.WriteLine(file);…
-
Pattern: NullObject
Description Instead of returning null,use an object which implements the expected interface,but whose method body is empty. Examples in .NET : EmptyFolder [code lang=”csharp”] using System; using System.IO; namespace NullObject; internal class EmptyFolderDemo { public static void DemoWithCreateNewFolder() { //start example 1 var env = Environment.CurrentDirectory; var guid = Guid.NewGuid().ToString("X"); var fldEmpty = Path.Combine(env,guid); //create…