Back to list
Views:   6.5K
Replies:  1

Pattern for Product to Product Variant association

Is there a design pattern to solve the product to product variant association? A product varies from another product by only it's color, size or material only. Every other static properties like name and description are the same.

I looked at the type-object / item-descriptor pattern which does not seem to be a fit. In this case, I'm not creating two types of products but the same type of product but varies in dynamic properties. I don't know these properties (color, size and material) at compile time. I know what they are at run time only.

Any idea?

Varghese Pallathu, Apr 16, 2012
Reply 1
I've a simple pattern I like to use in cases such as these.  I call it the comparator pattern.  Here's a simple Console app that can be used to describe a possible solution.

I hope this helps

class Program
        static void Main(string[] args)
            var productA = new Product("Red", "Shoes", "10", "Rubber");
            var productB = new Product("Red", "Shoes", "10", "Rubber");
            var productC = new Product("Green", "Shoes", "8", "Plastic");

            if (productA.IsEqualTo(productB)){Console.WriteLine("Product A and Product B are the same");}
            if (!productB.IsEqualTo(productC)){Console.WriteLine("Product B and Product C are NOT the same"); }



    public class Product
        private readonly string color, name, size, material;

        public Product(string color, string name, string size, string material)
            this.color = color;
   = name;
            this.size = size;
            this.material = material;

        public bool IsEqualTo(Product product)
            return (product.color == color && == name) && product.material == material;

Sameer Lamba, Apr 18, 2012
Thats a good point and one possible answer. I think thats why singleton class can be used for state mangement in stateless scenarios like shopping cart scenario,
Apr 17, 2012
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.