- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近一直在学习更多关于设计模式的知识,并想尝试一下。我不确定这是否是使用工厂模式的正确方法,并且想知道是否有人可以向我提供反馈?
我基本上有一组相似的日历项。膳食、锻炼和测量。它们都有日期和姓名,并且可以打印详细信息。但除此之外,他们还有一些他们各自需要的不同元素。
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
最佳答案
下面是我要将其更改为:
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();
}
}
}
需要注意的几件事:
仍有一些事情可以改变。具有公共(public)属性的基类仍然是一件坏事。但这是一个很大的改进。
我还删除了对 StartLoop() 的递归调用。当您只需要一个循环时,您不想增加堆栈。
编辑:对工厂类进行了一些更改。
回应评论:
总体来说还不错:)
关于design-patterns - 有人可以批评我的工厂模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3400077/
对此感到疯狂,真的缺少一些东西。 我有webpack 4.6.0,webpack-cli ^ 2.1.2,所以是最新的。 在文档(https://webpack.js.org/concepts/mod
object Host "os.google.com" { import "windows" address = "linux.google.com" groups = ["linux"] } obj
每当我安装我的应用程序时,我都可以将数据库从 Assets 文件夹复制到 /data/data/packagename/databases/ .到此为止,应用程序工作得很好。 但 10 或 15 秒后
我在 cc 模式缓冲区中使用 hideshow.el 来折叠我不查看的文件部分。 如果能够在 XML 文档中做到这一点就好了。我使用 emacs 22.2.1 和内置的 sgml-mode 进行 xm
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
根据java: public Scanner useDelimiter(String pattern) Sets this scanner's delimiting pattern to a patt
我读过一些关于 PRG 模式以及它如何防止用户重新提交表单的文章。比如this post有一张不错的图: 我能理解为什么在收到 2xx 后用户刷新页面时不会发生表单提交。但我仍然想知道: (1) 如果
看看下面的图片,您可能会清楚地看到这一点。 那么如何在带有其他一些 View 的简单屏幕中实现没有任何弹出/对话框/模式的微调器日期选择器? 我在整个网络上进行了谷歌搜索,但没有找到与之相关的任何合适
我不知道该怎么做,我一直遇到问题。 以下是代码: rows = int(input()) for i in range(1,rows): for j in range(1,i+1):
我想为重写创建一个正则表达式。 将所有请求重写为 index.php(不需要匹配),它不是以/api 开头,或者不是以('.html',或'.js'或'.css'或'.png'结束) 我的例子还是这样
MVC模式代表 Model-View-Controller(模型-视图-控制器) 模式 MVC模式用于应用程序的分层开发 Model(模型) - 模型代表一个存取数据的对象或 JAVA PO
我想为组织模式创建一个 RDF 模式世界。您可能知道,组织模式文档基于层次结构大纲,其中标题是主要的分组实体。 * March auxiliary :PROPERTIES: :HLEVEL: 1 :E
我正在编写一个可以从文件中读取 JSON 数据的软件。该文件包含“person”——一个值为对象数组的对象。我打算使用 JSON 模式验证库来验证内容,而不是自己编写代码。符合代表以下数据的 JSON
假设我有 4 张 table 人 公司 团体 和 账单 现在bills/persons和bills/companys和bills/groups之间是多对多的关系。 我看到了 4 种可能的 sql 模式
假设您有这样的文档: doc1: id:1 text: ... references: Journal1, 2013, pag 123 references: Journal2, 2014,
我有这个架构。它检查评论,目前工作正常。 var schema = { id: '', type: 'object', additionalProperties: false, pro
这可能很简单,但有人可以解释为什么以下模式匹配不明智吗?它说其他规则,例如1, 0, _ 永远不会匹配。 let matchTest(n : int) = let ran = new Rand
我有以下选择序列作为 XML 模式的一部分。理想情况下,我想要一个序列: 来自 my:namespace 的元素必须严格解析。 来自任何其他命名空间的元素,不包括 ##targetNamespace和
我希望编写一个 json 模式来涵盖这个(简化的)示例 { "errorMessage": "", "nbRunningQueries": 0, "isError": Fals
首先,我是 f# 的新手,所以也许答案很明显,但我没有看到。所以我有一些带有 id 和值的元组。我知道我正在寻找的 id,我想从我传入的三个元组中选择正确的元组。我打算用两个 match 语句来做到这
我是一名优秀的程序员,十分优秀!