Saturday, November 13, 2010

..........Factory Pattern[CD]

Factory Pattern :- A Factory Pattern is one that returns an instance of class depending on parameter passed. Means which class object should be created it decide on run time.

Example : We have a base class Shape and two child classes named Circle and Square.

public class Shape
{
public abstract Draw(String Type);
}

public void Square:Shape
{
private override Draw()
{
System.Console.Writeline(“Draw Square”);
}
}


public void Circle:Shape
{
private override Draw()
{
System.Console.Writeline(“Draw Circle”);
}
}


public void Client
{

private void CreateObject(String Type)
{
if(Type==”Circle”)
return new Circle();
else if(Type==”Square”)
return new Square();
return null;
}
}

public static void main(String[] args)
{
Client objClient =null;
Shape objShape =null;

objClient = new Client();
objShape= objClient.CretaeObject(“Circle”);
objShape.Draw();.

objShape= objClient.CretaeObject(“Square”);
objShape.Draw();.
}

No comments:

Followers

Link