-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);
    }

}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3308470
  • 107
  • 1
  • 3
  • 10
  • @MitchWheat http://en.wikipedia.org/wiki/Multiton_pattern – user3308470 Jan 12 '15 at 02:36
  • what you have implemented looks nothing like what is described in the wiki you linked to! – Mitch Wheat Jan 12 '15 at 02:37
  • What is the problem you are having? Do you not know how to make the key unique on both Name and Price? This is one way: http://stackoverflow.com/questions/2877660/composite-key-dictionary – AaronLS Jan 12 '15 at 02:38
  • @AaronLS The problem isn't that I dont know how to make it unique. Main problem is that I need to make it unique using Multiton design pattern ( it's for school.. ). – user3308470 Jan 12 '15 at 02:42
  • @user3308470 Ok, you know how to make it unique, so that solves: "List cannot have two objects with the same both name and price.". Then yous say "found only one example of multiton pattern in c# and it didnt solve my problem." What is the problem you're having? You know how to make the dictionary unique, you have an implementation of multiton pattern. Seems you've got all the pieces. I've read your question couple times and you don't make clear where you're running into a problem. – AaronLS Jan 12 '15 at 02:45
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 12 '15 at 02:46
  • @AaronLS In the example there is a dictionary in class A that stores objects of type A and use constructor of class A. I have dictionary inside class B that stores objects of type A and uses constructor of class C. – user3308470 Jan 12 '15 at 02:53

2 Answers2

0

Why not make Magazine a dictionary, with the key being product name.

public class Magazine : Dictionary<string, Product>
{
   public void Add(Product p)
   {
      base[p.name] = p;
   }
}
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
  • Can it be called Multiton pattern then? The problem is I have to do project with 9-10 exact patterns including multiton. – user3308470 Jan 12 '15 at 02:39
  • 1
    Oh! This seems an academic problem then. Instead of trying to fit a pattern where it doesn't, try to modify the problem space where this pattern actually fits. For instance, it doesn't make sense to have the product name and price as unique. You should simplify this to have your product have a unique identifier. – sudheeshix Jan 12 '15 at 02:43
0

What you could use is a self-defined class that contains a string identifier and a hashset. The hashset by default will ensure uniqueness.

Jonathan Holland
  • 607
  • 9
  • 18
  • But will it still be Multiton design pattern? The main problem is that I need to do it with this design pattern (it's for school..). – user3308470 Jan 12 '15 at 02:41
  • Hmmm, If you make a Map where your multiton is another class where you ensure users can't access it and each key in the map is unique, that would suit it better than a hashset if a multiton style is a requirement – Jonathan Holland Jan 12 '15 at 02:47