【发布时间】:2010-11-01 00:28:37
【问题描述】:
我正在努力思考如何对我的代码进行企业化:采用一个简单的例程并将其拆分为 3 或 4 个类中的 5 或 6 个方法。
我很快想出了三个简单的代码示例我目前是如何编写的。有人可以将这些转换成 MVC/MVP 混淆版本吗?
示例 1:姓氏是强制性的。如果未输入任何内容,则将文本框涂成红色。如果输入内容,则将其涂成绿色:
private void txtLastname_TextChanged(object sender, EventArgs e)
{
//Lastname mandatory.
//Color pinkish if nothing entered. Greenish if entered.
if (txtLastname.Text.Trim() == "")
{
//Lastname is required, color pinkish
txtLastname.BackColor = ControlBad;
}
else
{
//Lastname entered, remove the coloring
txtLastname.BackColor = ControlGood;
}
}
示例 2:名字 是可选的,但尝试 获取它。我们将为这个“try to get”字段添加蓝色调:
private void txtFirstname_TextChanged(object sender, EventArgs e)
{
//Firstname can be blank.
//Hint them that they should *try* to get it with a bluish color.
//If they do enter stuff: it better be not all spaces.
if (txtFirstname.Text == "")
{
//Nothing there, hint it blue
txtFirstname.BackColor = ControlRequired;
}
else if (txtFirstname.Text.Trim() == "")
{
//They entered spaces - bad user!
txtFirstname.BackColor = ControlBad;
}
else
{
//Entered stuff, remove coloring
txtFirstname.BackColor = SystemColors.Window;
}
}
示例 3age 完全是可选的。如果输入了年龄,最好是有效的:
private void txtAge_TextChanged(object sender, EventArgs e)
{
//Age is optional, but if entered it better be valid
int nAge = 0;
if (Int32.TryParse(txtAge.Text, out nAge))
{
//Valid integer entered
if (nAge < 0)
{
//Negative age? i don't think so
txtAge.BackColor = ControlBad;
}
else
{
//Valid age entered, remove coloring
txtAge.BackColor = SystemColors.Window;
}
}
else
{
//Whatever is in there: it's *not* a valid integer,
if (txtAge.Text == "")
{
//Blank is okay
txtAge.BackColor = SystemColors.Window;
}
else
{
//Not a valid age, bad user
txtAge.BackColor = ControlBad;
}
}
}
每次我看到 MVC 代码时,它看起来几乎就像将代码随机拆分为不同的方法、类和文件。我无法确定他们疯狂的原因或模式。如果不了解他们为什么这是某种方式,这是没有意义的。并且使用 model、view、controller 和 presenter 等词,就像我应该知道这意味着什么,没有帮助。
模型就是你的数据。
视图在屏幕上显示数据。
控制器用于执行 用户操作
橙子尝起来是橙色的。
这是我为了使代码更难理解而尝试拆分的尝试。这是靠近 MVC 的地方吗?
private void txtFirstname_TextChanged(object sender, EventArgs e)
{
FirstnameTextChangedHandler(sender, e);
}
private void FirstnameTextChangedHandler(sender, e)
{
string firstname = GetFirstname();
Color firstnameTextBoxColor = GetFirstnameTextBoxColor(firstname);
SetFirstNameTextBoxColor(firstnameTextBoxColor);
}
private string GetFirstname()
{
return txtFirstname.Text;
}
private Color GetFirstnameTextBoxColor(string firstname)
{
//Firstname can be blank.
//Hint them that they should *try* to get it with a bluish color.
//If they do enter stuff: it better be not all spaces.
if (firstname == "")
{
//Nothing there, hint it blue
return GetControlRequiredColor();
}
else if (firstname.Trim() == "")
{
//They entered spaces - bad user!
return GetControlBadColor();
}
else
{
//Entered stuff, remove coloring
return GetControlDefaultColor();
}
}
private Color GetControlRequiredColor()
{
return ControlRequired;
}
private Color GetControlBadColor()
{
return ControlBad;
}
private Color GetControlGoodColor()
{
return ControlGood;
}
//am i doin it rite
我已经混淆了代码,但它仍然是完全一样的。我认为 MVC 混淆的下一步是将代码隐藏在 3 或 4 个不同的文件中。
这是我不明白的下一步。将哪些功能移到其他哪些类中的逻辑分离是什么?有人可以将我上面的 3 个简单示例翻译成成熟的 MVC 混淆吗?
编辑:不是 ASP/ASP.NET/Online。假装它在台式机、手持设备、表面、信息亭上。并假装它与语言无关。
【问题讨论】:
-
你在撒尿吗?
-
我不确定这意味着什么。是英国俚语吗?
-
"如何企业化我的代码";分离关注点并在适当的地方使用模式称为“好”代码,而不是企业代码。
标签: model-view-controller language-agnostic design-patterns