-2

I need to make this magazine_list that holds only unique values and do it by using dictionary and multiton pattern.
List cannot have two objects with the same both name and price.
I found only one example of multiton pattern in c# and it didn't solve my problem.

It's simplified version of code that I already have, but these are the most important things of that problem.

public class Product
{
  string name;
  int price;
}

public class Coffee : Product 
{
    public Coffee(string _name, int _price)
    {
      name = _name;
      price = _price;
    }
}

public class Tea : Product 
{
    public Tea(string _name, int _price)
    {
      name = _name;
      price = _price;
    }
}

public class Magazine
{
    List<Product> magazine_list;

    public Magazine()
    {
      List<Product> magazine_list = new List<Product>();
    }

    public void add(Product p)
    {
      magazine_list.Add(p);
    }

}
4

2 に答える 2

0

商品名をキーにして、雑誌を辞書にしてみませんか。

public class Magazine : Dictionary<string, Product>
{
   public void Add(Product p)
   {
      base[p.name] = p;
   }
}
于 2015-01-12T02:36:45.433 に答える
0

使用できるのは、string識別子とhashset. デフォルトのハッシュセットは一意性を保証します。

于 2015-01-12T02:37:48.080 に答える