我的产品工厂
public interface IProductFactory
{
void Drive(int miles);
}
我的车辆工厂
public interface IVehicleFactory
{
IProdctFactory GetVehicle(string Vehicle);
}
产品自行车
class Bike:IProdctFactory
{
public void Drive(int miles)
{
Console.WriteLine("Drive the Bike : " + miles.ToString() + "km");
}
}
产品滑板车
class Scooter:IProdctFactory
{
public void Drive(int miles)
{
Console.WriteLine("Drive the Scooter : " + miles.ToString() + "km");
}
}
产品车
class Car:IProdctFactory
{
public void Drive(int miles)
{
Console.WriteLine("Drive the Car : " + miles.ToString() + "km");
}
}
产品吉普车
class Jeep:IProdctFactory
{
public void Drive(int miles)
{
Console.WriteLine("Drive the Jeep : " + miles.ToString() + "km");
}
}
Two Wheeler Factory
public class TwoWheelerFactory : IVehicleFactory
{
public IProdctFactory GetVehicle(string Vehicle)
{
switch (Vehicle)
{
case "Scooter":
return new Scooter();
case "Bike":
return new Bike();
default:
throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Vehicle));
}
}
}
Four Wheeler Factory
class FourWheelerFactory:IVehicleFactory
{
public IProdctFactory GetVehicle(string Vehicle)
{
switch (Vehicle)
{
case "Car":
return new Car();
case "Jeep":
return new Jeep();
default:
throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Vehicle));
}
}
}
客户端
class Program
{
static void Main(string[] args)
{
IVehicleFactory twoWheelerfactory = new TwoWheelerFactory();
IProdctFactory scooter = twoWheelerfactory.GetVehicle("Scooter");
scooter.Drive(10);
IProdctFactory bike = twoWheelerfactory.GetVehicle("Bike");
bike.Drive(20);
IVehicleFactory fourWheelerFactory = new FourWheelerFactory();
IProdctFactory car = fourWheelerFactory.GetVehicle("Car");
car.Drive(100);
IProdctFactory jeep = fourWheelerFactory.GetVehicle("Car");
jeep.Drive(200);
Console.ReadKey();
}
}
工厂模式中有两个具体的车辆类(TwoWheelerFactory,FourWheelerFactory)吗?
虽然我读了很多关于抽象工厂模式的内容,但我仍然不确定它是什么:(有人可以帮助我将其转换为抽象工厂模式吗?
我读了
Design Patterns: Abstract Factory vs Factory Method
和
http://www.dotnet-tricks.com/Tutorial/designpatterns/FUcV280513-Factory-Method-Design-Pattern---C
答案 0 :(得分:2)
从Wikipedia - 抽象工厂模式的本质是提供一个界面来创建相关或从属对象的族,而不指定它们的具体类。"。
关键部分是客户永远不会知道它有汽车或自行车的实例或其他什么,它只处理抽象类型。
不同的工厂会为您提供不同的实例,但抽象类型是相同的。
您的自行车,踏板车,吉普车和汽车都是应该通过工厂方法创建的具体产品。在您的示例中,让这些产品继承自界面,特别是名为' Factory'这是错误和令人困惑的(我并不是说接口通常是错误的)。
以下我只是将您的示例/措辞(带有一些小扩展名)应用于Data& amp;提供的Abstract Factory模式。对象工厂的人帮你提供一些背景信息。
虽然学习新模式和技术非常棒且令人兴奋,但是不要试图将你学到的这种新模式应用到各种情境中。你能做的最好的事情就是编写简单的代码。在实际需要之前,应避免任何额外的复杂性。
class MainApp
{
public static void Main()
{
// Abstract factory #1
AbstractFactory factory1 = new EuropeVehicleFactory();
Client client1 = new Client(factory1);
client1.Run();
// Abstract factory #2
AbstractFactory factory2 = new UsaVehicleFactory();
Client client2 = new Client(factory2);
client2.Run();
}
}
/// <summary>
/// The 'AbstractFactory' abstract class
/// </summary>
abstract class AbstractFactory
{
public abstract TwoWheelerProduct CreateTwoWheelerProduct();
public abstract FourWheelerProduct CreateFourWheelerProduct();
}
/// <summary>
/// The 'ConcreteFactory1' class
/// </summary>
class UsaVehicleFactory : AbstractFactory
{
public override TwoWheelerProduct CreateTwoWheelerProduct()
{
return new Bike();
}
public override FourWheelerProduct CreateFourWheelerProduct()
{
return new Car();
}
}
/// <summary>
/// The 'ConcreteFactory2' class
/// </summary>
class EuropeVehicleFactory : AbstractFactory
{
public override TwoWheelerProduct CreateTwoWheelerProduct()
{
return new Scooter();
}
public override FourWheelerProduct CreateFourWheelerProduct()
{
return new Jeep();
}
}
/// <summary>
/// The 'AbstractProductA' abstract class
/// </summary>
abstract class TwoWheelerProduct
{
}
/// <summary>
/// The 'AbstractProductB' abstract class
/// </summary>
abstract class FourWheelerProduct
{
//Disclaimer: I am a temporary citizen...
public abstract void RunOver(TwoWheelerProduct twoWheeler);
}
/// <summary>
/// The 'ProductA1' class
/// </summary>
class Scooter : TwoWheelerProduct
{
}
/// <summary>
/// The 'ProductB1' class
/// </summary>
class Car : FourWheelerProduct
{
public override void RunOver(TwoWheelerProduct twoWheeler)
{
Console.WriteLine(this.GetType().Name +
" smashes into " + a.GetType().Name);
}
}
/// <summary>
/// The 'ProductA2' class
/// </summary>
class Bike : TwoWheelerProduct
{
}
/// <summary>
/// The 'ProductB2' class
/// </summary>
class Jeep : FourWheelerProduct
{
public override void RunsOver(TwoWheelerProduct twoWheeler)
{
Console.WriteLine(this.GetType().Name +
" collides with " + a.GetType().Name);
}
}
/// <summary>
/// The 'Client' class. Interaction environment for the products.
/// </summary>
class Client
{
private TwoWheelerProduct _abstractTwoWheeler;
private FourWheelerProduct _abstractFourWheeler;
// Constructor
public Client(AbstractFactory factory)
{
_abstractTwoWheeler = factory.CreateTwoWheeler();
_abstractFourWheeler = factory.CreateFourWheeler();
}
public void Run()
{
_abstractFourWheeler.RunOver(_abstractTwoWheeler);
}
}