我最近一直在学习更多关于设计模式的知识,并且我认为我会对它进行一些修改。我不确定这是否是使用工厂模式的正确方法,并且想知道是否有人可以向我提供反馈?
我基本上有一组相似的日历项目。膳食,锻炼和测量。它们都有日期和名称,可以打印详细信息。但除此之外,他们有一些项目,他们每个人都需要不同的东西。
public enum AppointmentType
{
Workout,
Meal,
Measurement
}
public abstract class Appointment
{
public string Name { get; set; }
public DateTime DateStarted { get; set; }
public virtual string PrintDetails()
{
return string.Format("Type: {0}\nName: {1}\nDate: {2}",this.GetType().ToString(), Name, DateStarted.ToShortDateString());
}
}
public class Workout : Appointment
{
public List<string> Exercises { get; set; }
public Workout()
{
Exercises = new List<string>();
}
public override string PrintDetails()
{
string startInfo = base.PrintDetails();
string addedInfo = "\nToday I will be doing the following:\n";
foreach (string exercise in Exercises)
{
addedInfo += string.Format("{0}\n", exercise);
}
return startInfo + addedInfo;
}
}
public class Meal : Appointment
{
public List<string> FoodItems { get; set; }
public Meal()
{
FoodItems = new List<string>();
}
public override string PrintDetails()
{
string startInfo = base.PrintDetails();
string addedInfo = "\nToday I will be eating the following:\n";
foreach (string foodItem in FoodItems)
{
addedInfo += string.Format("{0}\n", foodItem);
}
return startInfo + addedInfo;
}
}
public class Measurement : Appointment
{
public string Height { get; set; }
public string Weight { get; set; }
public override string PrintDetails()
{
string startInfo = base.PrintDetails();
string addedInfo = string.Format("\nI am {0} feet tall and I weight {1} pounds!\n", Height, Weight);
return startInfo + addedInfo;
}
}
public interface IAppointmentFactory
{
Appointment CreateAppointment(AppointmentType appointmentType);
}
public class AppointmentFactory : IAppointmentFactory
{
public Appointment CreateAppointment(AppointmentType appointmentType)
{
switch (appointmentType)
{
case AppointmentType.Workout:
return new Workout();
case AppointmentType.Meal:
return new Meal();
case AppointmentType.Measurement:
return new Measurement();
default:
return new Workout();
}
}
}
这是一个使用该库的程序:
class Program
{
public static List<Appointment> myAppointments;
public static AppointmentFactory factory;
public static Appointment myAppointment;
static void Main(string[] args)
{
myAppointments = new List<Appointment>();
factory = new AppointmentFactory();
StartLoop();
}
private static void StartLoop()
{
Console.WriteLine(string.Format("\nWelcome to Appointment App: You have {0} appointment(s)!", myAppointments.Count()));
Console.WriteLine("0 = Exit");
Console.WriteLine("1 = New Workout");
Console.WriteLine("2 = New Meal");
Console.WriteLine("3 = New Measurement");
Console.WriteLine("P = Print Schedule\n");
switch (Console.ReadLine().ToUpper())
{
case "0":
return;
case "1":
CreateNewAppointment(AppointmentType.Workout);
AddExercises();
break;
case "2":
CreateNewAppointment(AppointmentType.Meal);
AddFoodItems();
break;
case "3":
CreateNewAppointment(AppointmentType.Measurement);
GetMeasurements();
break;
case "P":
PrintSchedule();
break;
default:
return;
}
StartLoop();
}
private static void GetMeasurements()
{
Console.WriteLine("How tall are you?");
((Measurement)myAppointment).Height = Console.ReadLine();
Console.WriteLine("What is your weight?");
((Measurement)myAppointment).Weight = Console.ReadLine();
}
private static void AddFoodItems()
{
Console.WriteLine("How many food items do you want to add?");
string exerciseCount = Console.ReadLine();
for (int i = 0; i < Convert.ToInt32(exerciseCount); i++)
{
Console.WriteLine(string.Format("Food {0}:", (i + 1)));
((Meal)myAppointment).FoodItems.Add(Console.ReadLine());
}
}
private static void AddExercises()
{
Console.WriteLine("How many exercises do you want to add?");
string exerciseCount = Console.ReadLine();
for (int i = 0; i < Convert.ToInt32(exerciseCount); i++)
{
Console.WriteLine(string.Format("Exercise {0}:", (i + 1)));
((Workout)myAppointment).Exercises.Add(Console.ReadLine());
}
}
private static void PrintSchedule()
{
foreach (Appointment appointment in myAppointments)
{
Console.WriteLine(appointment.PrintDetails());
}
}
public static void CreateNewAppointment(AppointmentType appointmentType)
{
myAppointment = factory.CreateAppointment(appointmentType);
Console.WriteLine("Name:");
myAppointment.Name = Console.ReadLine();
Console.WriteLine("Start Date:");
myAppointment.Name = Console.ReadLine();
myAppointments.Add(myAppointment);
}
}
谢谢!
-ajax
答案 0 :(得分:3)
以下是我将其更改为:
public enum AppointmentType
{
Workout,
Meal,
Measurement
}
public abstract class Appointment
{
public string Name { get; set; }
public DateTime DateStarted { get; set; }
public abstract void PrintDetails();
public abstract void RequestInputFromUser();
}
public class Workout : Appointment
{
private List<string> Exercises { get; set; }
public Workout()
{
Exercises = new List<string>();
}
public override void PrintDetails()
{
string startInfo = base.PrintDetails();
string addedInfo = "\nToday I will be doing the following:\n";
foreach (string exercise in Exercises)
{
addedInfo += string.Format("{0}\n", exercise);
}
Console.WriteLine(StartInfo + addedInfo);
}
public override void RequestInputFromUser()
{
Console.WriteLine("How many exercises do you want to add?");
string exerciseCount = Console.ReadLine();
for (int i = 0; i < Convert.ToInt32(exerciseCount); i++)
{
Console.WriteLine(string.Format("Exercise {0}:", (i + 1)));
Exercises.Add(Console.ReadLine());
}
}
}
public class Meal : Appointment
{
private List<string> FoodItems { get; set; }
public Meal()
{
FoodItems = new List<string>();
}
public override void PrintDetails()
{
string startInfo = base.PrintDetails();
string addedInfo = "\nToday I will be eating the following:\n";
foreach (string foodItem in FoodItems)
{
addedInfo += string.Format("{0}\n", foodItem);
}
Console.WriteLine(startInfo + addedInfo);
}
public override void RequestInputFromUser()
{
Console.WriteLine("How many food items do you want to add?");
string exerciseCount = Console.ReadLine();
for (int i = 0; i < Convert.ToInt32(exerciseCount); i++)
{
Console.WriteLine(string.Format("Food {0}:", (i + 1)));
FoodItems.Add(Console.ReadLine());
}
}
}
public class Measurement : Appointment
{
private string Height { get; set; }
private string Weight { get; set; }
public override void PrintDetails()
{
string startInfo = base.PrintDetails();
string addedInfo = string.Format("\nI am {0} feet tall and I weight {1} pounds!\n", Height, Weight);
Console.WriteLine(startInfo + addedInfo);
}
public override void RequestInputFromUser()
{
Console.WriteLine("How tall are you?");
Height = Console.ReadLine();
Console.WriteLine("What is your weight?");
Weight = Console.ReadLine();
}
}
public interface IAppointmentFactory
{
Appointment CreateAppointment(AppointmentType appointmentType);
}
public class AppointmentFactory : IAppointmentFactory
{
public Appointment CreateAppointment(AppointmentType appointmentType)
{
Appointment apt;
switch (appointmentType)
{
case AppointmentType.Workout:
apt = new Workout();
case AppointmentType.Meal:
apt = new Meal();
case AppointmentType.Measurement:
apt = new Measurement();
default:
apt = new Workout();
}
Console.WriteLine("Name:");
apt.Name = Console.ReadLine();
Console.WriteLine("Start Date:");
apt.Name = Console.ReadLine(); // Logic error on this line.
// Change it to do what you mean.
apt.RequestInputFromUser();
return apt;
}
}
代码使用如下:
class Program
{
public static List<Appointment> myAppointments;
public static AppointmentFactory factory;
public static Appointment myAppointment;
static void Main(string[] args)
{
myAppointments = new List<Appointment>();
factory = new AppointmentFactory();
StartLoop(factory);
}
private static void StartLoop(IAppointmentFactory factory)
{
bool exit = false;
while(!exit)
{
Console.WriteLine(string.Format("\nWelcome to Appointment App: You have {0} appointment(s)!", myAppointments.Count()));
Console.WriteLine("0 = Exit");
Console.WriteLine("1 = New Workout");
Console.WriteLine("2 = New Meal");
Console.WriteLine("3 = New Measurement");
Console.WriteLine("P = Print Schedule\n");
switch (Console.ReadLine().ToUpper())
{
case "0":
exit = true;
break;
case "1":
myAppointments.Add( factory.CreateAppointment( AppointmentType.Workout ) );
break;
case "2":
myAppointments.Add( factory.CreateAppointment( AppointmentType.Meal ) );
break;
case "3":
myAppointments.Add( factory.CreateAppointment( AppointmentType.Measurement ) );
break;
case "P":
PrintSchedule();
break;
default:
exit = true;
}
}
}
private static void PrintSchedule()
{
foreach (Appointment appointment in myAppointments)
{
appointment.PrintDetails();
}
}
}
有几点需要注意:
仍有一些事情可以改变。具有公共属性的基类仍然是一件坏事。但这是一个很大的改进。
我还删除了对StartLoop()的递归调用。当你需要的只是一个循环时,你不想增长堆栈。
编辑:对Factory类进行了一些更改。
回应评论:
答案 1 :(得分:0)
据我了解,当您需要创建相同类型对象的不同集合时,将使用抽象工厂模式。因此,例如,您可能有一个出售
的工厂Supersize-Workout
Supersize-Meal
Supersize-Measurement
和另一个出售
的人Mini-Workout
Mini-Meal
Mini-Measurement
然后可以将这些工厂类别提供给需要创建锻炼,膳食和测量的系统,但不管他们是否超大或迷你,只要他们都是相同的。
在没有第二家工厂的情况下,我觉得你的抽象工厂有点奇怪。
答案 2 :(得分:0)
嗯,对于初学者 - 你的代码很好地遵循了模式。
您的特定实现会遇到一些问题(但无法解决的问题):
对参数使用枚举违反了OCP原则。如果您添加或更糟糕,从枚举中删除值,客户端将不得不重新编译以使用正确的(新)枚举。对于教科书答案 - 您可以使用字符串来表示您要创建的类型,然后在请求不受支持的类型时以其他方式抛出异常/处理它。
添加新的Factory实现将被强制仅支持原始工厂可以处理的相同返回类型 - 除非您可以重新编译(再次违反OCP)。根据您的情况 - 这可能没问题。
但除此之外 - 在我看来,这是一个可行的实施方案。