Design Patterns

Builder Design Pattern

Builder pattern separate the construction of a object from its representation so that the same construction process can create different representations.

The Builder Pattern is comprised of four components: a builder interface, a concrete builder, a director and a product.

Builder is an interface which is used to define all the steps to create a product.

   //This is the Builder Interface  
public interface IBlogBuilder
{
void BuildSendEmail();
void BuildShare();
Blog blog { get; }
}

ConcreteBuilder is a class which implements the Builder interface to create complex product.

 //This is the Post Concrete Builder class  
public class PostBuilder : IBlogBuilder
{
Blog post;
public PostBuilder()
{
post = new Blog("New Post 1");
}
public void BuildSendEmail()
{
post.SendEmail();
}
public void BuildShare()
{
post.Share();
}
}
//This is the Page Concrete Builder class
public class PageBuilder : IBlogBuilder
{
Blog page;
public PageBuilder()
{
page = new Blog("New Page 1");
}
public void BuildSendEmail()
{
page.SendEmail();
}
public void BuildShare()
{
page.Share();
}
}

Product is a class which defines the parts of the complex object which are to be generated by the builder pattern.

 //This is the Product  
public class Blog
{
string Title;
public Blog(string name)
{
Title = name;
}
public void SendEmail()
{
//TO DO : Send Email Logic
}
public void Share()
{
//TO DO : Send Share Logic
}
}

Director is a class which is used to construct an object using the Builder interface.

 //This is the Director class  
public class BuildBlog
{
public void ConstructBlog(IBlogBuilder blogBuilder)
{
blogBuilder.BuildSendEmail();
blogBuilder.BuildShare();
}
}

Now when we look at the client code we can see how clean it is to create any product.

 using System;  
namespace dotnetfundamentals.BuilderPattern
{
class Program
{
static void Main(string[] args)
{
//This will create instance of Director
BuildBlog newPost = new BuildBlog();

//Initializes Builder class
IBlogBuilder blogBuilder = null;

//This will create New Post
blogBuilder = new PostBuilder();
newPost.ConstructBlog(blogBuilder);

//This will create New Page
blogBuilder = new PageBuilder();
newPost.ConstructBlog(blogBuilder);
}
}
}

Design Patterns

ProtoType Design Pattern

Prototype pattern gives us a way to create new objects from the existing instance of the object.

That is, we clone the existing object with its data. By cloning any changes to the cloned object does not affect the original object value.

In below example i have used ProtoTypePattern which need to be clonned. This can be achieved by using MemberWiseClone method.

 Create ProtoTypePattern.cs class file and add below code.

 using System;  
namespace dotnetfundamentals.DesignPatterns
{
//This is the class to be Cloned
public class ProtoTypePattern
{
public string sName { get; set; }
public ProtoTypePattern getClone()
{
//MemberWiseClone function will cereate complete new cpoy of object
return (ProtoTypePattern)this.MemberwiseClone();
}
}
}

Add below code in Program.cs file.

 using System;  
namespace dotnetfundamentals.DesignPatterns
{
class Program
{
static void Main(string[] args)
{
//This is the first copy of the the object
ProtoTypePattern protoType1 = new ProtoTypePattern();
//This will set the value for the first object.
protoType1.sName = "Patel Himen";
//This is the second copy of object
ProtoTypePattern protoType2 = new ProtoTypePattern();
//This will create copy of the object
protoType2.getClone();
}
}
}
Design Patterns

Abstract Factory Design Pattern

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

In below example i have used two different types of Logging can be stored – Error Logger and Info Logger. I have used abstract factory pattern to abstract away object creation details from client

Create ExceptionAbstractFactory.cs class file and add below code.

using System;
namespace dotnetfundamentals.AbstractFactory
{
//Common interface for concrete classes
public interface ILogger
{
void Log();
}
public class Error : ILogger
{
public void Log()
{
Console.Write("Error Logger");
}
}
public class Info : ILogger
{
public void Log()
{
Console.Write("Info Logger");
}
}
//Abstract factory and factory code snippet
public abstract class AbstractFactory
{
static public ILogger WriteLog(LogType logType)
{
if (logType == LogType.Error)
{
return ErrorFactory.Error();
}
else if (logType == LogType.Info)
{
return InfoFactory.Info();
}
return null;
}
}
public class ErrorFactory : AbstractFactory
{
static public ILogger Error()
{
return new Error();
}
}
public class InfoFactory : AbstractFactory
{
static public ILogger Info()
{
return new Info();
}
}
public enum LogType
{
Error,
Info
}
}

Add below code in Program.cs file.

using System;
namespace dotnetfundamentals.AbstractFactory
{
class Program
{
static void Main(string[] args)
{
ILogger iLogger;
iLogger = AbstractFactory.WriteLog(LogType.Error);
iLogger.Log();
}
}
}

Design Patterns

Factory Pattern with example – Exception logger

The intent of Factory Mehod is Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

In below example i have used two different places where exception can be stored – File system and Database. The logs are created depending on the LogType specified by the client.

Create ExceptionFactory.cs class file and add below code.

using System;
namespace dotnetfundamentals.AbstractFactory
{
//Creating the Abstract Logger
public interface ILog
{
void Log();
}
public class FileLogger : ILog
{
public void Log()
{
Console.Write("Log to File");
}
}
public class DBLogger : ILog
{
public void Log()
{
Console.Write("Log to DB");
}
}
//Creating the Concrete Factories
public class LogTypeChecker
{
static public ILog WriteLog(LogType logType)
{
ILog objLog = null;
if(logType == LogType.File)
{
objLog = new FileLogger();
}
else if (logType == LogType.Database)
{
objLog = new DBLogger();
}
return objLog;
}
}
public enum LogType
{
File,
Database
}
}

Add below code in Program.cs file.

using System;
namespace dotnetfundamentals.AbstractFactory
{
class Program
{
static void Main(string[] args)
{
ILog objLogger = LogTypeChecker.WriteLog(LogType.File);
objLogger.Log();
}
}
}