Monday, May 16, 2011

Stratey design pattern

Strategy design pattern, also known as Policy,is a behavioral design pattern.

Intent:
  • Encapsulates the algorithms to make them interchangeable from clients.


Scope:
  • When there are multiple algorithms which need to be interchangeable
  • Many related classes differ only in their behavior;to configure multiple behavior of the classes
Participants:
  • strategy
  • concrete strategy
  • context
Structure:


fig.UML Diagram

Sample Code:

Strategy:

abstract class SortStrategy
{
public abstract void Sort(List list)
}

Concrete Strategy:

class Quicksort:Sortstrategy
{
public overrid void Sort(Listlist)
{
Console.WriteLine("
Quicksort")
}
}

class SheellSort:SortStrategy
{

{
Console.WriteLine("SheellSort")

}

Context:

class SortContext
{
private List list=new
List();
private Strategy sortStrategy;
public void SetSortStrategy(SortStrategy sortStrategy)
{
this.sortStrategy=sortStrategy;
}
public void Add(string name)
{
list.Add(name);
}
public void Sort()
{
sortStrategy.Sort(list);
}


Client:

static void Main()
{
SortContext s=new SortConext();
s.Add("car");
s.Add("bus");
s.SetSortStrategy(new QuickSort());
s.Sort();
s.SetSortStrategy(new ShellSort());
s.Sort();
}






Creating crystal report with custom datasource datatable

We can use datatable as cutom datasource to crystal report with the following steps.
A.add dataset and datatable as below
-right click the project and add dataset


-double click the datatable in dataset toolbox
-right click the new datatable and add columns



B Add crystal report template
and follow the report wizard





on the list of available data sources,under the node project data and ado.net datasets,there is dataset1 which is to be selected.Move it to selected tables


add available fields to fields to display





add crystal report viewer to a form where report is to be displayed and
add following code to the form load.


Dataset1 ds = new Dataset1 ();
ds.Tables.Add(dt);
CRTransaction objRpt = new CRTransaction();
objRpt.SetDataSource(ds.Tables[1]);
crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();

dt is can be datasource of gridview or any custom datatable.