示例#1
0
    static void Main()
    {
        IFactory catFactory = new CatFactory();
        IPet     cat        = catFactory.Create();

        cat.Show();
    }
示例#2
0
        private void SavePet()
        {
            IPet pet;

            Console.WriteLine("Please enter your pets name:");
            var name = Console.ReadLine();

            Console.WriteLine();

            Console.WriteLine("Please enter your pets age:");
            var age = Console.ReadLine();

            Console.WriteLine();

            if (!int.TryParse(age, result: out var option) || !option.IsInRange(1, 20))
            {
                Console.WriteLine("Invalid input.");
                Console.WriteLine();
                return;
            }

            Console.WriteLine("Please enter what type of pet it is (0 for dog, 1 for cat):");
            var type = Console.ReadLine();

            Console.WriteLine();

            if (!int.TryParse(type, result: out option) || !option.IsInRange(0, 1))
            {
                Console.WriteLine("Invalid input.");
                Console.WriteLine();
                return;
            }

            // A problem I have is there is now two places where I am deciding to create a pet. Is this wrong? It feels wrong.
            // I both decide here (when passing to the PetManager) and within the PetManager if I get pets from the database.
            // Would it be better to just pass the parameters themselves instead of creating an object then passing it?
            // I have been struggling a little bit with the Factory Method and where to put the switch statement, because I feel its needed
            // to determine which type of pet to create. Ultimately I feel that I want to go down the path of Dependency injection however
            // with the example I have shown here, it requires adjustment or a better understanding of the factory method.
            switch (Enum.Parse <PetType>(type))
            {
            case PetType.Dog:
                pet = new DogFactory().GetPet(name, Int32.Parse(age), Enum.Parse <PetType>(type.ToString()));
                break;

            case PetType.Cat:
                pet = new CatFactory().GetPet(name, Int32.Parse(age), Enum.Parse <PetType>(type.ToString()));
                break;

            default:
                pet = null;
                break;
            }

            // Our dependency injection begins here.
            _pets.SavePet(pet);
            // Update pets list after saving a new pet.
            _pets.GetPets();
        }
示例#3
0
        public void TestGetCandidates_ShouldReturnCorrectNumberOfCandidates()
        {
            //Given
            catHttpRepositoryMocked.Setup(mock => mock.GetAllCandidates()).Returns(CatFactory.GetAllCandidates());

            //When
            var candidates = Tested.GetCandidates();

            //Then
            candidates.Count.Should().Be(CatBusiness.NbrOfCandidate);
        }
示例#4
0
        public void TestGetCandidates_ShouldReturnDifferentCandidates()
        {
            //Given
            catHttpRepositoryMocked.Setup(mock => mock.GetAllCandidates()).Returns(CatFactory.GetAllCandidates());

            //When
            var candidates = Tested.GetCandidates();

            //Then
            candidates[0].Reference.Should().NotBe(candidates[1].Reference);
            candidates[0].Url.Should().NotBe(candidates[1].Url);
        }
        public void CatFactoryTest()
        {
            IFactory      catFactory = new CatFactory();
            IAnimal       cat        = catFactory.GetAnimal();
            StringBuilder strBuilder = new StringBuilder();

            strBuilder.Append("Head of: cat").AppendLine()
            .Append("Body of: cat").AppendLine()
            .Append("Members of: cat").AppendLine()
            .Append("And says: meow").AppendLine();
            Assert.AreEqual(strBuilder.ToString(), cat.GetDescription());
        }
示例#6
0
        static void Main(string[] args)
        {
            // Making a few cats and treating them as ICat
            // This is good for a demonstration, but in real life generally you won't do this
            ICat fatCat   = new FatCat();
            ICat shortCat = new ShortCat();

            fatCat.Meow();
            shortCat.Meow();

            // Since we know we have a fat cat, we can use our fat cat printer
            FatCat castFatCat = fatCat as FatCat;

            FatCatPrinter.Print(castFatCat);

            // What happens if we cast our ShortCat as a FatCat?
            FatCat castShortCat = shortCat as FatCat;

            FatCatPrinter.Print(castShortCat);

            // What would happen if we didn't use the "as" keyword?
            // FatCatPrinter.Print((FatCat)shortCat);

            // What's the advantage of using "as" ?
            // We can do null checks
            //FatCat castShortCat2 = shortCat as FatCat;
            //if (castShortCat2 != null)
            //    FatCatPrinter.Print(castShortCat2);

            // The "is" keyword is similar to as, but only returns true/false
            if (shortCat is FatCat)
            {
                FatCatPrinter.Print((FatCat)shortCat);
            }

            Console.WriteLine("---");
            Console.WriteLine("--- Now using factory ---");
            Console.WriteLine("---");

            // More realistic example of getting cats
            ICat catFromFactory1 = CatFactory.GetCat();
            ICat catFromFactory2 = CatFactory.GetCat();

            catFromFactory1.Meow();
            catFromFactory2.Meow();

            // Assignment
            //
            // todo: Use both keywords "as" and "is" to safely call the FatCatPrinter.Print method
            //
        }
示例#7
0
        public void TestGetCat_ShouldReturnCatWithLoadedData()
        {
            //Given
            catSqlRepositoryMocked.Setup(mock => mock.GetCats()).Returns(CatFactory.GetCats());

            //When
            var cats = Tested.GetCats();

            //Then
            cats[0].Reference.Should().Be("Ugly");
            cats[0].Url.Should().Be("ugly.com");
            cats[0].LostVoteCount.Should().Be(10);
            cats[0].WinVoteCount.Should().Be(1);
        }
示例#8
0
        public static void Main(string[] args)
        {
            IReader reader = new ConsoleReader();
            IWriter writer = new ConsoleWriter();
            IFactoryCleansingCenter factoryClearsinCenter = new CleansingCenterFactory();
            IFactoryAdoptedCenter   factoryAdoptingCenter = new AdoptedCenterFactory();
            IFactoryDog             factoryDog            = new DogFactory();
            IFactoryCat             factoryCat            = new CatFactory();
            IDataBaseCenter         dataBase = new DataBaseCenter();

            var engine = new Engine(reader, writer, factoryClearsinCenter,
                                    factoryAdoptingCenter, factoryDog, factoryCat, dataBase);

            engine.Run();
        }
示例#9
0
        void Update()
        {
            if (_isOpen)
            {
                if (_spawnWait > _spawnRate)
                {
                    Cat cat = CatFactory.Instance().RandomCat();
                    cat.transform.position = _spawnLocation.position;

                    _spawnWait = 0.0f;

                    EvSys.Instance().AddMessage("Window Cat: <color=red>+1 to Cat Lady</color>");
                }

                _spawnWait += Time.deltaTime;
            }
        }
示例#10
0
        private IPet CreatePet(DataRow data)
        {
            IPet pet;

            switch (Enum.Parse <PetType>((string)data["Type"]))
            {
            case PetType.Dog:
                pet = new DogFactory().GetPet((string)data["Name"], Int32.Parse((string)data["Age"]), Enum.Parse <PetType>((string)data["Type"]));
                break;

            case PetType.Cat:
                pet = new CatFactory().GetPet((string)data["Name"], Int32.Parse((string)data["Age"]), Enum.Parse <PetType>((string)data["Type"]));
                break;

            default:
                pet = null;
                break;
            }
            return(pet);
        }
示例#11
0
 public List <Cat> GetCats()
 {
     return(CatFactory.GetCats());
 }
示例#12
0
 public List <Cat> GetCandidates()
 {
     return(CatFactory.GetAllCandidates().Take(CatBusiness.NbrOfCandidate).ToList());
 }