示例#1
0
        //instance variables
        public static Insect CreateInsect(InsectSpecies species)
        {
            //type not known at this time
            Insect animalObj = null;

            try
            {
                //type determined by late binding - species is an enumeration
                switch (species)
                {
                    case InsectSpecies.Bee:

                        //ID, nickName, age, category, gender
                        animalObj = new Bee(); //late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }
                        break;

                    //continue with the rest
                    case InsectSpecies.Butterfly:

                        animalObj = new Butterfly(); //late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }
                        break;

                    default:
                        Debug.Assert(false, "To be completed!");
                        break;

                }
            }
            //custom exception
            catch (Exception e)
            {
                e.Message.ToString();
                NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                throw (ex);
            }
            finally
            {

                //Set the category
                animalObj.Category = CategoryType.Insect;
            }
            return animalObj;
        }
        public static Reptile CreateReptile(ReptileSpecies species)
        {
            // Object type not known at this time
            Reptile animalObj = null;

            try
            {
                switch (species)
                {

                    case ReptileSpecies.Boa:

                        animalObj = new Boa();//late binding

                        if(animalObj == null){

                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();
                            throw (ex);
                        }

                        break;

                    case ReptileSpecies.Chamaleon:

                        animalObj = new Chamaleon();//late binding

                        if (animalObj == null)
                        {

                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();
                            throw (ex);
                        }

                        break;

                }
            }
            //custom exception
            catch (Exception e)
            {
                e.Message.ToString();

                NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                throw (ex);
            }

            finally
            {

                animalObj.Category = CategoryType.Reptile;
            }

            return animalObj;
        }
示例#3
0
        public static Bird CreateBird(BirdSpecies species)
        {
            //type not known at this time
            Bird animalObj = null;

            try
            {

                //type determined by late binding
                switch (species)
                {
                    case BirdSpecies.Eagle:

                         animalObj = new Eagle();//late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }

                        break;

                    case BirdSpecies.Falcon:

                        animalObj = new Falcon();//late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }

                        break;

                    case BirdSpecies.Duck:

                        animalObj = null;

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }

                        break;

                    default:

                        Debug.Assert(false, "to be completed");
                        break;
                }
            }
            //custom exception
            catch (Exception e)
            {
               e.Message.ToString();

               NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

               throw (ex);

            }

                //set the category
                animalObj.Category = CategoryType.Bird;

            return animalObj;
        }
示例#4
0
        public static Marine CreateMarine(MarineSpecies species)
        {
            //type not known at this time
            Marine animalObj = null;

            try
            {
                //type determined by late binding
                switch (species)
                {

                    case MarineSpecies.Shark:

                        animalObj = new Shark(); //late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }

                        break;

                    case MarineSpecies.Turtle:

                        animalObj = new Turtle(); //late binding

                        if (animalObj == null)
                        {
                            NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                            throw (ex);
                        }

                        break;

                    default:

                        Debug.Assert(false, "to be completed");

                        break;

                }
            }

            //custom exception
            catch (Exception e)
            {
                e.Message.ToString();
                NonExistentAnimalTypeException ex = new NonExistentAnimalTypeException();

                throw (ex);
            }
            finally
            {

                //set the category
                animalObj.Category = CategoryType.Marine;

            }
            return animalObj;
        }