Tuesday, April 27, 2010

Amazing Strategy!!


iFighterI
recently downloaded a cool fighter plane game (iFighter lite) on my iPhone. As I started playing, I felt that plane is

moving a bit slow!! But soon enough I saw a little perk on the screen and I flew over it and suddenly plane's speed increased. Soon enough, enemy planes started attacking me and I shot back at them. But again I felt my bullets were too slow! I shot down few planes and a new perk showed up on the screen. As soon as I collected it, my plane started shooting more bullets per attack!!

Yeeee hoo!!! I started enjoying this and before I knew, I by mistake collected a "skull and bones" perk and "darrnn!!" I said, cause I knew now my plane had lost those powerful bullets :(

Now, of course I am not writing a review about the human behavior while playing games on iPhone! But what I am trying to highlight here is the plane's ability to act differently in certain conditions! And that's something the Strategy Design Pattern allows us to do. I’ll try to use this game as an analogy to discuss about Strategy Design Pattern.

Definition: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

So if you think about it, the game engine has defined the family of algorithms and (ability to shoot bullets, ability to fly with different Speeds etc) uses them interchangeably which allows the plane to shoot bullets from less to more per attack and fly with less speed to increased speed and vice versa.

LFigher Plane Gameets put this little “game”, into a bit more technical paradigm. So here we are talking about some objects and their behaviors. We are talking about planes and their flying and shooting behaviors. We have two kinds of planes here, our plane and enemy planes. We know that all planes can fly, can turn left, can turn right regardless if it’s our plane or enemy planes. So there should be an abstract base class called Plane, which implements these obvious behaviors by itself (concrete methods).

We know that each type of plane has to distinguish itself through a unique outer display. So each plane have to take care of their own display or body work and hence, the Plane class leaves the implementation of the display() method to the concrete classes. Like we have My Plane and Enemy Plane classes here.

Because the flying behavior is variable for planes, we have pulled it out from the Plane class and encapsulated it separately. We did the same with the attack behavior. In fact that’s one of the rules while applying any design patterns to your existing design, which says “Identify the aspects of your application that vary and separate them from what stays the same.” So we identified, flying and shooting behaviors vary in our case. There is one more rule I would like to mention here. “Program to Interface not to the implementation”. This allows the design to be much more dynamic and scalable in the future.

Pay attention to methods setFlyBehavior() and setAttackBehavior(), they expect iFlyBehavior and iAttackBehavior respectively. These are the methods which actually turns this whole design into a dynamic design. Client code is able to call these methods and change the behaviors from FlySlow to FlyFaster!!

Now what next!? Well, now it’s the time for our little simulation of this iPhone game through C#.

DosScreen

 

 

 

 

 

 

 

And here’s the main() method that runs this code. As you can see, setFlyBehavior() method causes it to change the speed to 300 km/hour and shooting frequency also gets increased on the fly!

static void Main(string[] args)
{
    Plane plane = new MyPlane();

    Console.WriteLine("My Plane, Start Flying....");
    plane.performFly();

    Console.WriteLine("My Plane, Start Shooting....");
    plane.performAttack();
    Console.WriteLine();

    Console.WriteLine("****** My Plane collected an 'Increase Speed' item on screen ********");
    Console.WriteLine();
    plane.setFlyBehavior(new FlyFaster());
    plane.performFly();
    Console.WriteLine();
    Console.WriteLine();

    Console.WriteLine("****** My Plane collected an 'Increase Attack Speed' item on screen ********");
    Console.WriteLine();
    plane.setAttackBehavior(new FastAttack());
    plane.performAttack();
    Console.ReadLine();
}

In future if they want to add few more flying behaviors or attacking behaviors, they can simply create new concrete classes for those behaviors and they are done. They don’t need to change the Plane class. They even can add a whole new kind of plane which is a type of Bomber. In fact, in the game, at one stage, you can call upon a Bomber which takes care of all the enemy tanks on the ground in one shot. This Bomber is of course derived from Plane class but it has its own way of dealing with the Bad Guys!!

Apart from this demo, I actually used Strategy Pattern in my code too. I didn’t mention it in detail like iFighter’s demo. Because main purpose of this article is to uncover the awesome powers of Strategy Pattern and it would be easier to demonstrate with a simpler example like this. 

Problem that I solved using Strategy Pattern
In our system, we have to send out the email notifications to number of recipients depending on the type of the event happened. Generally, it's just a static email message, saying something has happened in the system. But in my case, this email body will be generated dynamically. There is some static text and along with it, I also have place holders (Tags) for the data which will be pulled from the data source when the email notification is ready to be sent out. So for each event type, it has its own message format and each message format has its own Tags.

Following image will provide a little more details about the tags and the formats.


 EmailNotificationProblem2

So as you can see above, the event HR Days Off Requested actually generates two notifications, one to employee and other to the Supervisor. They both have the similar data but the formats of the email bodies are different. We can identify this “variability” with the different flying / attacking behavior of the plane.

Class Diagram1

Here, the getNotificationData() method is where all the action happens. HRDaysOffRequestEmployeeFormat and HRDaysOffRequestSupervisorFormat implements this method separately, just like in iFighter example, FlySlow and FlyFast classes implemented fly() method individually. I know it’s not really a BIG deal here and it sounds like overkill to use the pattern for this problem. But hey, I did want to see it in action for REAL!! ;)

