static public ICarType getCarTypeObj(string carType) { ICarType objCarType = null; if (carType.ToLower() == "sedan") { objCarType = new Sedan(); } else if (carType.ToLower() == "racecar") { objCarType = new Racecar(); } else { objCarType = new InvalidCarType(); } return(objCarType); }
private void button1_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex != -1 && listBox2.SelectedIndex != -1) { ICarFactory factory = listBox1.SelectedItem as ICarFactory; ICarType t = listBox2.SelectedItem as ICarType; label1.Text = factory.GetType().Name; if (t is SUV) { ISUV temp = factory.CreateSUV(); label1.Text += " " + temp.OffRoadDriving() + " " + temp.Sell(); } if (t is Hybrid) { IHybrid temp = factory.CreateHybrid(); label1.Text += " " + temp.ChargeCar() + " " + temp.Sell(); } } }