Version 1.0
Week - 9
The information in this document is confidential to the person/entity to whom it is addressed and
should not be disclosed to any other person/entity. It may not be reproduced in whole, or in part,
nor may any of the information contained therein be disclosed without the prior consent of the
management of ISGN (the Company). Any form of reproduction, dissemination, copying,
disclosure, modification, distribution and or publication of this material is strictly prohibited. ISGN
Confidential and Proprietary. 2014 ISGN Corporation.
ISGN - Confidential
ISGN - Confidential
www.isgn.com | p.1
Document Information
Line of Business / Department
Technology
Design Patterns
Internal Only
Design Patterns V1.0
Training Presentation
1.0
Detail
Geetha V
Sinila Mundengara
Geetha V
NA
20th March 2015
20th March 2015
Subject
Distribution
Procedure Name
Document Type
Version
Level
Document Author / Owner
Reviewed By
Approved (Internal) By
Approved (External) By
Approval Date
Effective Date
Change History
Sl No Version #
1
1.0
Change Description
New document
Section Number
Changed By
Release Date
20th March 2015
The information in this document is confidential to the entity to which it is addressed and should not be disclosed to any other person. It
may not be reproduced in whole, or in part, nor may any of the information contained therein be disclosed without the prior consent of
the management of ISGN (the Company). A recipient may not solicit, directly or indirectly (whether through an agent or otherwise) the
participation of another institution or person without the prior approval of the management of the Company. Any form of reproduction,
ISGN - Confidential
dissemination, copying, disclosure, modification, distribution, and or publication of this material is strictly prohibited.
www.isgn.com | p.2
Week - 9
ISGN - Confidential
www.isgn.com | p.3
Learning Objectives
At the end of the session, the learners will have a good
understanding about:
Strategy Pattern
Template Method Pattern
Visitor Pattern
ISGN - Confidential
www.isgn.com | p.4
Strategy Pattern
In a scenario where we need to define a family of algorithms,
encapsulate each one, and make them interchangeable, Strategy
lets the algorithm vary independently from clients that use it.
Family of Algorithms- The definition says that the pattern
defines the family of algorithms; it means we have
functionality (in these algorithms) which will do the same
common thing for our object, but in different ways
Encapsulate each one of them- The pattern would force you
to place your algorithms in different classes (encapsulate
them). Doing so would help us in selecting the appropriate
algorithm for our objects
Make them interchangeable- The beauty with strategy pattern
is, we can select at run time which algorithm we should apply
to our object and can replace them with one another
ISGN - Confidential
www.isgn.com | p.5
Fig 1.0
ISGN - Confidential
www.isgn.com | p.6
ISGN - Confidential
www.isgn.com | p.7
ISGN - Confidential
www.isgn.com | p.8
{
public void Sort<T>(List<T>
dataToBeSorted)
{
//Logic for Heap sort
}
}
class Program
{
static void Main(string[] args)
{
ISortingStrategy sortingStrategy = null;
//Sorting the countyResidents
List<string> countyResidents = new
List<string>{ "ad","ac", "ax", "zw" };
sortingStrategy =
GetSortingOption(ObjectToSort.CountyRes
idents);
sortingStrategy.Sort(countyResidents);
//Sorting student numbers
List<int> studentNumbers = new
List<int>{ 123,678,543, 189};
sortingStrategy =
GetSortingOption(ObjectToSort.StudentN
umber);
sortingStrategy.Sort(studentNumbers);
sortingStrategy.Sort(railwayPassengers);
}
private static ISortingStrategy
GetSortingOption(ObjectToSort
objectToSort)
{
ISortingStrategy sortingStrategy = null;
switch (objectToSort)
{
case ObjectToSort.StudentNumber:
sortingStrategy = new
MergeSort();
break;
case
ObjectToSort.RailwayPassengers:
sortingStrategy = new HeapSort();
break;
case ObjectToSort.CountyResidents:
sortingStrategy = new QuickSort();
break;
default:
break;
}
return sortingStrategy;
}
}
}
ISGN - Confidential
www.isgn.com | p.9
Template Pattern
Define the skeleton of an algorithm in an operation,
deferring some steps to subclasses. Template Method lets
subclasses redefine certain steps of an algorithm without
changing the algorithm's structure.
ISGN - Confidential
www.isgn.com | p.10
Fig 2.0
ISGN - Confidential
www.isgn.com | p.11
Template Pattern
ISGN - Confidential
www.isgn.com | p.12
Template Pattern
using System;
using System.Data;
using System.Data.OleDb;
namespace
DoFactory.GangOfFour.Template.RealWorld
{
/// <summary>
/// MainApp startup class for Real-World
/// Template Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
static void Main()
{
DataAccessObject daoCategories = new
Categories();
daoCategories.Run();
DataAccessObject daoProducts = new Products();
daoProducts.Run();
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'AbstractClass' abstract class
/// </summary>
abstract class DataAccessObject
{
protected string connectionString;
protected DataSet dataSet;
/// <summary>
/// A 'ConcreteClass' class
/// </summary>
class Products : DataAccessObject
{
public override void Select()
{
string sql = "select ProductName from Products";
OleDbDataAdapter dataAdapter = new
OleDbDataAdapter(
sql, connectionString);
dataSet = new DataSet();
dataAdapter.Fill(dataSet, "Products");
}
DataTable dataTable =
dataSet.Tables["Categories"];
foreach (DataRow row in dataTable.Rows)
{
Console.WriteLine(row["CategoryName"]);
}
Console.WriteLine();
}
}
ISGN - Confidential
www.isgn.com | p.13
Visitor Pattern
Represent an operation to be performed on the elements of
an object structure. Visitor lets you define a new operation
without changing the classes of the elements on which it
operates.
ISGN - Confidential
www.isgn.com | p.14
Visitor Pattern
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
static void Main()
{
// Setup employee collection
Employees e = new Employees();
e.Attach(new Clerk());
e.Attach(new Director());
e.Attach(new President());
// Employees are 'visited'
e.Accept(new IncomeVisitor());
e.Accept(new VacationVisitor());
}
}
/// <summary>
/// The 'Element' abstract class
/// </summary>
abstract class Element
{
public abstract void Accept(IVisitor visitor);
}
/// <summary>
/// The 'ConcreteElement' class
/// </summary>
class Employee : Element
{
private string _name;
private double _income;
private int _vacationDays;
// Constructor
public Employee(string name, double income,
int vacationDays)
{
this._name = name;
this._income = income;
this._vacationDays = vacationDays;
}
}
}
/// <summary>
/// The 'Visitor' interface
/// </summary>
interface IVisitor
{
void Visit(Element element);
}
/// <summary>
/// A 'ConcreteVisitor' class
/// </summary>
class IncomeVisitor : IVisitor
{
public void Visit(Element element)
{
Employee employee = element as Employee;
}
}
/// <summary>
/// A 'ConcreteVisitor' class
/// </summary>
class VacationVisitor : IVisitor
{
public void Visit(Element element)
{
Employee employee = element as Employee;
}
public override void Accept(IVisitor visitor)
{
visitor.Visit(this);
}
}
/// <summary>
ISGN - Confidential
www.isgn.com | p.15
Thank you
ISGN - Confidential
www.isgn.com | p.16