Strategy Pattern

Well it has been a long time since I wrote something and I thought this time it would be cool that I wrote something technical. Before that I just wanted to mention that I completed my MCTS (Microsoft Certified Technology Specialist) with scores of an 89 in framework and a 95 in web. Not too bad I must say as I took about 3 days each to prepare for both these exams. Just going to add my MCTS logo with this post right about here -->
Anyways have been keeping myself busy learning design patterns. Patterns are really cool especially if you are the sort of person who likes to design (Well I am more of the sort of person who would give the idea for the design rather than implement it).
There are way too many patterns out there to be mastered but I rely on probably 4 to 5 of which I will talk about one below:

1]Strategy Pattern: Now this pattern is probably the easiest to master. It involves taking a preexisting set of classes and exposing their properties to other newer classes using interfaces or abstract classes. Let me take an example here. Let us consider a class Administration. Now we always have a president or a leader for an administration, so lets declare a variable administrator as well as define a function administer. Now we would like to keep modifying this function based on every new class we create. Just take a look at the following piece of code and there is nothing major to explain in this unless someone does not know basic OOPS concepts:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Patterns
{
public class Administration
{
string Administrator;
int size;
public string administer()
{
size=800;
Administrator="Manmohan Singh";
return "The name of our president is "+Administrator+" and he has a cabinet size of"+size;
}
}
abstract class pullAdministration : Administration
{
public abstract new string administer();

}

class School : pullAdministration
{
public override string administer()
{
return "The name of our president is Jayanthi and she has a faculty staff numbering 40";
}
}

class Strategy:School
{
static void Main(string[] args)
{
Administration adm = new Administration();
Console.WriteLine(adm.administer());
School sch = new School();
Console.WriteLine(sch.administer());

}
}
}

Well this is my first pattern for now. Probably will give a short and sweet pattern the next time I blog

Comments

Popular posts from this blog

System.ConfigurationSettings.AppSettings is Obsolete

Branding your SharePoint site in a super fast way - Emgage

Sharepoint & SSRS integration Issues