【发布时间】:2021-03-03 08:18:56
【问题描述】:
我现在是通过在线内容和你们进行编程和学习的新手!我正在阅读有关工厂设计模式并尝试在非常基本的项目中实现,我有一个解决方案有两个项目,一个项目包含接口,另一个包含实现,我已经阅读了有关工厂的信息,但不幸的是,我不知道如何在我的项目中实现,在一个项目中,我有 2 个接口 IBasicCars 和 ILuxuryCars,IluxuryCars 实现 IBasicCars,然后在第二个项目中,我有一个从 ILuxuryCars 继承并实现其所有方法和 IBasicCars 方法和属性的类,这是我的代码为那个班级。
public class LuxuryCars : ILuxuryCar
{
private string _color { get; set; }
public string Color
{
get
{
return _color;
}
set
{
_color = value;
}
}
private int _model { get; set; }
public int Model
{
get
{
return _model;
}
set
{
_model = value;
}
}
private string _make { get; set; }
public string Make
{
get
{
return _make;
}
set
{
_make = value;
}
}
public void Break()
{
Console.WriteLine("This is the basic function of all cars !!!");
}
public void CruiseControl()
{
Console.WriteLine("This is the luxury feature for luxury cars !!!");
}
public void Drive()
{
Console.WriteLine("This is the basic function of all cars !!!");
}
public void Navigation()
{
Console.WriteLine("This is the luxury feature for luxury cars !!!");
}
public void Park()
{
Console.WriteLine("This is the basic function of all cars !!!");
}
}
现在我在那个项目中有另一个类“FactoryObject”,它现在什么都没有,有人可以告诉我如何实现工厂设计模式吗?
这就是我在 main 方法中调用这些方法的方式
static void Main(string[] args)
{
ILuxuryCar lc = new LuxuryCars();
lc.Color = "Black";
lc.Make = "Honda";
lc.Model = 2007;
Console.WriteLine("Car color is: {0} Made by: {1} Model is: {2}", lc.Color, lc.Make, lc.Model);
lc.Navigation();
lc.CruiseControl();
lc.Break();
lc.Drive();
lc.Park();
Console.WriteLine();
IBasicCar b = new LuxuryCars();
b.Color = "Red";
b.Make = "Alto";
b.Model = 2019;
Console.WriteLine("Car color is: {0} Made by: {1} Model is: {2}", lc.Color, lc.Make, lc.Model);
lc.Break();
lc.Drive();
lc.Park();
Console.ReadLine();
}
【问题讨论】:
-
Î 假设不要在
Main中写new LuxureCars(),而应该在工厂中这样做,因为创建其他对象是工厂的工作。
标签: c# console-application factory factory-pattern