public void setAnimalFactory(String factoryType) { if ("sea".Equals(factoryType)) { currentFactory = new SeaAnimalFactory(); } else if ("land".Equals(factoryType)) { currentFactory = new LandAnimalFactory(); } else { MessageBox.Show("Invalid factory type"); } }
static void Main(string[] args) { IAnimal animal = null; AnimalFactory animalFactory = null; string speakSound = null; // Create the Sea Factory object by passing the factory type as Sea animalFactory = AnimalFactory.CreateAnimalFactory("Sea"); Console.WriteLine("Animal Factory type : " + animalFactory.GetType().Name); Console.WriteLine(); // Get Octopus Animal object by passing the animal type as Octopus animal = animalFactory.GetAnimal("Octopus"); Console.WriteLine("Animal Type : " + animal.GetType().Name); speakSound = animal.speak(); Console.WriteLine(animal.GetType().Name + " Speak : " + speakSound); Console.WriteLine(); Console.WriteLine("--------------------------"); // Create Land Factory object by passing the factory type as Land animalFactory = AnimalFactory.CreateAnimalFactory("Land"); Console.WriteLine("Animal Factory type : " + animalFactory.GetType().Name); Console.WriteLine(); // Get Lion Animal object by passing the animal type as Lion animal = animalFactory.GetAnimal("Lion"); Console.WriteLine("Animal Type : " + animal.GetType().Name); speakSound = animal.speak(); Console.WriteLine(animal.GetType().Name + " Speak : " + speakSound); Console.WriteLine(); // Get Cat Animal object by passing the animal type as Cat animal = animalFactory.GetAnimal("Cat"); Console.WriteLine("Animal Type : " + animal.GetType().Name); speakSound = animal.speak(); Console.WriteLine(animal.GetType().Name + " Speak : " + speakSound); Console.ReadKey(); // Go to http://aka.ms/dotnet-get-started-console to continue learning how to build a console app! }
public Form1() { InitializeComponent(); animalFactoryMain = new AnimalFactory(); }