Exemple #1
0
        static void Main(string[] args)
        {
            //------------------------------------Factory---------------------------------------

            ShipFactory factory = new ShipFactory();
            List <Ship> ships   = new List <Ship>();

            Console.WriteLine("Enter type of ship U / R / B");
            String a = Console.ReadLine();
            Ship   s = factory.makeShip(a);

            ships.Add(s);
            Console.WriteLine("Enter type of ship U / R / B");
            a = Console.ReadLine();
            s = factory.makeShip(a);
            ships.Add(s);
            foreach (Ship ship in ships)
            {
                Console.WriteLine(ship);
            }
            ships.Clear();
            //------------------------------------ABSTRACTING FACTORIES---------------------------------------


            ships.Add(ShipBuilding.orderShip("UFO"));
            ships.Add(ShipBuilding.orderShip("BIG UFO"));
            ships.Add(ShipBuilding.orderShip("ROCK"));
            foreach (Ship ship in ships)
            {
                Console.WriteLine(ship);
            }
            Console.ReadLine();
        }
        public static Ship orderShip(String type)
        {
            ShipBuilding shipbuilding = null;

            switch (type)
            {
            case "UFO":
                shipbuilding = new UFOShipBuilding();
                break;

            case "BIG UFO":
                shipbuilding = new UFOShipBuilding();
                break;

            case "ROCK":
                shipbuilding = new RocketShipBuilding();
                break;
                //DEFAULT
            }
            //treat missing cases
            Ship newship = shipbuilding.makeShip(type);

            return(newship);
        }