static void Main(string[] args) { string continent = ""; if (args.Length >= 1) { continent = args[0]; //properties of the project >> Debug >> Aplication Arguments } //here we can see that the class (AnimalWorld) has private constructor IContinentFactory myContinent1 = AnimalWorld.CreateContinent(continent); //factory ICarnivore myCarnivore1 = myContinent1.CreateCarnivore(); //factory IHebivore myHarbivore1 = myContinent1.CreateHerbivore(); myCarnivore1.Hunt(); myHarbivore1.Graze(); Console.WriteLine(myCarnivore1.GetType().FullName); //Console.WriteLine("Choose continent"); //string continent = "america"; //Console.WriteLine("Choose continent"); //string continent2 = Console.ReadLine(); //var myContinent2 = AnimalWorld.CreateContinent(continent2); //var myC2 = myContinent2.CreateCarnivore(); //myC2.Hunt(); Console.ReadKey(); }
public AnimalWorld() { factory = new T(); carnivore = factory.CreateCarnivore(); herbivore = factory.CreateHerbivore(); }
public AnimalWorld() { _factory = new T(); _herbivore = _factory.CreateHerbivore(); _carnivore = _factory.CreateCarnivore(); }
/// <summary> /// Contructor of Animalworld /// </summary> public AnimalWorld() { // Create new continent factory _factory = new T(); // Factory creates carnivores and herbivores _carnivore = _factory.CreateCarnivore(); _herbivore = _factory.CreateHerbivore(); }
static void Main(string[] args) { Console.WriteLine("Which continent you want to play?"); string continent = Console.ReadLine(); IAnimalFactory factory = new EuroFactory(); if (continent == "Africa") { factory = new AfricaFactory(); } ICarnivore carnivore = factory.GetCarnivore(); }
public void GiveMeat() // Feed all animals that eat meat { foreach (GameObject animal in animals) { ICarnivore carnivoreTemp = animal.GetComponent <ICarnivore>(); if (carnivoreTemp != null) { carnivoreTemp.EatMeat(); } } }
public static bool FightHerbivore(this ICarnivore carnivore) { //var herbivore = Zoo.Instance().FindHerbivore(); //if (herbivore != null) //{ // Console.WriteLine($"{carnivore.Name} ate {herbivore.Name}"); // carnivore.IsHungry = false; // return true; //} return(false); }
public static bool FightHerbivore(this ICarnivore carnivore) { var herbivore = Zoo.Instance().FindHerbivore(); if (herbivore != null) { Console.WriteLine($"{carnivore.Name} ate {herbivore.Name}"); carnivore.IsHungry = false; return(true); } return(false); }
/// <summary> /// Contructor of Animalworld /// </summary> /// <param name="continent">Continent of the animal world that is created.</param> public AnimalWorld(Continent continent) { // Get fully qualified factory name string name = this.GetType().Namespace + "." + continent.ToString() + "Factory"; // Dynamic factory creation IContinentFactory factory = (IContinentFactory)System.Activator.CreateInstance (Type.GetType(name)); // Factory creates carnivores and herbivores _carnivore = factory.CreateCarnivore(); _herbivore = factory.CreateHerbivore(); }
static void Main(string[] args) { // the diff with SimpleFactory is that here we can have // many factories Console.WriteLine("In which continent do you want to play_"); string continent = Console.ReadLine(); IAnimalFactory factory = new EuropeFactory(); if (continent == "Africa") { factory = new AfricaFactory(); } ICarnivore animal = factory.GetCarnivore(); }
static void Main(string[] args) { Console.WriteLine("Which contitnent do you want to play?"); string contitnent = Console.ReadLine(); IAnimalFactory factory = null; if (contitnent == "Africa") { factory = new AfricaFactory(); } else if (contitnent == "Europe") { factory = new EuropeFactory(); } ICarnivore animal = factory.GetCarnivore(); }
public AnimalWorld(IContinentFactory factory) { carnivore = factory.CreateCarnivore(); herbivore = factory.CreateHerbivore(); }
private static void TestPolymorphism() { // Променлива от тип ICarnivore. Може да сочи към обект, който наследява интерфейс ICarnivore. // Чрез нея могат да бъдат достъпени методите описани в този интерфейс // както и методите описани в Object, защото всичко така или иначе наследява Object ICarnivore carnivore = CreateCarnivore(); carnivore.EatMeat(); carnivore.ToString(); //Но чрез нея не могат да бъдат извикани методите на обект Fox, тък като тя (променливата) не е от тип Fox. //carnovore.StealTheCheese() //Променлива от тип AbstractAnimal. Може да сочи към обект, който наследява обект AbstractAnimal. //Чрез нея могат да бъдат достъпени методите описани в AbstractAnimal, //както и методите описани в Object, защото всичко така или иначе наследява Object //Но пак както преди не може да бъде използвана за извикване на методите на FOX AbstractAnimal anyAnimal = CreateAbstractAnimal(); //Променлива от тип Object, може да сочи към всичко, защото всичко всъщност наследява Object, директно или не. //Но може да бъде използвана за извикване само на методи дефинирани в Object Object obj = CreateObject(); obj.GetHashCode(); // променлива от тип IHerbivore. Може да сочи към обект наследяващ IHerbivore - Elephant, Rabbit или Fox, // но не може да сме 100% сигурни към кое от тях без да сме проверили IHerbivore herbivore = CreateHerbivore(); // може да проверим дали случайно не сочи към Rabbit if (herbivore is Rabbit) { // по случайност сочи към обект който всъщност е Rabbit Rabbit r; // създаваме променлива от тип Заек - тя може да сочи само към обект от тип Заек. r = (Rabbit)herbivore; // тъй като вече сме проверили, че herbivore всъщност сочи към Заек е безопасно да кастнем към Заек. // тъй като r е от тип Заек може да извикваме методите на заек. r.DoZigZag(); } else if (herbivore is Fox) { // съкратена версия на предното ((Fox)herbivore).StealTheCheese(); } else if (herbivore is Elephant el) { // още по-съкратена версия на предното. el.DrinkFromTheNose(); } // директното кастване е опасно: ((Fox)herbivore).StealTheCheese(); // Използва се само ако преди това сме направили проверка дали наистина обекта е такъв, какъвто ние очакваме: if (herbivore is Fox) // В противен случай програмата ще гръмне ако не сме уцелили правилният тип //Втори вариант на кастване - ключова дума "as" : Fox fox = herbivore as Fox; if (fox == null) { // резултат 1: herbivore не е от тип лисица и няма как да бъде третиран като такава. Променливата Fox ще сочи към нищото. // При такъв вид кастване трябва винаги да се провери дали променливата не е null. Console.WriteLine("This herbivore is not a fox"); } else { //резултат 2: успешно успяхме да кастнем към лисица, това означава, че обекта herbivore всъщност е лисица. // и сега чрез променливата fox може да викаме методи характерни само за лисица. fox.StealTheCheese(); } //Всяка променлива от референтен тип А може: Fox f = new Fox(); //да сочи към обект от тип А IHerbivore herb = new Rabbit(); //, обект, който наследява А Elephant elephant = null; //или към нищо (null) }
public Jungle(IHabitatFactory factory) { herbivore = factory.CreateHerbivore(); carnivore = factory.CreateCarnivore(); }
public AnimalWorld(IContinentFactory continentFactory) { _herbivore = continentFactory.CreateHerbivore(); _carnivore = continentFactory.CreateCarnivore(); }