我正在使用工厂模式编写代码。在切换的情况下,我实际上是在返回Class对象。使用此返回类,我将调用一个方法。这是策略模式的例子吗?
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();
}
}
}
您能在这里消除我的误会吗?这段代码是否既是工厂模式又是策略模式的示例? 预先谢谢你。
编辑
这是策略模式的例子吗?我刚刚编辑了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();
}
}
答案 0 :(得分:1)
您的代码是带有参数的工厂方法的示例。您可以执行不带参数的Factory方法,这是更好的做法。
策略模式编辑算法,我喜欢从Abstract class
而不是从Interface
开始执行Strategy。
例如,您的策略可能如下所示:
首先,一个Strategy
类。例如,将计算油耗:
public abstract class Strategy
{
public abstract int FuelConsumption(int km);
}
现在,您执行自己的strategies
。我将做两个,分别是快车和慢车:
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
}
现在,您可以在每辆车中使用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;};
}
现在,您需要填充策略。您可以根据需要在班级正文中或在main中完成:
Strategy strategy = new FastDriving();
Strategy strategy = new SlowDriving();
但是您可以做得更好。
只需执行一个抽象类,例如Vehicle
:
public abstract class Vehicle
{
public Strategy strategy {get; set;};
}
然后,您的Vehicle可能如下所示:
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
}
现在,在您的主类中,您可以执行“汽车列表”,通过“工厂方法”进行填充并附加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():
}
然后,您可以通过一个循环计算所有燃油消耗量:
foreach(Vehicle vehicle in vehicles)
int fuel += vehicle.CalculateFuelConsumption();
我写的策略是PUSH
策略。也有PULL
策略。您可以在网上阅读。我相信,我的答案足以让您了解策略的工作原理:)
如果您想了解有关模式的更多信息,我建议您访问该网站:
答案 1 :(得分:1)
我要说的是,GetVehicle
方法是一种称为simple factory的特殊工厂模式的示例,您使用的是它返回的东西,将使用策略模式-调用代码与该策略的具体实现无关。