jeudi 24 janvier 2019

Strategy pattern used in Factory pattern?

I am writing code with the factory pattern. In switch case, I am actually returning Class objects. Using this return class, I am going to call a method. Is this an example of strategy pattern?

using System;
using System.Linq;

namespace ConsoleApplication1
{
    public interface IVehicle
    {
          void Manufacture();
    }

    public class Car : IVehicle
    {
        public void Manufacture()
        {
            Console.WriteLine("Car Manufacturing");
         }
     }

     public class Bike : IVehicle
     {
         public void Manufacture()
         {
            Console.WriteLine("Bike Manufacturing");
         }
     }

     public static class factory
     {
         public static IVehicle GetVehicle(string name)
         {
            switch(name)
            {
                case "Car":
                    return new Car();
                case "Bike":
                    return new Bike();
                default:
                    throw new ArgumentException();
            }
        }
    }

    public class program
    {
        public static void Main()
        {
            Console.WriteLine("Please enter Car or Bike for manufacture");
            var vehicleName = Console.ReadLine();
            factory.GetVehicle(vehicleName).Manufacture();
            Console.ReadLine();
        }
    }

}

Can you clear my misunderstanding here? Is this code is an example of factory and strategy pattern both? Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire