Search This Blog

Wednesday, October 27, 2010

Prototype Design Pattern

Prototype pattern is one of the simplest design pattern. Prototype means a to create a part and not to create the complete object. How Prototype relates to Design pattern is that in this design pattern, we can create a copy of the object and dont create the complete object from the scratch

When to use?

This design pattern is used when:
--> Creation of new objects is costly/time intensive.

How to use?

In .Net world we have something called as "MemberwiseClone()". MemberwiseClone method when invoked on some class, will return a shallow copy of the class's object.


Example:
We have a class named Employee:

class Employee
{
    int EmpID;
    string EmployeeName;
}


In the client or the Main method we will create the object like this:

Employee objEmployee1 = new Employee();

If we need to create another object of Employee class we can create it like this:

Employee objEmployee2 = new Employee();

Now, instead of creating the object objEmployee2, we can also create it without the new operator with Employee i.e by implementing the Prototype Pattern.

Implementing Prototype Pattern:
The modifications required for implementing the Prototype pattern are:
--> Addition of Clone method in the Employee Class
--> Calling the Clone method in the client to copy the object instead of creating a new one from the scratch.


Now inorder to add the Clone method, we would implement the IClonable interface in Employee class:
Here is the new code:

class Employee : IClonable
{
    int EmpID;
    string EmployeeName;
    
    public Employee Clone()
    {
       return (Employee)this.MemberwiseClone();
    }
}


In the client or the Main method we will create the objects like this:

Employee objEmployee1 = new Employee();

Creation of second object with Cloning:

Employee objEmployee2 = objEmployee1.Clone();

Now in objEmployee2, all the values that would be set in the objEmployee1 object would still be there, but both of these objects would be referring to different addresses as we have cloned them.

Monday, October 18, 2010

Catch Yourself Young

Being a Professional now for a number of years I have realized an important thing in life. Financial Security and a regular stream of money is really an important aspect of life. This is a thing that we normally donot realize when we are the most capable of doing it i.e in the very young years of our professional career. The Law here is "Catch yourself young".

Catch yourself young (CYY) - It basically means that the earlier we understand the importance of money in life the better we will be in future. There is a very important fact that will drive for achieving CYY and that is the environment you are in. If you are in an environment where people dont really think before spending, then you are 90% sure that you cannot achieve CYY formula. But if by God's grace, while you are young, you fortunately are in a company of people who think while spending that the thing you are going to buy is actually an asset to you or a liability, then you definitely are going to achieve the formula of CYY. For Example: Buying a Home is an Asset to you as the Cost of Property would increase eventually in the future and you would double benfeit from it by living in it free of cost and on the other hand your property price is increasing while you donot do any effort for it, and on the contrary if you buy a Car, you are buying a liablity as you would be spending a lot of maintainance money on it and you would also sell the car on a cost too low than you bought it. Both of the above examplified things are the ones which a person dreams of to buy, but we really need to make a conscious decision while buying, that is what we can get from the example above.
You would realize the effective usage of money only when you would know the above kind of facts and you can differentiate between what is actually a Liability and what is actually an Asset. And you would then spend your money in a direction that will again return you your spent money with a percentage increase in it.

I have realized this fact in my life, although not too early in my life, so even i am not able to achieve CYY, but i am trying to take the best of it now. ITS NEVER TOO LATE......