Saturday, November 13, 2010

..........Flyweight Pattern[SD]

When we need to share information between many objects than we use Flyweight pattern. If an information is shared between many objects than we can remove it from each object and referenced. This eliminates the redundancy and we can save memory. So instead of storing the same information n times for n object, it is only stored once. This object that contains all of the intrinsic(Moolbhoot) information is called a flyweight object.

Different Component involved in flyweight Pattern are:-

Flyweight is the set of intrinsic information that a set of objects share in common. It is a abstract class.

The ConcreateFlyweight is a subclass of Flyweight, and contains all of its information as well as how to calculate extrinsic information for a particular object. It is shared among more than one object.

The FlyweightFactory serves to dispense particular Flyweight requested. When a Flyweight with certain properties requested it check to see if it exist, If it exist it return else create a new one store it and return.

The Client, in creating a new object, must assign a Flyweight to it. So it ask the Flyweight Factory for a particular flyweight, receives that flyweight and create a reference to it, in the object it is creating.

abstract class Company
{
protected String m_sCompanyName = “ABC”
public abstract void GetEmployeeDetails();
}

private class Engineering:Company
{
Console.WriteLine(“Company Name “ + m_sCompanyName);
private void GetEmployeeDetails()
{ Consloe.WriteLine(“Engineering Employees Detail”);
}
}


private class RND:Company
{
Console.WriteLine(“Company Name “ + m_sCompanyName);
private void GetEmployeeDetails()
{ Consloe.WriteLine(“RND Employees Detail”);
}
}

No comments:

Followers

Link