Reputation: 141
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.
EDIT
Is this an example of Strategy pattern? I just edited Program.cs
public class program
{
public static void Main()
{
Console.WriteLine("Please enter Car or Bike for manufacture");
var vehicleName = Console.ReadLine();
var vehicle = factory.GetVehicle(vehicleName);
}
public void manufacture(IVehicle vehicle)
{
// assume that this method is in different class and method is calling with strategy as i understood.
vehicle.Manufacture();
}
}
Upvotes: 5
Views: 582
Reputation: 5236
Your code is example of Factory Method with Parameter. You can do Factory method without parameter that is better practice.
Strategy Pattern edits alghoritm, i like do Strategy from Abstract class
, not Interface
.
For example, your Strategy might look like this:
First, a Strategy
class. For example it will be calculate Fuel Consumption:
public abstract class Strategy
{
public abstract int FuelConsumption(int km);
}
Now, you do your strategies
. I will do two of them, for fast driving and for slow driving:
public class FastDriving : Strategy
{
//you need to override abstract methods from abstract class that
// are mentioned in Strategy as acstract, or any else abstract class
public override double FuelComsumption(int km) => km * fuelPer100Km;
private int fuelPer100Km = 30;
}
public class SlowDriving : Strategy
{
//same history as above
public override double FuelComsumption(int km) => km * fuelPer100Km - 100;
private int fuelPer100Km = 10;
//u need to edit alghoritm to strategy be a strategy
}
Now, in every Vehicle you can do a property of your Abstract Class Strategy
:
public class Bike : IVehicle
{
public void Manufacture()
{
Console.WriteLine("Bike Manufacturing");
}
int i = 1000; //some propertys needed to calculate alghoritm
// method in vehicle class that we use to strategy, to edit our alghoritm
public int CaluculateFuelConsumption() => Strategy.FuelConsumption() - i;
//here is a property of your strategy
public Strategy strategy {get; set;};
}
Now, You need to populate your Strategy. You can do it in class body if you want or in main:
Strategy strategy = new FastDriving();
Strategy strategy = new SlowDriving();
But you can do it even better.
Just do an abstract class, for example Vehicle
:
public abstract class Vehicle
{
public Strategy strategy {get; set;};
}
And then, your Vehicle can look like this:
public class Bike : Vehicle, IVehicle
{
public void Manufacture()
{
Console.WriteLine("Bike Manufacturing");
}
int i = 1000; //some propertys needed to calculate alghoritm
// method in vehicle class that we use to strategy, to edit our alghoritm
public int CaluculateFuelConsumption() => Strategy.FuelConsumption() - i;
//We deleted Strategy, because abstract Car class have already Strategy
//We dont need override non abstract method. Only abstract propertys
//need to be overrided
}
Now, in your main class you can do List of Cars, Populate it by Factory Method and attach Strategy
List<Vehicle> vehicles= new List<Vehicle>();
foreach(Vehicle vehicle in vehicles)
{
//its not factory, but you can use factory for your post here
switch(name)
{
case "Car":
vehicle = new Car();
case "Bike":
vehicle = new Bike();
default:
throw new ArgumentException();
}
//now, we populate strategy
vehicle.strategy = new FastDriving():
}
Then you can calculate all Fuel Consumptions by one loop:
foreach(Vehicle vehicle in vehicles)
int fuel += vehicle.CalculateFuelConsumption();
Strategy, that i write is a PUSH
Strategy. There is PULL
Strategy too. You can read about on the net. I belive, my answer is enough for you to understand, how strategy works :)
If you want read more about patterns, i recomend you that site:
Upvotes: 1
Reputation: 5773
I'd say Yes, the GetVehicle
method is an example of a specialised factory pattern called the simple factory, and you are using the thing it returns in the way that a strategy pattern would be used - the calling code is agnostic about the concrete implementation of the strategy.
Upvotes: 1