Showing posts with label design pattern. Show all posts
Showing posts with label design pattern. Show all posts

Abstract Factory Design Pattern

Abstract Factory Design Pattern
Creational pattern
Among the creational pattern, we will discuss how the Abstract Factory is useful in detail.
Abstract Factory
This is mostly useful when we are dealing where the application needs to be ported in different operating system. Create an abstract interface which it deals with the methods to be instantiated for the different operating system.
What could be the problem if you are not using the Abstract factory design pattern?
1. Code will not look good and maintenance is difficult
2. Porting to different operating system is a time consuming work
3. Abstraction will not be followed as we expected
What are the advantages of implementing the Abstract factory design pattern?
1. Code is easier to maintain
2. Porting to different operating system is easy and it could be extended. This means a new class needs to be instantiated and this will take care of handling the mechanism for new operating system
How to implement the Abstract factory design pattern?
Create base class with pure virtual function and inherit the child class based on the porting as required. Number of child classes to be inherited is based on the number of operating system to be ported. In the man() function as based on the operating system by using "#ifdef" the instantiation of the child class to the base class can be done.

Design Pattern Singleton design pattern

Design Pattern Singleton design pattern
Singleton Pattern:
It comes under the category of ‘Creational Pattern’.

Definition:

It allows a single instance of a class and provides global access to it.

Steps to implement a Singleton Pattern in C#:
1. Create a sealed class.
2. Declare a private variable for that class inside the class.
3. Make the constructor as private.
4. Create a public static property which only returns the instance
5. Inside that use lock() to handle the object instance for supporting multi-threading functionality.

Applicability:
1. Logger
It is primarily used in the areas where single access channel is needed. Logging is an area where it is primarily used.
2. Db Connection
For managing database connections also it is used as there should be only one connection at a time.
3. Configuration Manager
Then configuration managers, which read and write to configuration files, there also Singletons are used.

A Logger example in C#:
Steps to write it:
1. Select Visual studio > Project > C# & Windows Application.
2. Follow the steps given in the steps to write a Singleton class above
3. The complete code of the Singleton Pattern and its usage class are given below :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SingletonDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
MySingleton sg = MySingleton.Instance;
sg.LogMyError("test");
}
}


public sealed class MySingleton
{
private static volatile MySingleton instSingleton;
private static object objSynformultithread = new Object();

private MySingleton()
{
}

public void LogMyError(String msg)
{
TextWriter twLog = new StreamWriter("c:\\log.txt");

// write a line of text to the file
twLog.WriteLine("Start" + DateTime.Now);
twLog.WriteLine(msg);
twLog.WriteLine("End");
// close the stream
twLog.Close();
}


public static MySingleton Instance
{
get
{
if (instSingleton == null)
{
lock (objSynformultithread)
{
if (instSingleton == null)
instSingleton = new MySingleton();
}
}

return instSingleton;
}
}
}
}

Explanations over the Program:

Purpose of Sealed:
It prevents derivation which could add instances.

Purpose of Private Static Volatile:
Private, it makes the variable private and so not accessible from outside.
Static, without creating instance (only the instance created inside) we can call with name of the class as MySingleton.variable.
Volatile, makes the instance variable assignment completed before it is accessed.

Purpose of Lock:
It avoids deadlocks, enables multithreaded functionality.

The above program has the function LogMyError which does the logging to the file system. You can change the file name and path wherever you want it to get stored. Moreover it was written for a demo purpose only, so its not that much fuller. You need to add the append functionality to it such that it works as according to your need.

If you do the minor modifications based on your need of logging then it will be a good Logger for any kind of application.
Similarly it can be used for DB Connection, Configuration Management, etc.