Design Question - OO Food Application

Let's say I have several user controls, each user control inside a tabout inside a window.

For example, let's say this is a food collection application. Then we have the Fruits, Vegetables and Snacks tabs. On each tab, a list of products of this object will be displayed and it is allowed for the user to add, delete and change power in each section. Food is stored in separate text files, i.e. Fruit.txt, Vegetable.txt, Snack.txt

Actual text files might look something like this (vegetable.txt):

Name        Carbs    Fat
Eggplant    2        1.1
Cucumber    3        0.5
etc

Now this is a big list, and there is a boot method that pulls all the vegetables into a list

The question I have is that this loadVegetables method is in the code behind the file, and I end up repeating this loading method all over the place because I have another screen like ReviewAllFood, AddVegetable, etc. along with all other methods of loading fruits and snacks.

This is more of a design issue, I wonder how I set it up so as not to repeat this code. I could have a VegetableManager class (or something like that) that has a loading method in it, but does this really mean less duplicate code? Then on each screen I need to create a VegetableManager object and in any case call its loading method. Therefore, I believe that efficiency is not better, but I have achieved a better design.

, - . , , , . , - , , , .

.

+3
4

VegetableManager ( -) , , ? VegetableManager .

, (.. ). , . , , , . , , . , - . , - , -, .

codebehind :

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

VegetableManager.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
    }
  }
}

_veggies - static, veggies, , VegetableManager. , (, -), (, lock s).

. UncleBob SOLID , ( ).

, , - , , , , . DRY "" , ; . , , , , VegetableManager, , OOO: -)

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

, UserControl.

Eager-loading 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). veggies , GetAll Add.

+3

( , ) , . . , , . , .

EDIT:

List<T> loadObjects(File file, ILineConversionStrategy strategy) {
   // read eaqch line of the file
   // for each line
   T object = strategy.readLine(line);
   list.add(object);
   return listOfObjects;
}

EDIT 2:

class FoodModel {
   List<Vegetable> getVegetables();
   List<Fruit> getFruit();
   // etc
}
+2
    public interface IEatable {}

    class Vegitable : IEatable 
    { string Name { get; set; } }
    class Fruit : IEatable 
    { string Name { get; set; } }

    public interface IEatableManager
    {
        List<Vegitables> LoadEatables(string filePath);
    }
    public class VetabaleManager : IEatableManager
    {
        #region IEatableManagerMembers    
        public List<Vegitable> LoadVegs(string filePath)
        {
            throw new NotImplementedException();
        }    
        #endregion
    }
    .
    .
    .

, ,

a :

0

. , :

public class FoodRepository
{
    public IList<Vegetable> GetVegetables() { ... }
    public IList<Fruit> GetFruit() { ... }
    // etc.
}

This class should be the only class in your application that knows that the products are actually stored in text files.

Once you get this working, you may want to cache frequently used data for better performance.

0
source

Source: https://habr.com/ru/post/1733287/


All Articles