User1899289003 User1899289003 - 14 days ago 5
C# Question

Composite Design Pattern

I have a ListBox with elements and I need to display the elements, I have that working but I want to add an option to remove the selected item, here is the problem... I removed it but I was removing from the listbox not from the elements class, what can I do to remove the items from the tree? Here is my code:

Form 1:

private void btnBuildPartsList_Click(object sender, EventArgs e)
{
Assembly topWidget = new Assembly("Main Assembly", 1);
Assembly chassis = new Assembly("Chassis", 1);
Assembly display = new Assembly("Display", 1);
Assembly powerSupply = new Assembly("Power Supply", 1);
Part bolt = new Part("Bolt", 24);
Part mainCase = new Part("Case", 1);
Part screen = new Part("Screen", 3);
Part displayElectronics = new Part("Display Electronics", 3);
Part transformer = new Part("Transformer", 1);
Part powerBoard = new Part("Power Board", 1);
Part ductTape = new Part("Duct Tape", 5);
Part gum = new Part("Bubble Gum", 25);

topWidget.Add(chassis);
topWidget.Add(display);
topWidget.Add(powerSupply);
chassis.Add(mainCase);
chassis.Add(bolt);
display.Add(screen);
display.Add(displayElectronics);
powerSupply.Add(transformer);
powerSupply.Add(powerBoard);
topWidget.Add(ductTape);
topWidget.Add(gum);

DisplayPartsList(topWidget);
}

private void DisplayPartsList(Assembly topWidget)
{
List<string> partsList = new List<string>();
topWidget.WritePartsList(partsList);

listParts.Items.Clear();
listParts.Items.AddRange(partsList.ToArray());
}


And here is my Assembly class:

namespace Composite
{
public class Assembly : BaseAssembly
{
private List<BaseAssembly> elements = new List<BaseAssembly>();

public Assembly(string name, int quantity)
: base(name, quantity)
{
}

public override void Add(BaseAssembly assembly)
{
elements.Add(assembly);
}

public override void Remove(BaseAssembly assembly)
{
elements.Remove(assembly);
}

public override void WritePartsList(List<string> list)
{
list.Add(Quantity.ToString() + " - " + Name);

foreach (BaseAssembly assembly in elements)
{
assembly.WritePartsList(list);
}
}
}
}


I think that this code is enough, all the other code it's like in other Composite Desing pattern code, now, how can I do that remove item function? Selecting
x
element from my listbox. Like I said before I was removing the item from
the listbox
but I want to remove it from all the class. Thanks in advance.

Answer

I have one solution, first of all put this code as global variable:

Assembly topWidget = new Assembly("Main Assembly", 1);    
Assembly chassis = new Assembly("Chassis", 1);
Assembly display = new Assembly("Display", 1);    
Assembly powerSupply = new Assembly("Power Supply", 1);    
Part bolt = new Part("Bolt", 24);    
Part mainCase = new Part("Case", 1);
Part screen = new Part("Screen", 3);
Part displayElectronics = new Part("Display Electronics", 3);
Part transformer = new Part("Transformer", 1);
Part powerBoard = new Part("Power Board", 1);
Part ductTape = new Part("Duct Tape", 5);
Part gum = new Part("Bubble Gum", 25);

After that you need to handle the delete function how you going to do that? With a button or only selecting the element from the listbox? That depends on you, I'll give you the code and then you only change what you need.

Your elements have this structure for example:

A
A,B
A,B,C

In this case A is topWidget B are chassis, display, powerSupply and C are mainCase, bolt, screen, displayElectronics... etc with that information you can choose what you want to delete, in other words, if you delete B element you will delete all the C elements too, so you need to choose if you want to delete only a C element or all the B elements, you can do this using a switch, and what you need to send? The selected item from the ListBox but, your ListBox contains items like this: 24 - Bolt right? Then you only need to make an split to get only Bolt or whatever.

var select = listParts.SelectedItem.ToString().IndexOf('-');
string selectVal = listParts.SelectedItem.ToString().Substring(select + 1).Trim();

With that you will get the same string that you add, the you only need to handle the switch and use the Remove function, something like this:

switch (selectVal)
{
   //This will remove all the elements of powerSupply (transformer, powerBoard )
   case "Power Supply":
      topWidget.Remove(powerSupply);
      break;
}

Or you can do this to delete the C element one by one

switch (selectVal)
{
   //This will remove only "Transformer".
   case "Transformer":
      powerSupply.Remove(transformer);
      break;
}

And remember, after the switch you need to refresh your ListBox (call again DisplayPartsList(topWidget);)

You can combine both cases without problems, I hope this help you.

Comments