Last active
October 30, 2017 20:09
-
-
Save sardap/26d2022512be456a02581287d7728d21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IEven | |
{ | |
}; | |
public interface IOdd | |
{ | |
}; | |
public abstract class EvenOrOddNumber | |
{ | |
protected int _value; | |
public static EvenOrOddNumber CreateInstanceEvenOrOddNumber(int x) | |
{ | |
EvenOrOddNumber newCreation; | |
switch (x) | |
{ | |
case 1: | |
newCreation = new One(); | |
break; | |
case 2: | |
newCreation = new Two(); | |
break; | |
default: | |
throw new System.ArgumentException("Number not supported"); | |
} | |
if (newCreation is IEven && newCreation is IOdd) | |
{ | |
throw new System.ArgumentException("Number cannot be even and odd"); | |
} | |
return newCreation; | |
} | |
public int Value | |
{ | |
get | |
{ | |
return _value; | |
} | |
} | |
public bool isEven() | |
{ | |
bool result; | |
if (this is IEven) | |
{ | |
result = true; | |
} | |
else | |
{ | |
result = false; | |
} | |
return result; | |
} | |
public bool isOdd() | |
{ | |
bool result; | |
if (this is IOdd) | |
{ | |
result = true; | |
} | |
else | |
{ | |
result = false; | |
} | |
return result; | |
} | |
}; | |
public class One : EvenOrOddNumber, IOdd | |
{ | |
public One() | |
{ | |
_value = 1; | |
} | |
}; | |
public class Two : EvenOrOddNumber, IEven | |
{ | |
public Two() | |
{ | |
_value = 2; | |
} | |
}; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
EvenOrOddNumber number = EvenOrOddNumber.CreateInstanceEvenOrOddNumber(1); | |
EvenOrOddNumber number2 = EvenOrOddNumber.CreateInstanceEvenOrOddNumber(2); | |
Console.WriteLine("is {0} even? {1} and is {2} even? {3}", number.Value, number.isEven().ToString(), number2.Value, number2.isEven().ToString()); | |
Console.ReadLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment