gpt4 book ai didi

c# - 设计问题 - 面向对象的食品应用

转载 作者:行者123 更新时间:2023-11-30 15:11:29 26 4
gpt4 key购买 nike

假设我有许多用户控件,每个用户控件都在一个 tabitem 中,在一个窗口中。

例如,假设这是一个食物收集应用程序。然后我们有标签水果、蔬菜和零食。每个选项卡将显示该主题的食物列表,并允许用户添加、删除、修改每个部分中的食物。食物存储在单独的文本文件中,即 Fruit.txt、Vegetable.txt、Snack.txt

实际的文本文件可能看起来像这样 (vegetable.txt):

Name        Carbs    Fat
Eggplant 2 1.1
Cucumber 3 0.5
etc

现在这是一个大列表,有一个加载方法可以将所有蔬菜拉出到一个列表中

我的问题是这个 loadVegetables 方法在代码隐藏文件中,我最终到处重复这个加载方法,因为我有另一个屏幕,如 ReviewAllFood、AddVegetable 等,以及所有其他屏幕加载水果和零食的方法。

这更像是一个设计问题,我想知道如何设置它才能不重复这段代码。我可以有一个加载方法所在的 VegetableManager(或其他)类,但这是否真的意味着重复代码更少?然后在每个屏幕中我必须创建 VegetableManager 的对象并调用它的加载方法。所以我想效率方面并没有更好,但我确实实现了更好的设计。

我想我在这里遗漏了一些东西。自从我研究内聚和耦合以来已经有一段时间了,我想我现在对这些概念感到困惑。如果有人可以针对这种情况提出设计建议并解释他们为什么选择它以及为什么它比我目前的做法更好,我们将不胜感激。

感谢阅读。

最佳答案

I could have a VegetableManager (or something) class where the load method is, but does this actually mean less repeated code? Then in each screen I have to create object of VegetableManager and call its load method anyway.

这样做的重点不是效率(即性能)。重点是将加载该数据的细节封装到一个独立的对象中。举例来说,您的站点变得非常大,您决定将数据存储移动到数据库以实现可伸缩性和性能。在您描述的现有代码中,您必须遍历每个用户控件或页面并更改加载方法的逻辑。在最好的情况下,这是一种痛苦,在最坏的情况下,你会错过一些或错误地复制粘贴。如果将逻辑封装到专用对象中,其唯一职责就是知道如何从某处加载数据,那么您只需进行一次更改。

用户控件的代码隐藏:

protected void Page_Load(object sender, EventArgs e) {
var veggieManager = new VegetableManager();
VeggieListControl.DataSource = veggieManager.GetAll();
VeggieListControl.DataBind();
}

蔬菜管理器.cs:

public class VegetableManager {
private static Collection<Vegetable> _veggies;
private static object _veggieLock;

public ReadOnlyCollection<Vegetable> GetAll() {
if (_veggies == null) {
lock(_veggieLock) { //synchronize access to shared data
if (_veggies == null) { // double-checked lock
// logic to load the data into _veggies
}
}
}

return new ReadOnlyCollection(_veggies);
}

public void Add(Vegetable veggie) {
GetAll(); // call this to ensure that the data is loaded into _veggies
lock(_veggieLock) { //synchronize access to shared data
_veggies.Add(veggie);
// logic to write out the updated list of _veggies to the file
}
}
}

因为 _veggiesstatic,内存中只有一个 veggies 集合,尽管多个调用者将实例化 VegetableManager。但是因为它是静态的,如果您有一个多线程应用程序(例如网站),您必须在所有线程之间同步对该字段的访问(因此 lock)。

这只是良好面向对象方面的冰山一角。我建议仔细阅读 UncleBob's SOLID principles , 和 Domain-Driven Design (free e-book)。

因此,是的,您正在重复某些内容,但您所重复的只是一个方法调用,并且可以重复。 DRY 意味着减少“逻辑”代码的重复,即决策和算法;简单的方法调用不属于此范围。但是,如果你愿意,你可以将逻辑合并到一个基类中来执行此操作,从而有效地将用户控件隔离开来,而不必了解 VegetableManager,尽管我认为这是面向对象的矫枉过正,或者 OOO :-)

public abstract class FoodUserControl : UserControl {
protected List<Vegetable> GetVeggies() {
return new VegetableManager().GetAll();
}
}

然后您的实际控件将派生自此而不是派生自 UserControl。

更新

预加载 VegetableManager.cs:

public class VegetableManager {
private static Collection<Vegetable> _veggies;
private static object _veggieLock;

static VegetableManager() {
// logic to load veggies from file
}

public ReadOnlyCollection<Vegetable> GetAll() {
return new ReadOnlyCollection(_veggies);
}

public void Add(Vegetable veggie) {
lock(_veggieLock) { //synchronize access to shared data
_veggies.Add(veggie);
// logic to write out the updated list of _veggies to the file
}
}
}

请注意,此预加载版本不必围绕构造函数中的加载代码进行双重检查锁定。另请注意,加载代码位于 static 构造函数中,因为此代码初始化了一个 static 字段(否则,您将在每次构造时将文件中的数据重新加载到相同的共享 static 字段)。因为蔬菜是预先加载的,所以您不需要在 GetAll 或 Add 中加载。

关于c# - 设计问题 - 面向对象的食品应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2285366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com