예제 #1
0
 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");
     }
 }
예제 #2
0
        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!
        }
예제 #3
0
파일: Form1.cs 프로젝트: Isayur/DPR
 public Form1()
 {
     InitializeComponent();
     animalFactoryMain = new AnimalFactory();
 }