Hi I am new to design pattern and apologize if this question is creating any confusion although i am trying to describe the issue in best possible way.I have implemented sample abstract factory pattern in winforms. Front end contains two check boxes to create the objects. Note: If both the check box are checked, both the objects are created. I am using objs.CreateProduct(Maxima,Ultima) method and passing the boolean values to create the objects. Here I am passing the values of both the properts whether I want to create object for ultima or maxima. Can you suggest any other better way to achieve this ? I don't want to pass the properties for maxima and ultima if I am creating the objects.
public partial class Form1 : Form
{
public bool Maxima
{
get;
set;
}
public bool Ultima
{
get;
set;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Factory[] obj = new Factory[2];
obj[0] = new B();
obj[1] = new C();
foreach (Factory objs in obj)
{
iProduct prod = objs.CreateProduct(Maxima,Ultima);
if (prod != null)
{
prod.GetDetails();
}
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
Maxima = true;
else
Maxima = false;
if (checkBox1.Checked)
Ultima = true;
else
Ultima = false;
}
}
abstract class Factory
{
public abstract iProduct CreateProduct(bool maxima, bool ultima);
}
class B : Factory
{
public override iProduct CreateProduct(bool maxima,bool ultima)
{
if (ultima)
{
return new NissanUltima();
}
else return null;
}
}
class C : Factory
{
public override iProduct CreateProduct(bool maxima,bool ultima)
{
if (maxima)
{
return new NissanMaxima();
}
else return null;
}
}
interface iProduct
{
void GetDetails();
}
class NissanUltima:iProduct
{
public void GetDetails()
{
MessageBox.Show("NissanUltima is created");
}
}
class NissanMaxima:iProduct
{
public void GetDetails()
{
MessageBox.Show("NissanMaxima is created");
}
}