hi,
am back after a long time :)
today its a new design pattern.
scenario
i want to design a graphic engine which has a graphic surface and this surface renders many graphic images like lines,rectangles etc.
so where do we start?
we will have an interface IGraphic (methods - AddControl,RemoveControl,getControls)
this is implemented by a a class Graphic
public class Graphic :IGraphic
{
//implement the methods here
//also this class will compose of IControl - which are implemented by control class
}
this is fine.....
i am happy and the software works fine.....until one da ,when the customer comes back to me ..he says that he wants controls to host other controls...he wants the graphic surface to host controls and these controls may contain other controls.
so now how will i incorporate this?
am i stuck ?
well as a matter of fact,i am in a litle bit of a soup.
i can somehow manage for the particular control the cutomer says as of now.
but if he comes back tomorrow for another control, i am lost again :(.......
wel lbut now i know of these problems, why not change the intial design itself to incorporate all the changes the customer asks for?
how? have a look below
IGraphic can be an abstract class.
public abstract class IGraphic
{
public void AddChild();
public void RemoveChild();
public arraylist getChildren();
}
public class Graphic :IGraphic
{
//implement the methods of the base class here
//have an arraylist of child objects
//so it will be
arraylist Children;
}
public class GroupBox : IGraphic
{
//implement the methods of the base class
//have an arralist of the children
arraylist children;
}
public class Button:Igraphic
{
//provide dummy implementation here for addchild , getchildren and removechild as
//this control does not have children
}
example code is given below
public abstract class IGraphic
{
protected System.Collections.ArrayList Children;
string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public abstract void AddChild(object obj);
public abstract void removechild(object obj);
public abstract System.Collections.ArrayList getChildren();
public void print()
{
Console.WriteLine("Name is "+Name);
}
}
public class graphic : IGraphic
{
public graphic()
{
Children = new ArrayList();
}
public override void AddChild (object obj)
{
Children.Add(obj);
}
public override void removechild (object obj)
{
Children.Remove(obj);
}
public override System.Collections.ArrayList getChildren()
{
return Children;
}
public void print()
{
Console.WriteLine("Name is "+Name);
foreach(object obj in Children)
{
Console.WriteLine("children are " + ((IGraphic)obj).Name);
}
}
}
public class Groupbox : IGraphic
{
public Groupbox()
{
Children = new ArrayList();
}
public override void AddChild (object obj)
{
Children.Add(obj);
}
public override void removechild (object obj)
{
Children.Remove(obj);
}
public override System.Collections.ArrayList getChildren()
{
return Children;
}
public void print()
{
Console.WriteLine("Name is "+Name);
foreach(object obj in Children)
{
Console.WriteLine("children are " + ((IGraphic)obj).Name);
}
}
}
public class Button : IGraphic
{
public Button()
{
}
public override void AddChild (object obj)
{
//do nothing
}
public override void removechild (object obj)
{
//do nothing
}
public override System.Collections.ArrayList getChildren()
{
//do nothing
return null;
}
public void print()
{
Console.WriteLine("Name is "+Name);
}
}
static void Main(string[] args)
{
graphic g = new graphic();
g.Name = "graphic object";
Groupbox gbox = new Groupbox();
gbox.Name = "Groupbox";
Button b = new Button();
b.Name = "button inside the groupBox";
Button btn = new Button();
btn.Name = "button inside the graphic";
gbox.AddChild(b);
gbox.print();
g.AddChild(gbox);
g.print();
g.AddChild(btn);
btn.print();
g.print();
}
output is
Name is Groupbox
children are button inside the groupBox
Name is graphic object
children are Groupbox
Name is button inside the graphic
Name is graphic object
children are Groupbox
children are button inside the graphic