G
Gustaf
I'm working on an object model that will represent trees of products, where one product may contain others (components of a product). Each product needs to keep track of its parent and child products. That far it seems I'll only need one class:
public class Product
{
private string name;
private Product parent;
private List<Product> children;
public Product(string name)
{
this.name = name;
this.parent = null;
this.children = new List<Product>();
}
public string Name
{
get { return this.name; }
}
public void AddChild(Product product)
{
product.parent = this;
this.children.Add(product);
}
public Product Parent
{
get { return this.parent; }
set { this.parent = value; }
}
public List<Product> Children
{
get { return this.children; }
}
public void Traverse()
{
Console.WriteLine(this.name);
foreach (Product product in this.children)
{
product.Traverse();
}
}
}
But there's another requirement: there may be several variants of each product. So in addition to having children, any product in the tree is potentially a collection of alternative products! This requirement seem to complicate the model a great deal. When looking for a solution, I found the Composite design pattern:
http://en.wikipedia.org/wiki/Composite_pattern
If I understand it right, I'd need three classes:
public abstract class ComponentProduct
public class Product : ComponentProduct
public class CompositeProduct : ComponentProduct
I'd appreciate comments on whether this is a good solution in my case, or whether there's an obvious and simple solution that eludes me.
Also, the way I implement Children above has a serious flaw. The calling method gets access to all the members of the List<> class, which makes it possible to side-track the AddChild() method, by adding children like this:
myProduct.Children.Add(new Product(...));
How do I prevent that?
Many thanks,
Gustaf
public class Product
{
private string name;
private Product parent;
private List<Product> children;
public Product(string name)
{
this.name = name;
this.parent = null;
this.children = new List<Product>();
}
public string Name
{
get { return this.name; }
}
public void AddChild(Product product)
{
product.parent = this;
this.children.Add(product);
}
public Product Parent
{
get { return this.parent; }
set { this.parent = value; }
}
public List<Product> Children
{
get { return this.children; }
}
public void Traverse()
{
Console.WriteLine(this.name);
foreach (Product product in this.children)
{
product.Traverse();
}
}
}
But there's another requirement: there may be several variants of each product. So in addition to having children, any product in the tree is potentially a collection of alternative products! This requirement seem to complicate the model a great deal. When looking for a solution, I found the Composite design pattern:
http://en.wikipedia.org/wiki/Composite_pattern
If I understand it right, I'd need three classes:
public abstract class ComponentProduct
public class Product : ComponentProduct
public class CompositeProduct : ComponentProduct
I'd appreciate comments on whether this is a good solution in my case, or whether there's an obvious and simple solution that eludes me.
Also, the way I implement Children above has a serious flaw. The calling method gets access to all the members of the List<> class, which makes it possible to side-track the AddChild() method, by adding children like this:
myProduct.Children.Add(new Product(...));
How do I prevent that?
Many thanks,
Gustaf