Conclusion:
Here are some more facts about the Strategy Design Patter and probably the reasons why you should think about using it some day!
Strategy pattern uses composition instead of inheritance. In the strategy pattern behaviors are defined as separate interfaces and specific classes that implement these interfaces. Specific classes encapsulate these interfaces. This allows better decoupling between the behavior and the class that uses the behavior. The behavior can be changed without breaking the classes that use it, and the classes can switch between behaviors by changing the specific implementation used without requiring any significant code changes. Behaviors can also be changed at run-time as well as at design-time.


Plane Demo Source code:
Source files (vs 2005) : Download

Thursday, October 15, 2009

Operator Overloading for Dummies

Hi,

It's been a while I have blogged about anything. And I found myself replying to a question regarding "Operator Overloading" in a User Group in Orkut.

Here's what the requirement was,

i have just started learning object oriented concepts and was wondering if some one could explain me using a practical real world example the use of operator overloading.

Disclaimer(read before you go any further):
the guy also happened to be an Indian. And in India, after the God, there's only one thing people worship, which is "Cricket"!! so, the Example that I have used is of course as you guessed right, it's definitely related to Cricket!!

Here's my reply to him, but be forewarned, this is strictly meant to help the Dummies like ME!! so please try not to leave any sarcastic comments please ;)

Here we Go!!!

I am pretty sure you can find out a lot of examples if you search for "Operator Overloading C#" in Google.

Apart from that, if you want a very high level understanding how Operator Overloading is used and why, I was thinking about, in a way Funny and not so convincingly real life but easy to understand example.

Say someone decides to create a game of Cricket, and he wants to give the user the ability to create a new player of his own, out of the existing players which are already in the game.

like, the user wants to create a super Player whose main skill is "batting", and he should have the strike rate which is average of Sachin and Lara both

If we try to put this particular requirement in terms of Object Oriented Programming.

There should be Classes like "Player", with following implementation,

class Player
{
public string name;
public double strikeRate;
public Player()
{
}

public Player(string Name, double StrikeRate)
{
name = Name;
strikeRate = StrikeRate;
}

//here's where Operator Overloading is done
public static Player operator +(Player p1,Player p2)
{
Player temp = new Player();
temp.strikeRate = (p1.strikeRate + p2.strikeRate) / 2;
return temp;
}
}

so now, as you know mostly + operator is used for addition.
but here we can Overload + operator for Player Class, so that when you type

Player playerSachine = new player("Sachine", 85.74);
Player playerLara = new player("Lara", 79.51);
Player SuperPlayer = new Player();

SuperPlayer = PlayerSachin + PlayerLara;

This will set the SuperPlayer's strikeRate which is the average of both the Great Players.
Which is 82.63. Here you can see the use of "Operator Overloading". So without this particular method

public static Player operator +(Player p1,Player p2)

in Player class, it would have been a compiler error at the line

SuperPlayer = PlayerSachin + PlayerLara;

hope it helps to someone who's starting up with OOP.

Happy Coding!!

Thursday, October 16, 2008

List.Exists(Predicate match)

Hi,

At times when you want to check for an Item inside a List list, you can use the Exists method as follows.

List.Exists(Predicate match)

List l = new List();
l.Add("Red");
l.Add("Green");
l.Add("Blue");
l.Add("Purple");

//prints "true" as "Red" is in the list
Console.WriteLine(l.Exists(delegate(string s) { return s == "Red"; }));

//prnts "false" as "Pink" is not in the list
Console.WriteLine(l.Exists(delegate(string s) { return s == "Pink"; }));
Published 30 September 04 08:00 by BradA
http://blogs.msdn.com/brada/archive/2004/09/30/236460.aspx

In my case I had to check against an object and not the string.

this will work, SourceFieldID is of type string.
bool itemExists = _criteriaItems.Items.Exists(delegate(CriteriaItem ci) { return ci.SourceFieldId == item.SourceFieldId; });

but this wont work, it will always return false.
bool itemExists = _criteriaItems.Items.Exists(delegate(CriteriaItem ci) { return ci == item; });

happy coding!!

Wednesday, September 10, 2008

Patterns and Practices Guideline Explorer

Patterns and Practices Explorer
A very useful tool and knowledge base for the believers of Patterns and Practices.


Link to Home Page
Link to Download

It gives you some very nice guidelines to go through in a given technology for example, Please look at the attached pic.

click the Image to see it better.



enjoy!!

Wednesday, September 26, 2007

using Indexing Server from .NET

I had to write the Indexing Server code to search within the file in VB6 and I again had to write it but this time in .net. Following is the Simplest way we can get the same thing done in .NET.
You have to have your Catalog created in your Indexing Server in Component Management Console (Start->run->Compmgmt.msc)

string strCat = "DWF"; //Your Catalog in Index Server
string strQuery = "Select DocTitle,Filename,Size,PATH,URL from SCOPE() where FREETEXT('" + Convert.ToChar(34) + text + Convert.ToChar(34) + "')";
//CONTAINS(Contents, '" + TextBox1 + "')";
//FREETEXT('" + TextBox1.Text + "')";
string conStr = "Provider=MSIDXS.1;Integrated Security .='';Data Source='" + strCat + "'";
cn.ConnectionString = conStr;
cn.Open();
OleDbDataAdapter DA = new OleDbDataAdapter(strQuery, cn);
DataSet testDataSet = new DataSet();
DA.Fill(testDataSet);
//Bind DataGrid to the DataSet. DataGrid is the ID for the
//DataGrid control in the HTML section.

DataView source = new DataView(testDataSet.Tables[0]);
GridView1.DataSource = source;
GridView1.DataBind();

Enjoy!!